Wrapping Python - A function wrapping template

Kirk McDonald kirklin.mcdonald at gmail.com
Wed Jun 14 17:08:31 PDT 2006


Kirk McDonald wrote:
> In wrapping Python with D, I have just made a large step:
> 

I've just done two things with regard to this wrapper:

First, I've implemented some basic exception wrapping. If a wrapped 
Python API function raises a Python exception, this is now captured and 
thrown as a D exception. If this exception is not caught by user code, 
it is caught by the function wrapping template, and translated back into 
the original Python exception. If a D exception is thrown and not caught 
by user code, the function wrapping template will translate it into a 
Python RuntimeError and pass along the message.

On the "to do" list is creating a hierarchy of D exception classes that 
mirrors the hierarchy of Python exceptions, as at the moment there is a 
single "PythonException" class. It might also be nice to provide some 
Python exceptions that mirror various D exceptions, for when those 
escape into the interpreter. (Alternately, one might map them onto an 
appropriate existing Python exception.)

Second, the function wrapper now supports void return types. Imagine my 
suprise when I realised it didn't support them before! (It was sort of a 
"duh" moment.) Adding this support roughly doubled the length of the 
template, which wasn't short to begin with. Each number of arguments the 
wrapped function can accept (from 0 to 10) has its own "static if" 
clause, and now each of these has been doubled, as functions with return 
types to capture must be called differently than those without. Anyway, 
functions with a void return type now return Py_None as per the usual 
Python idiom.

-Kirk McDonald



More information about the Digitalmars-d mailing list