Pyd

Don Clugston dac at nospam.com.au
Thu Jun 29 02:19:05 PDT 2006


Kirk McDonald wrote:
> 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

Great stuff, though I have very little experience with Python.
It's fascinating to me to see how many similarities there are between 
the front ends of Pyd and DDL. When I finish updating my meta.nameof 
module, I'll return to improving the DDL interface for class loading. 
Since I last worked on it, many DMD bugs have been fixed, and we now 
have IFTI. Some of the techniques will be transferable to Pyd.
Certainly it would AT LEAST be possible to do:

 >     auto Foo_ = wrap_class!(Foo)();
 >     Foo_.init!(ctor!(int), ctor!(int, int)); // could almost 
certainly improve this, too.
 >     Foo_.def!(Foo.foo);
 >     finalize_class!(Foo);

It may be possible to come up with a generic dynamic linking front-end 
which can be reused for a huge variety of programming languages; that's 
an exiting concept.



More information about the Digitalmars-d mailing list