Wrapping Python - A function wrapping template
Daniel Keep
daniel.keep.lists at gmail.com
Tue Jun 13 13:23:13 PDT 2006
Kirk McDonald wrote:
> In wrapping Python with D, I have just made a large step:
>
> [testdll.d]
> import python;
> import pyd.pyd; // My "Python/D" package
>
> int str_len(char[] str) {
> return str.length;
> }
>
> extern (C) {
>
> PyMethodDef testdll_GlobalMethods[] = [
> { "str_len", cast(PyCFunction)&func_wrap!(str_len).func,
> METH_VARARGS, "" },
> { null, null, 0, null }
> ];
>
> export void inittestdll() {
> _loadPythonSupport();
>
> PyObject* m = Py_InitModule("testdll", testdll_GlobalMethods);
> }
>
> } /* end extern (C) */
> // EOF
>
> (In a Python session:)
>>>> import testdll
>>>> testdll.str_len("This")
> 4
>
> Note the "func_wrap" template. This takes a regular D function and wraps
> it with something that can be exposed to Python. There are some
> limitations, as yet: The arguments and return type of the function must
> be ones it knows how to convert to and from Python, and there is not yet
> a mechanism for adding types to this list. It also does not yet support
> default arguments. It would also be nice to wrap all of the business
> with the method definition array. This can all be done.
>
> I'd like to acknowledge the work of David Rushby, whose "celerid"
> project and updated Python header were invaluable, as well as Deja
> Augustine, who first took a crack at the header.
>
> Daniel Keep and Tom S also proivded some useful function
> property-deriving templates.
>
> This library, as it exists now, is more a proof-of-concept than a useful
> library, so I'm not quite prepared to release it yet.
>
> -Kirk McDonald
You stole my idea :P
Seriously, congratulations. I know I didn't quite get that far. This
is fantastic stuff; it won't be long now before we have our own version
of Boost::Python :)
Just a few questions:
* How did you deal with the whole 8-bit with encodings <==> UTF-8 thing?
I was seriously considering just forcing everything to go via UTF-16,
or did you write code to do the transcoding manually?
* Do you have any plans on how to wrap the method definition array? I
tried using nested templates but D didn't like it :(
In any case, fantastic work.
-- Daniel
--
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list