Thread overview
Pyd: Stackthreads and returning arrays from D
Jun 26, 2007
Simen Haugen
Jun 26, 2007
Lars Ivar Igesund
Jun 26, 2007
Kirk McDonald
Jun 27, 2007
Simen Haugen
Jun 27, 2007
Frits van Bommel
Jun 27, 2007
Simen Haugen
Jun 27, 2007
Kirk McDonald
Jun 27, 2007
Kirk McDonald
June 26, 2007
Pyd doesn't work with stackthreads in newers versions of the dmd compiler (1.014+ at least). As I understand from the documentation and my testing, this means I cannot return arrays from D to Python (and renders Pyd quite useless for me).

"Pyd uses StackThreads for its iteration wrapping support. By setting this to False, opApply wrapping, Iter, and AltIter will be unavailable."

Has anyone found a simple workaround for this?


June 26, 2007
Simen Haugen wrote:

> Pyd doesn't work with stackthreads in newers versions of the dmd compiler (1.014+ at least). As I understand from the documentation and my testing, this means I cannot return arrays from D to Python (and renders Pyd quite useless for me).
> 
> "Pyd uses StackThreads for its iteration wrapping support. By setting this to False, opApply wrapping, Iter, and AltIter will be unavailable."
> 
> Has anyone found a simple workaround for this?

Whether it is a simple workaround or not, the stackthreads work fine with later DMD's in the Fiber reincarnation in Tango.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango
June 26, 2007
Simen Haugen wrote:
> Pyd doesn't work with stackthreads in newers versions of the dmd compiler (1.014+ at least). As I understand from the documentation and my testing, this means I cannot return arrays from D to Python (and renders Pyd quite useless for me).
> 
> "Pyd uses StackThreads for its iteration wrapping support. By setting this to False, opApply wrapping, Iter, and AltIter will be unavailable."
> 
> Has anyone found a simple workaround for this?
> 
> 

No, StackThreads has nothing to do with arrays. It is strictly for wrapping opApply overloads in classes. Currently, Pyd will convert D arrays to Python lists (except for char[] and wchar[], which are converted to the Python str and unicode types, respectively). Because this is a less-than-optimal solution in a number of ways, I plan on eventually writing a Python type that wraps D arrays more directly.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org
June 27, 2007
Ok, I just cannot get it to work..
Look at this simple example:

import pyd.pyd;


class Item {
}


class Container {
    private:
    Item[] _items;

    public:
    Item[] items() {
        return _items;
    }

    void items(Item[] items) {
        _items = items;
    }
}


extern(C) void PydMain() {
    module_init();

    wrap_class!(Item);

    wrap_class!(
        Container,
        Property!(Container.items)
    );
}

When trying to compile this I get an error "expected to return a value of type Item[]" for the Container.items property:

running build
running build_ext
building 'test' extension
sources:  ['test.d', 'python.d', 'class_wrap.d', 'ctor_wrap.d', 'def.d',
'dg_con
vert.d', 'exception.d', 'func_wrap.d', 'lib_abstract.d', 'make_object.d',
'make_
wrapper.d', 'op_wrap.d', 'pyd.d', 'pydobject.d', 'struct_wrap.d',
'pydmain.d', '
Default.d', 'Demangle.d', 'Nameof.d', 'Util.d',
'python_dll_windows_boilerplate.
d']
c:\d\dmd\bin\dmd.exe -c -version=Python_2_4_Or_Later -version=Python_Unicode_UCS
2 -debug -IC:\Python24\lib\site-packages\celerid\infrastructure\python\2.4 -IC:\
Python24\lib\site-packages\celerid\infrastructure -ofbuild\temp.win32-2.4\Releas
e\project\test.obj test.d
wrap_class: Item
class wrapper : T {
    mixin OverloadShim;

}

wrap_class: Container
class wrapper : T {
    mixin OverloadShim;
    alias Params[0] __pyd_p0;
    ReturnType!(__pyd_p0.get_t) items() {
        return __pyd_get_overload!("items", __pyd_p0.get_t).func("items");
    }
    ReturnType!(__pyd_p0.set_t) items(ParameterTypeTuple!(__pyd_p0.set_t) t)
{
        return __pyd_get_overload!("items", __pyd_p0.set_t).func("items",
t);
    }

}

C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(228):
fun
ction pyd.make_object.d_type!(Item[]).d_type expected to return a value of
type
Item[]
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(289):
tem
plate instance pyd.make_object.d_type!(Item[]) error instantiating
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(274):
templ
ate instance pyd.func_wrap.PydWrappedFunc.fn!(Item[]) error instantiating
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(264):
templ
ate instance pyd.func_wrap._pycallable_asdgT!(Item[] delegate(())) error
instant
iating
error: command 'c:\d\dmd\bin\dmd.exe' failed with exit status 1


When I used stackthreads I had no problem returning array of classes.



"Kirk McDonald" <kirklin.mcdonald@gmail.com> wrote in message news:f5rsov$apc$1@digitalmars.com...
> Simen Haugen wrote:
>> Pyd doesn't work with stackthreads in newers versions of the dmd compiler (1.014+ at least). As I understand from the documentation and my testing, this means I cannot return arrays from D to Python (and renders Pyd quite useless for me).
>>
>> "Pyd uses StackThreads for its iteration wrapping support. By setting this to False, opApply wrapping, Iter, and AltIter will be unavailable."
>>
>> Has anyone found a simple workaround for this?
>>
>>
>
> No, StackThreads has nothing to do with arrays. It is strictly for wrapping opApply overloads in classes. Currently, Pyd will convert D arrays to Python lists (except for char[] and wchar[], which are converted to the Python str and unicode types, respectively). Because this is a less-than-optimal solution in a number of ways, I plan on eventually writing a Python type that wraps D arrays more directly.
>
> -- 
> Kirk McDonald
> http://kirkmcdonald.blogspot.com
> Pyd: Connecting D and Python
> http://pyd.dsource.org


June 27, 2007
Simen Haugen wrote:
> Ok, I just cannot get it to work..
> Look at this simple example:
> 
> import pyd.pyd;
> 
> 
> class Item {
> }
> 
> 
> class Container {
>     private:
>     Item[] _items;
> 
>     public:
>     Item[] items() {
>         return _items;
>     }
> 
>     void items(Item[] items) {
>         _items = items;
>     }

Try:
     Item[] items(Item[] items) {
         return _items = items;
     }

> }
> 
> 
> extern(C) void PydMain() {
>     module_init();
> 
>     wrap_class!(Item);
> 
>     wrap_class!(
>         Container,
>         Property!(Container.items)
>     );
> }
> 
> When trying to compile this I get an error "expected to return a value of type Item[]" for the Container.items property:
[snip]
> When I used stackthreads I had no problem returning array of classes.

I think the error message was for the second method (the setter) which *didn't* return an Item[] as was expected.
So if I'm correct your only problem with returning an array of classes was forgetting to do so :P (in the setter).
June 27, 2007
Thats not the problem. If I change the test to use a getter method instead, the same error occurs.

import pyd.pyd;

class Item {
}

class Container {
    private:
    Item[] _items;

    public:
    Item[] getItems() {
        return _items;
    }
}

extern(C) void PydMain() {
    module_init();

    wrap_class!(Item);

    wrap_class!(
        Container,
        Def!(Container.getItems)
    );
}


The same error:
running build
running build_ext
building 'test' extension
sources:  ['test.d', 'python.d', 'class_wrap.d', 'ctor_wrap.d', 'def.d',
'dg_con
vert.d', 'exception.d', 'func_wrap.d', 'lib_abstract.d', 'make_object.d',
'make_
wrapper.d', 'op_wrap.d', 'pyd.d', 'pydobject.d', 'struct_wrap.d',
'pydmain.d', '
Default.d', 'Demangle.d', 'Nameof.d', 'Util.d',
'python_dll_windows_boilerplate.
d']
c:\d\dmd\bin\dmd.exe -c -version=Python_2_4_Or_Later -version=Python_Unicode_UCS
2 -debug -IC:\Python24\lib\site-packages\celerid\infrastructure\python\2.4 -IC:\
Python24\lib\site-packages\celerid\infrastructure -ofbuild\temp.win32-2.4\Releas
e\project\test.obj test.d
wrap_class: Item
class wrapper : T {
    mixin OverloadShim;

}

wrap_class: Container
class wrapper : T {
    mixin OverloadShim;
    alias Params[0] __pyd_p0;
    ReturnType!(__pyd_p0.func_t)
getItems(ParameterTypeTuple!(__pyd_p0.func_t) t
) {
        return __pyd_get_overload!("getItems",
__pyd_p0.func_t).func("getItems",
 t);
    }

}

C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(228):
fun
ction pyd.make_object.d_type!(Item[]).d_type expected to return a value of
type
Item[]
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(289):
tem
plate instance pyd.make_object.d_type!(Item[]) error instantiating
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(274):
templ
ate instance pyd.func_wrap.PydWrappedFunc.fn!(Item[]) error instantiating
C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(264):
templ
ate instance pyd.func_wrap._pycallable_asdgT!(Item[] delegate(())) error
instant
iating
error: command 'c:\d\dmd\bin\dmd.exe' failed with exit status 1



"Frits van Bommel" <fvbommel@REMwOVExCAPSs.nl> wrote in message news:f5tar1$2u2p$1@digitalmars.com...
> Simen Haugen wrote:
>> Ok, I just cannot get it to work..
>> Look at this simple example:
>>
>> import pyd.pyd;
>>
>>
>> class Item {
>> }
>>
>>
>> class Container {
>>     private:
>>     Item[] _items;
>>
>>     public:
>>     Item[] items() {
>>         return _items;
>>     }
>>
>>     void items(Item[] items) {
>>         _items = items;
>>     }
>
> Try:
>      Item[] items(Item[] items) {
>          return _items = items;
>      }
>
>> }
>>
>>
>> extern(C) void PydMain() {
>>     module_init();
>>
>>     wrap_class!(Item);
>>
>>     wrap_class!(
>>         Container,
>>         Property!(Container.items)
>>     );
>> }
>>
>> When trying to compile this I get an error "expected to return a value of type Item[]" for the Container.items property:
> [snip]
>> When I used stackthreads I had no problem returning array of classes.
>
> I think the error message was for the second method (the setter) which
> *didn't* return an Item[] as was expected.
> So if I'm correct your only problem with returning an array of classes was
> forgetting to do so :P (in the setter).


June 27, 2007
Simen Haugen wrote:
> Ok, I just cannot get it to work..
> Look at this simple example:
> 
> import pyd.pyd;
> 
> 
> class Item {
> }
> 
> 
> class Container {
>     private:
>     Item[] _items;
> 
>     public:
>     Item[] items() {
>         return _items;
>     }
> 
>     void items(Item[] items) {
>         _items = items;
>     }
> }
> 
> 
> extern(C) void PydMain() {
>     module_init();
> 
>     wrap_class!(Item);
> 
>     wrap_class!(
>         Container,
>         Property!(Container.items)
>     );
> }
> 
> When trying to compile this I get an error "expected to return a value of type Item[]" for the Container.items property:
> 
[snip] (Man, Pyd has verbose compilation output...)
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(228): fun
> ction pyd.make_object.d_type!(Item[]).d_type expected to return a value of type
> Item[]
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(289): tem
> plate instance pyd.make_object.d_type!(Item[]) error instantiating
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(274): templ
> ate instance pyd.func_wrap.PydWrappedFunc.fn!(Item[]) error instantiating
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(264): templ
> ate instance pyd.func_wrap._pycallable_asdgT!(Item[] delegate(())) error instant
> iating
> error: command 'c:\d\dmd\bin\dmd.exe' failed with exit status 1
> 

Careful reading of the type conversion docs
    http://pyd.dsource.org/conversion.html
will reveal that, while Pyd is capable of converting D arrays to Python lists, it is currently unable to perform the reverse operation (with the exception of char arrays, of course). This is obviously stupid, and getting Pyd's handling of arrays right has been bumped up on my priority list. (I'll probably set it up so that any Python iterable whose elements can be converted to the proper type can be converted to a D array.)

> 
> When I used stackthreads I had no problem returning array of classes.
> 
> 

Pyd can easily return an array. It is the reverse operation (wrapping functions with array parameters) which causes problems at the moment.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org
June 27, 2007
Simen Haugen wrote:
> Thats not the problem. If I change the test to use a getter method instead, the same error occurs.
> 
> import pyd.pyd;
> 
> class Item {
> }
> 
> class Container {
>     private:
>     Item[] _items;
> 
>     public:
>     Item[] getItems() {
>         return _items;
>     }
> }
> 
> extern(C) void PydMain() {
>     module_init();
> 
>     wrap_class!(Item);
> 
>     wrap_class!(
>         Container,
>         Def!(Container.getItems)
>     );
> }
> 
> 
> The same error:
[snip]
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(228): fun
> ction pyd.make_object.d_type!(Item[]).d_type expected to return a value of type
> Item[]
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\make_object.d(289): tem
> plate instance pyd.make_object.d_type!(Item[]) error instantiating
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(274): templ
> ate instance pyd.func_wrap.PydWrappedFunc.fn!(Item[]) error instantiating
> C:\Python24\lib\site-packages\celerid\infrastructure\pyd\func_wrap.d(264): templ
> ate instance pyd.func_wrap._pycallable_asdgT!(Item[] delegate(())) error instant
> iating
> error: command 'c:\d\dmd\bin\dmd.exe' failed with exit status 1
> 

Now /this/ is strange to me. However, it is extraordinarily late, and I am far too tired to come up with a cognizant answer at the moment.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org