From 050397b723ed0208453346a3cc3139e965184416 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 26 Dec 2017 13:41:27 -0800 Subject: [PATCH] mark method that work with raw pointer unsafe --- .gitignore | 4 ++ examples/word-count-cls/.gitignore | 67 ------------------------------ examples/word-count/.gitignore | 67 ------------------------------ guide/.gitignore | 1 - src/instance.rs | 8 ++-- src/object.rs | 23 +++++----- 6 files changed, 19 insertions(+), 151 deletions(-) delete mode 100644 examples/word-count-cls/.gitignore delete mode 100644 examples/word-count/.gitignore delete mode 100644 guide/.gitignore diff --git a/.gitignore b/.gitignore index cb3d2508..9c6831e9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,11 @@ target Cargo.lock /doc /gh-pages +build/ +__pycache__/ +.cache *.so *.out +*.egg-info extensions/stamps/ diff --git a/examples/word-count-cls/.gitignore b/examples/word-count-cls/.gitignore deleted file mode 100644 index db8bc200..00000000 --- a/examples/word-count-cls/.gitignore +++ /dev/null @@ -1,67 +0,0 @@ -target/ -**/*.rs.bk -Cargo.lock - -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -bin/ -local/ -include/ -man/ -*.egg-info/ -.installed.cfg -*.egg -.ropeproject/ - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt -pip-selfcheck.json - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover - -# Translations -*.mo -*.pot - -# Django stuff: -*.log - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ diff --git a/examples/word-count/.gitignore b/examples/word-count/.gitignore deleted file mode 100644 index db8bc200..00000000 --- a/examples/word-count/.gitignore +++ /dev/null @@ -1,67 +0,0 @@ -target/ -**/*.rs.bk -Cargo.lock - -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -bin/ -local/ -include/ -man/ -*.egg-info/ -.installed.cfg -*.egg -.ropeproject/ - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt -pip-selfcheck.json - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover - -# Translations -*.mo -*.pot - -# Django stuff: -*.log - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ diff --git a/guide/.gitignore b/guide/.gitignore deleted file mode 100644 index 7585238e..00000000 --- a/guide/.gitignore +++ /dev/null @@ -1 +0,0 @@ -book diff --git a/src/instance.rs b/src/instance.rs index 04036ed2..cba8db7e 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -112,12 +112,12 @@ impl Py { /// Panics if the pointer is `null`. /// Undefined behavior if the pointer is invalid. #[inline] - pub fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> Py + pub unsafe fn from_owned_ptr_or_panic(ptr: *mut ffi::PyObject) -> Py { if ptr.is_null() { ::err::panic_after_error(); } else { - unsafe{ Py::from_owned_ptr(ptr) } + Py::from_owned_ptr(ptr) } } @@ -125,12 +125,12 @@ impl Py { /// returns a new reference (owned pointer). /// Returns `Err(PyErr)` if the pointer is `null`. /// Unsafe because the pointer might be invalid. - pub fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult> + pub unsafe fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult> { if ptr.is_null() { Err(PyErr::fetch(py)) } else { - Ok(unsafe{ Py::from_owned_ptr(ptr) }) + Ok(Py::from_owned_ptr(ptr)) } } diff --git a/src/object.rs b/src/object.rs index feb2784e..59f00dfc 100644 --- a/src/object.rs +++ b/src/object.rs @@ -48,28 +48,25 @@ impl PyObject { /// Construct `PyObject` from the result of a Python FFI call that /// returns a new reference (owned pointer). /// Returns `Err(PyErr)` if the pointer is `null`. - pub fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult + pub unsafe fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) + -> PyResult { if ptr.is_null() { Err(PyErr::fetch(py)) } else { - Ok(unsafe{ - PyObject::from_owned_ptr(py, ptr) - }) + Ok(PyObject::from_owned_ptr(py, ptr)) } } /// Construct `PyObject` from the result of a Python FFI call that /// returns a new reference (owned pointer). /// Returns `None` if the pointer is `null`. - pub fn from_owned_ptr_or_opt(py: Python, ptr: *mut ffi::PyObject) -> Option + pub unsafe fn from_owned_ptr_or_opt(py: Python, ptr: *mut ffi::PyObject) -> Option { if ptr.is_null() { None } else { - Some(unsafe{ - PyObject::from_owned_ptr(py, ptr) - }) + Some(PyObject::from_owned_ptr(py, ptr)) } } @@ -87,24 +84,26 @@ impl PyObject { /// Creates a `PyObject` instance for the given Python FFI pointer. /// Calls Py_INCREF() on the ptr. /// Returns `Err(PyErr)` if the pointer is `null`. - pub fn from_borrowed_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult + pub unsafe fn from_borrowed_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) + -> PyResult { if ptr.is_null() { Err(PyErr::fetch(py)) } else { - Ok(unsafe{PyObject::from_borrowed_ptr(py, ptr)}) + Ok(PyObject::from_borrowed_ptr(py, ptr)) } } /// Creates a `PyObject` instance for the given Python FFI pointer. /// Calls Py_INCREF() on the ptr. /// Returns `None` if the pointer is `null`. - pub fn from_borrowed_ptr_or_opt(py: Python, ptr: *mut ffi::PyObject) -> Option + pub unsafe fn from_borrowed_ptr_or_opt(py: Python, ptr: *mut ffi::PyObject) + -> Option { if ptr.is_null() { None } else { - Some(unsafe{PyObject::from_borrowed_ptr(py, ptr)}) + Some(PyObject::from_borrowed_ptr(py, ptr)) } }