Fix segfault on calling unknown method

This commit is contained in:
ijl 2018-09-24 02:49:52 +00:00
parent 7047af2873
commit 868e28d5ad
2 changed files with 27 additions and 5 deletions

View File

@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Added
* `#[pyclass]` objects can now be returned from rust functions
* `PyComplex` by kngwyu in [#226](https://github.com/PyO3/pyo3/pull/226)
@ -28,10 +28,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Added an explenation that the GIL can temporarily be released even while holding a GILGuard.
* Lots of clippy errors
* Fix segfault on calling an unknown method on a PyObject
### Removed
* The pyobject_extract macro
* The pyobject_extract macro
## [0.4.1] - 2018-08-20
@ -102,7 +103,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.2.5] - 2018-02-21
### Added
### Added
* CPython 3.7 support
@ -121,7 +122,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Drop `RefFromPyObject` trait
* Add Python::register_any() method
### Fixed
### Fixed
* Fix impl `FromPyObject` for `Py<T>`
* Mark method that work with raw pointers as unsafe #95
@ -160,7 +161,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Allow to add gc support without implementing PyGCProtocol #57
* Refactor `PyErr` implementation. Drop `py` parameter from constructor.
### Added
### Added
* Added inheritance support #15
* Added weakref support #56

View File

@ -213,6 +213,9 @@ impl PyObject {
let args = args.into_tuple(py).into_ptr();
let kwargs = kwargs.into_ptr();
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(py));
}
let result = PyObject::from_owned_ptr_or_err(py, ffi::PyObject_Call(ptr, args, kwargs));
ffi::Py_DECREF(ptr);
ffi::Py_XDECREF(args);
@ -322,3 +325,21 @@ impl Drop for PyObject {
}
}
}
#[cfg(test)]
mod test {
use super::PyObject;
use python::Python;
use types::PyDict;
#[test]
fn test_call_for_non_existing_method() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj: PyObject = PyDict::new(py).into();
assert!(obj.call_method0(py, "asdf").is_err());
assert!(obj.call_method(py, "nonexistent_method", (1,), None).is_err());
assert!(obj.call_method0(py, "nonexistent_method").is_err());
assert!(obj.call_method1(py, "nonexistent_method", (1,)).is_err());
}
}