Pyd

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Jun 29 01:34:51 PDT 2006


Kirk McDonald wrote:
> http://dsource.org/projects/pyd/wiki
> 
> As of revision 16, Pyd is now capable of the following:
[snipped stuff about class wrapping]
> Important things left to do:
> * Support for constructors other than the default constructor. This is 
> not trivial. I know how I am going to do it, but it is going to involve 
> hammering out a bunch of code.

This is now done to some extent. There is currently one fairly serious 
limitation: You can't wrap two different constructors that take the same 
number of arguments. I can fix this eventually, but this is a good first 
step. Aside from this limitation, it works:

class Foo {
     int m_i;
     this() { }
     this(int i) { m_i = i; }
     this(int i, int j) { m_i = i + j; }
     void foo() {
         writefln("Foo.foo(): i = %s", m_i);
     }
}

extern (C)
export void inittestdll() {
     module_init("testdll");

     auto Foo_ = wrap_class!("Foo", Foo)();
     Foo_.init!(ctor!(int), ctor!(int, int));
     Foo_.def!("foo", Foo.foo);
     finalize_class!("Foo", Foo);
}

In Python:
 >>> a = testdll.Foo()
 >>> a.foo()
Foo.foo(): i = 0
 >>> b = testdll.Foo(10)
 >>> b.foo()
Foo.foo(): i = 10
 >>> c = testdll.Foo(12, 30)
 >>> c.foo()
Foo.foo(): i = 42

There are still some issues: Passing an unconvertable type (e.g. giving 
the ctor a string when it expects an int) appears to have dire 
consequences (Python crashes), and I'm still tracking the precise 
problem down. (It's undoubtedly an uncaught exception; Python reacts 
quite badly when those go uncaught.)

-Kirk McDonald



More information about the Digitalmars-d mailing list