Michele De Stefano's C++ Utilities
mds_utils::python::Obj Class Reference

This is a simple wrapper around the PyObject* datatype. More...

#include <mds_utils/python/obj.hpp>

Inheritance diagram for mds_utils::python::Obj:
boost::numeric::ublas::NumPy1DArray< T > mds_utils::python::Dictionary mds_utils::python::FileObj mds_utils::python::iFileObj mds_utils::python::oFileObj mds_utils::python::Sequence_Base< Derived > mds_utils::python::Sequence_Base< List > mds_utils::python::Sequence_Base< Sequence > mds_utils::python::Sequence_Base< Tuple >

Classes

class  ProxyAttr
 Proxy class for managing attribute access. More...
 

Public Member Functions

 Obj ()
 Default constructor.
 
 Obj (const Obj &rhs)
 The copy-constructor. More...
 
 Obj (Obj &&rhs)
 Move constructor. More...
 
 Obj (ProxyAttr &&rhs)
 Move constructor from ProxyAttr objects. More...
 
 Obj (PyObject *po)
 Construct from a Python object. More...
 
Objoperator= (const Obj &rhs)
 Standard assignment.
 
Objoperator= (Obj &&rhs)
 Move assignment.
 
virtual Objoperator= (PyObject *po)
 Assignment from a Python object. More...
 
template<class T >
Objoperator= (const T &val)
 Assignment from a value. More...
 
virtual ~Obj ()
 Destructor.
 
PyObject * getPyObject () const
 Returns the underlying PyObject. More...
 
 operator PyObject * () const
 Automatic type conversion towards a Python object. More...
 
virtual PyObject * transfer ()
 Returns the Python object with transferred ownership. More...
 
virtual void incref ()
 Increments the reference count using Py_XINCREF. More...
 
virtual void decref ()
 Decrements the reference count using Py_XDECREF.
 
void reset ()
 Resets the object to the state given by the default constructor.
 
void get_ownership ()
 Used in place of incref, when the wrapped PyObject* was increfed already.
 
bool has_attr (const std::string &name) const
 Tests if the object has a particular attribute.
 
ProxyAttr attr (const std::string &name)
 Retrieves an attribute. More...
 
bool is_callable () const
 Checks if the object is callable. More...
 
Obj operator() ()
 Calling operator. More...
 
Obj operator() (const Obj &args)
 Calling operator, with positional arguments. More...
 
Obj operator() (const Obj &args, const Obj &kw)
 Calling operator, with positional and keyword arguments. More...
 

Protected Member Functions

void check_callable ()
 Checks if the object is callable. More...
 
void check_call_result (PyObject *result)
 Checks if the result of the object call was successful. More...
 

Protected Attributes

size_t decref_on_destroy
 
PyObject * m_po
 Underlying pointer to the wrapped Python object.
 

Detailed Description

This is a simple wrapper around the PyObject* datatype.

It automatically manages reference counting when needed and offers easy access to methods and attributes. Reference counting can be handled explicitly through the provided methods. The destructor automatically knows when it has to decrement the reference count. When not stated otherwise, the default behavior is to increment the reference count of the object.

Warning
Remember that the copy/move constructors and the assignement operators do not duplicate the underlying Python object. They only increment its reference count.
Author
Michele De Stefano
Date
24/09/2014

Definition at line 68 of file obj.hpp.

Constructor & Destructor Documentation

mds_utils::python::Obj::Obj ( const Obj rhs)
inline

The copy-constructor.

It increfs the current object.

Parameters
rhsThe right-hand-side of the copy.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 210 of file obj.hpp.

References incref().

mds_utils::python::Obj::Obj ( Obj &&  rhs)
inline

Move constructor.

Parameters
rhsThe right-hand-side of the move.
Author
Michele De Stefano
Date
28/05/2014

Definition at line 222 of file obj.hpp.

References get_ownership().

mds_utils::python::Obj::Obj ( ProxyAttr &&  rhs)
inline

Move constructor from ProxyAttr objects.

Parameters
rhsA ProxyAttr object.
Warning
It increfs the current object.
Author
Michele De Stefano
Date
28/05/2014

Definition at line 237 of file obj.hpp.

References incref().

mds_utils::python::Obj::Obj ( PyObject *  po)
inline

Construct from a Python object.

Parameters
poPointer to the Python object.
Warning
It does not incref the po pointer. You have to do it manually, if needed, on the Obj instance. In Python terminology, the reference to po is borrowed.
Author
Michele De Stefano
Date
08/04/2014

Definition at line 253 of file obj.hpp.

Member Function Documentation

ProxyAttr mds_utils::python::Obj::attr ( const std::string &  name)
inline

Retrieves an attribute.

If the attribute is not found, it throws an exception.

Parameters
nameThe attribute name.
Returns
The attribute (already increfed).
Remarks
If we want to set a new attribute, and the attribute does not exist, it is automatically created.
Author
Michele De Stefano
Date
28/03/2014

Definition at line 407 of file obj.hpp.

Referenced by mds_utils::python::ublas::get(), and mds_utils::python::ublas::to_python().

void mds_utils::python::Obj::check_call_result ( PyObject *  result)
inlineprotected

Checks if the result of the object call was successful.

Throws an exception if the call was unsuccessful.

Author
Michele De Stefano
Date
08/04/2014

Definition at line 189 of file obj.hpp.

Referenced by operator()().

void mds_utils::python::Obj::check_callable ( )
inlineprotected

Checks if the object is callable.

Throws an exception if the object is not callable.

Author
Michele De Stefano
Date
08/04/2014

Definition at line 175 of file obj.hpp.

References is_callable().

Referenced by operator()().

virtual void mds_utils::python::Obj::incref ( )
inlinevirtual

Increments the reference count using Py_XINCREF.

Author
Michele De Stefano
Date
26/03/2014

Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.

Definition at line 363 of file obj.hpp.

References decref_on_destroy.

Referenced by mds_utils::python::Dictionary::get_item(), Obj(), operator=(), and mds_utils::python::Sequence_Base< List >::set().

bool mds_utils::python::Obj::is_callable ( ) const
inline

Checks if the object is callable.

Returns
true if the object is callable; false otherwise.
Author
Michele De Stefano
Date
02/04/2014

Definition at line 420 of file obj.hpp.

Referenced by check_callable().

mds_utils::python::Obj::operator PyObject * ( ) const
inline

Automatic type conversion towards a Python object.

It does not transfer ownership.

Author
Michele De Stefano
Date
14/04/2014

Definition at line 338 of file obj.hpp.

References m_po.

Obj mds_utils::python::Obj::operator() ( )
inline

Calling operator.

Returns
The result of the call, not increfed.
Warning
It throws an exception if the object is not callable.
Author
Michele De Stefano
Date
15/04/2014

Definition at line 435 of file obj.hpp.

References check_call_result(), and check_callable().

Obj mds_utils::python::Obj::operator() ( const Obj args)
inline

Calling operator, with positional arguments.

Parameters
argsTuple of positional arguments. Setting args to the default-constructed Tuple will produce the same effect as calling operator()().
Returns
The result of the call, not increfed.
Warning
It throws an exception if the object is not callable.
Author
Michele De Stefano
Date
22/05/2014

Definition at line 467 of file obj.hpp.

References check_call_result(), and check_callable().

Obj mds_utils::python::Obj::operator() ( const Obj args,
const Obj kw 
)
inline

Calling operator, with positional and keyword arguments.

Parameters
argsTuple of positional arguments. It cannot be default-constructed Obj (because it cannot wrap a NULL pointer). When positional arguments are not needed, pass an empty Tuple (i.e. a Tuple of length 0).
kwDictionary of keyworkd arguments. It can be the default constructed Dictionary (i.e. a Dictionary wrapping a NULL object) if keyword arguments are not needed.
Returns
The result of the call, not increfed.
Warning
It throws an exception if the object is not callable.
Author
Michele De Stefano
Date
22/05/2014

Definition at line 504 of file obj.hpp.

References check_call_result(), check_callable(), Obj(), and mds_utils::python::to_python().

virtual Obj& mds_utils::python::Obj::operator= ( PyObject *  po)
inlinevirtual

Assignment from a Python object.

Parameters
poPointer to the Python object.
Warning
It does not incref the po pointer. You have to do it manually, if needed, on the Obj instance. In Python terminology, the reference to po is borrowed.
Author
Michele De Stefano
Date
24/09/2014

Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.

Definition at line 285 of file obj.hpp.

References reset().

template<class T >
Obj& mds_utils::python::Obj::operator= ( const T &  val)
inline

Assignment from a value.

This method uses the mds_utils::python::to_python function. Providing the proper overload for mds_utils::python::to_python allows this function to properly work for any type.

Parameters
valThe value to assign.
Returns
The current object.
Author
Michele De Stefano
Date
26/03/2014

Definition at line 308 of file obj.hpp.

References get_ownership(), reset(), and mds_utils::python::to_python().

virtual PyObject* mds_utils::python::Obj::transfer ( )
inlinevirtual

Returns the Python object with transferred ownership.

Returns
The Python object.
Author
Michele De Stefano
Date
27/03/2014

Reimplemented in boost::numeric::ublas::NumPy1DArray< T >.

Definition at line 350 of file obj.hpp.

References reset().

Referenced by operator=(), and mds_utils::python::ublas::to_python().

Member Data Documentation

size_t mds_utils::python::Obj::decref_on_destroy
protected

Counts the number of times this object has to be decrefed on destroy.

Definition at line 84 of file obj.hpp.

Referenced by boost::numeric::ublas::NumPy1DArray< T >::decref(), decref(), get_ownership(), boost::numeric::ublas::NumPy1DArray< T >::incref(), and incref().


The documentation for this class was generated from the following file: