call_method*() crashes when the method does not exist #113

This commit is contained in:
Nikolay Kim 2018-02-21 09:39:06 -08:00
parent b7a8d25338
commit 0b9557a245
3 changed files with 26 additions and 3 deletions

View File

@ -1,13 +1,15 @@
Changes
-------
0.2.5 (2018-xx-xx)
0.2.5 (2018-02-21)
^^^^^^^^^^^^^^^^^^
* Embedded CPython 3.7b1 crashes on initialization #110
* Generated extension functions are weakly typed #108
* call_method*() crashes when the method does not exist #113
0.2.4 (2018-01-19)
^^^^^^^^^^^^^^^^^^

View File

@ -1,6 +1,6 @@
[package]
name = "pyo3"
version = "0.2.4"
version = "0.2.5"
description = "Bindings to Python interpreter"
authors = ["PyO3 Project and Contributors"]
readme = "README.md"
@ -19,7 +19,7 @@ appveyor = { repository = "PyO3/pyo3" }
codecov = { repository = "PyO3/pyo3", branch = "master", service = "github" }
[dependencies]
log = "0.4"
log = "0.5"
libc = "0.2"
spin = "0.4.6"
num-traits = "0.1"

View File

@ -315,6 +315,9 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
{
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(self.py()))
}
let args = args.into_tuple(self.py()).into_ptr();
let kw_ptr = kwargs.into_dict_ptr(self.py());
let result = self.py().from_owned_ptr_or_err(
@ -330,6 +333,9 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
{
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(self.py()))
}
let args = PyTuple::empty(self.py()).into_ptr();
let result = self.py().from_owned_ptr_or_err(
ffi::PyObject_Call(ptr, args, std::ptr::null_mut()));
@ -343,6 +349,9 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
{
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
if ptr.is_null() {
return Err(PyErr::fetch(self.py()))
}
let args = args.into_tuple(self.py()).into_ptr();
let result = self.py().from_owned_ptr_or_err(
ffi::PyObject_Call(ptr, args, std::ptr::null_mut()));
@ -458,6 +467,7 @@ mod test {
use python::Python;
use conversion::{ToPyObject, PyTryFrom};
use objects::PyString;
use super::*;
#[test]
fn test_debug_string() {
@ -476,4 +486,15 @@ mod test {
let s = PyString::try_from(v.as_ref(py)).unwrap();
assert_eq!(format!("{}", s), "Hello\n");
}
#[test]
fn test_call_for_non_existing_method() {
let gil = Python::acquire_gil();
let py = gil.python();
let a = py.eval("42", None, None).unwrap();
a.call_method0("__str__").unwrap(); // ok
assert!(a.call_method("nonexistent_method", (1,), ()).is_err());
assert!(a.call_method0("nonexistent_method").is_err());
assert!(a.call_method1("nonexistent_method", (1,)).is_err());
}
}