pyd.make_object

This module contains some useful type conversion functions. There are two interesting operations involved here:

PyObject* -> D type

D type -> PyObject*

The former is handled by d_type, the latter by _py. The py function is provided as a convenience to directly convert a D type into an instance of DPyObject.

template _py (T)


PyObject* _py (T t);
Returns a new (owned) reference to a Python object based on the passed argument. If the passed argument is a PyObject*, this "steals" the reference. (In other words, it returns the PyObject* without changing its reference count.) If the passed argument is a DPyObject, this returns a new reference to whatever the DPyObject holds a reference to.

If the passed argument can't be converted to a PyObject, a Python RuntimeError will be raised and this function will return null.

template py (T)


DPyObject py (T t);
Constructs an object based on the type of the argument passed in.

For example, calling py (10) would return a DPyObject holding the value 10.

Calling this with a DPyObject will return back a reference to the very same DPyObject.

Calling this with a PyObject* will "steal" the reference.

class DPyConversionException : object.Exception;
An exception class used by d_type.

template d_type (T)


T d_type (PyObject* o);
This converts a PyObject* to a D type. The template argument is the type to convert to. The function argument is the PyObject* to convert. For instance:

PyObject* i = PyInt_FromLong(20);
int n = d_type!(int)(i);
assert(n == 20);


This throws a DPyConversionException if the PyObject can't be converted to the given D type.

Page was generated with on Sat Jul 1 22:14:02 2006