Thread overview
PyD - accessing D class fields in Python
Mar 18, 2019
clothlen
Mar 19, 2019
Bastiaan Veelo
May 17, 2021
mw
May 17, 2021
Imperatorn
May 17, 2021
mw
May 18, 2021
mw
May 19, 2021
Imperatorn
March 18, 2019
Howdy;

I'm trying to extend my Python program with D, but I'm having trouble accessing a D class's field/attribute/property/something.

My D file looks like this:

```
module simpletest;
import pyd.pyd;
import std.stdio;

class ExampleTest{
	int _a = 3;
	int a(){
		return _a;
	}
	void a(int a){
		this._a = a;
	}
	int b = 4;
}

extern(C) void PydMain() {
	module_init();
	wrap_class!(
		ExampleTest,
		Property!(ExampleTest.a),
		Property!(ExampleTest.b)
		// Member!("b")
	);
}

```

and my Python file looks like this:

```
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
"""Run with `pipenv run pytest -s tests/test_simple_test.py`."""
import simpletest


def test_class():
	test = simpletest.ExampleTest()
	assert None is print(test.a)
	assert test.a == 3
	test.a = 2
	assert test.a == 2
	assert None is print(test.b)
```

The first field, `a`, I can read, but `attribute 'b' of 'simpletest.ExampleTest' objects is not readable`. `a` has a getter and setter function; do I have to make getters and setters for all fields I want to share with Python? That sounds like a lot of wasted space if so. The only examples at the Github (https://github.com/ariovistus/pyd/tree/master/examples) only seem to show it with getters and setters.
March 19, 2019
On Monday, 18 March 2019 at 22:25:10 UTC, clothlen wrote:
> Howdy;
>
> I'm trying to extend my Python program with D, but I'm having trouble accessing a D class's field/attribute/property/something.
>
> My D file looks like this:
>
> ```
> module simpletest;
> import pyd.pyd;
> import std.stdio;
>
> class ExampleTest{
> 	int _a = 3;
> 	int a(){
> 		return _a;
> 	}
> 	void a(int a){
> 		this._a = a;
> 	}
> 	int b = 4;
> }
>
> extern(C) void PydMain() {
> 	module_init();
> 	wrap_class!(
> 		ExampleTest,
> 		Property!(ExampleTest.a),
> 		Property!(ExampleTest.b)
> 		// Member!("b")

Maybe you need `Member!(“b”, “rw”)`?

> 	);
> }

If that doesn’t work, maybe you find help by filing an issue on PyD.

May 17, 2021
On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo wrote:
> On Monday, 18 March 2019 at 22:25:10 UTC, clothlen wrote:
>> Howdy;
>>
>> I'm trying to extend my Python program with D, but I'm having trouble accessing a D class's field/attribute/property/something.
>>
>> My D file looks like this:
>>
>> ```
>> module simpletest;
>> import pyd.pyd;
>> import std.stdio;
>>
>> class ExampleTest{
>> 	int _a = 3;
>> 	int a(){
>> 		return _a;
>> 	}
>> 	void a(int a){
>> 		this._a = a;
>> 	}
>> 	int b = 4;
>> }
>>
>> extern(C) void PydMain() {
>> 	module_init();
>> 	wrap_class!(
>> 		ExampleTest,
>> 		Property!(ExampleTest.a),
>> 		Property!(ExampleTest.b)
>> 		// Member!("b")
>
> Maybe you need `Member!(“b”, “rw”)`?
>
>> 	);
>> }
>
> If that doesn’t work, maybe you find help by filing an issue on PyD.

I filed the issue here (still no response yet):

https://github.com/ariovistus/pyd/issues/152


Just tried:
```
     Member!("m_i", Mode!"rw"),
```

Still the same error:

```
/usr/local/lib/python3.6/dist-packages/pyd/infrastructure/pyd/make_wrapper.d(127): Error: no property shim for type pyd.struct_wrap.Member!("m_i", Mode!"rw")
/usr/local/lib/python3.6/dist-packages/pyd/infrastructure/pyd/make_wrapper.d(137): Error: template instance pyd.make_wrapper.class_decls!(0u, Foo, Member!("m_i", Mode!"rw"), Init!int, Init!(int, int), Property!(i, Docstring!"A sample property of Foo."), BinaryOperatorX!("+", false, Guess), Def!(opSlice, PyName!"__iter__", Range function()), Def!(foo, Docstring!"A sample method of Foo."), Def!(a), Def!(b), Def!(c), Def!(d), Def!(e),
 Def!(f), Def!(g), Def!(h), Def!(j), Def!(k), Def!(l), Def!(m), Def!(n)) error instantiating
```


Anyone know how to expose D class data member to Python?


Thanks!
May 17, 2021
On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
> On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo wrote:
>> [...]
>
> I filed the issue here (still no response yet):
>
> https://github.com/ariovistus/pyd/issues/152
>
> [...]

https://github.com/symmetryinvestments/autowrap
May 17, 2021
On Monday, 17 May 2021 at 17:16:27 UTC, Imperatorn wrote:
> On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
>> On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo wrote:
>>> [...]
>>
>> I filed the issue here (still no response yet):
>>
>> https://github.com/ariovistus/pyd/issues/152
>>
>> [...]
>
> https://github.com/symmetryinvestments/autowrap

Thanks, I just tried, but still cannot get access to class field:

I build this example:

https://github.com/symmetryinvestments/autowrap/tree/master/examples/pyd

and test this:

https://github.com/symmetryinvestments/autowrap/blob/master/examples/pyd/source/class_wrap.d
class Bizzy {
    int _m;
    ...
}


```
$ dub build --config=python36

$ ln -s lib/pyd/libpydtests.so pyd.so

$ python3
>>> import pyd
>>> b = pyd.Bizzy(0)
>>> b
bye(0)
>>> b._m = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'pyd.Bizzy' object has no attribute '_m'
```

Add `public` does not work either:
```
class Bizzy {
    public int _m;
    ...
}

```

Any hints?

May 18, 2021
On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
> I filed the issue here (still no response yet):
>
> https://github.com/ariovistus/pyd/issues/152

It's fixed now: pyd version >= 0.14.1

May 19, 2021
On Tuesday, 18 May 2021 at 23:27:40 UTC, mw wrote:
> On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
>> I filed the issue here (still no response yet):
>>
>> https://github.com/ariovistus/pyd/issues/152
>
> It's fixed now: pyd version >= 0.14.1

👍