Struct cpython::PyObject [] [src]

#[repr(C)]
pub struct PyObject { /* fields omitted */ }

Represents a reference to a Python object.

Python objects are reference counted. Calling clone_ref() on a PyObject will return a new reference to the same object (thus incrementing the reference count). The Drop implementation will automatically decrement the reference count. You can also call release_ref() to explicitly decrement the reference count. This is slightly faster than relying on automatic drop, because release_ref does not need to check whether the GIL needs to be acquired.

PyObject can be used with all Python objects, since all python types derive from object. This crate also contains other, more specific types that serve as references to Python objects (e.g. PyTuple for Python tuples, etc.).

You can convert from any Python object to PyObject by calling as_object() or into_object() from the PythonObject trait. In the other direction, you can call cast_as() or cast_into() on PyObject to convert to more specific object types.

Most of the interesting methods are provided by the ObjectProtocol trait.

Methods

impl PyObject
[src]

[src]

Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Undefined behavior if the pointer is NULL or invalid.

[src]

Creates a PyObject instance for the given FFI pointer. Calls Py_INCREF() on the ptr. Undefined behavior if the pointer is NULL or invalid.

[src]

Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Returns None for null pointers; undefined behavior if the pointer is invalid.

[src]

Returns None for null pointers; undefined behavior if the pointer is invalid.

[src]

Gets the underlying FFI pointer. Returns a borrowed pointer.

[src]

Gets the underlying FFI pointer. Consumes self without calling Py_DECREF(), thus returning an owned pointer.

[src]

Transmutes an FFI pointer to &PyObject. Undefined behavior if the pointer is NULL or invalid.

[src]

Transmutes a slice of owned FFI pointers to &[PyObject]. Undefined behavior if any pointer in the slice is NULL or invalid.

[src]

Gets the reference count of this Python object.

[src]

Gets the Python type object for this object's type.

[src]

Casts the PyObject to a concrete Python object type. Causes undefined behavior if the object is not of the expected type. This is a wrapper function around PythonObject::unchecked_downcast_from().

[src]

Casts the PyObject to a concrete Python object type. Fails with PythonObjectDowncastError if the object is not of the expected type. This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_from().

[src]

Casts the PyObject to a concrete Python object type. Causes undefined behavior if the object is not of the expected type. This is a wrapper function around PythonObject::unchecked_downcast_borrow_from().

[src]

Casts the PyObject to a concrete Python object type. Fails with PythonObjectDowncastError if the object is not of the expected type. This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_borrow_from().

[src]

Extracts some type from the Python object. This is a wrapper function around FromPyObject::from_py_object().

Trait Implementations

impl ToPyObject for PyObject
[src]

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

[src]

Converts self into a Python object.

[src]

Converts self into a Python object. Read more

[src]

Converts self into a Python object and calls the specified closure on the native FFI pointer underlying the Python object. Read more

impl<'source> FromPyObject<'source> for PyObject
[src]

[src]

Extracts Self from the source PyObject.

impl<'source> FromPyObject<'source> for &'source PyObject
[src]

[src]

Extracts Self from the source PyObject.

impl Send for PyObject
[src]

impl Sync for PyObject
[src]

impl Drop for PyObject
[src]

Dropping a PyObject decrements the reference count on the object by 1.

[src]

Executes the destructor for this type. Read more

impl PythonObject for PyObject
[src]

[src]

Casts the Python object to PyObject.

[src]

Casts the Python object to PyObject.

[src]

Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type. Read more

[src]

Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type. Read more

impl PythonObjectWithCheckedDowncast for PyObject
[src]

[src]

Cast from PyObject to a concrete Python object type.

[src]

Cast from PyObject to a concrete Python object type.

impl PythonObjectWithTypeObject for PyObject
[src]

[src]

Retrieves the type object for this Python object type.

impl PartialEq for PyObject
[src]

PyObject implements the == operator using reference equality: obj1 == obj2 in rust is equivalent to obj1 is obj2 in Python.

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl Eq for PyObject
[src]

PyObject implements the == operator using reference equality: obj1 == obj2 in rust is equivalent to obj1 is obj2 in Python.

impl ObjectProtocol for PyObject
[src]

[src]

Determines whether this object has the given attribute. This is equivalent to the Python expression 'hasattr(self, attr_name)'. Read more

[src]

Retrieves an attribute value. This is equivalent to the Python expression 'self.attr_name'. Read more

[src]

Sets an attribute value. This is equivalent to the Python expression 'self.attr_name = value'. Read more

[src]

Deletes an attribute. This is equivalent to the Python expression 'del self.attr_name'. Read more

[src]

Compares two Python objects. Read more

[src]

Compares two Python objects. Read more

[src]

Compute the string representation of self. This is equivalent to the Python expression 'repr(self)'. Read more

[src]

Compute the string representation of self. This is equivalent to the Python expression 'str(self)'. Read more

[src]

Determines whether this object is callable.

[src]

Calls the object. This is equivalent to the Python expression: 'self(*args, **kwargs)' Read more

[src]

Calls a method on the object. This is equivalent to the Python expression: 'self.name(*args, **kwargs)' Read more

[src]

Retrieves the hash code of the object. This is equivalent to the Python expression: 'hash(self)' Read more

[src]

Returns whether the object is considered to be true. This is equivalent to the Python expression: 'not not self' Read more

[src]

Returns the length of the sequence or mapping. This is equivalent to the Python expression: 'len(self)' Read more

[src]

This is equivalent to the Python expression: 'self[key]'

[src]

Sets an item value. This is equivalent to the Python expression 'self[key] = value'. Read more

[src]

Deletes an item. This is equivalent to the Python expression 'del self[key]'. Read more

[src]

Takes an object and returns an iterator for it. This is typically a new iterator but if the argument is an iterator, this returns itself. Read more

impl Debug for PyObject
[src]

[src]

Formats the value using the given formatter.

impl Display for PyObject
[src]

[src]

Formats the value using the given formatter. Read more

impl BaseObject for PyObject
[src]

[src]

Gets the size of the object, in bytes.

[src]

Allocates a new object (usually by calling ty->tp_alloc), and initializes it using init_val. ty must be derived from the Self type, and the resulting object must be of type ty. Read more

[src]

Calls the rust destructor for the object and frees the memory (usually by calling ptr->ob_type->tp_free). This function is used as tp_dealloc implementation. Read more