Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Vlad-Shcherbina opened this issue Feb 11, 2018 · 2 comments
Closed

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

Vlad-Shcherbina opened this issue Feb 11, 2018 · 2 comments

Comments

@Vlad-Shcherbina
Copy link
Contributor

extern crate pyo3;

use pyo3::{Python, ObjectProtocol};

fn main() {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let a = py.eval("42", None, None).unwrap();
    a.call_method0("__str__").unwrap();  // ok
    a.call_method0("nonexistent_method").unwrap();  // crashes
}

Ideally should produce AttributeError or something.


This happens because in

pyo3/src/objectprotocol.rs

Lines 329 to 340 in 30baa99

fn call_method0(&self, name: &str) -> PyResult<&PyObjectRef>
{
name.with_borrowed_ptr(self.py(), |name| unsafe {
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
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()));
ffi::Py_DECREF(ptr);
self.py().xdecref(args);
result
})
}
and several similar places, ptr could be NULL.

@fafhrd91
Copy link
Contributor

we should return PyErr if ptr is null,

https://github.com/PyO3/pyo3/blob/master/src/err.rs#L239

@fafhrd91
Copy link
Contributor

fixed in master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants