autowrap icon indicating copy to clipboard operation
autowrap copied to clipboard

Can't wrap functions that return pointers to structs that can't be wrapped

Open atilaneves opened this issue 5 years ago • 11 comments

struct Uncopiable {
    @disable this(this);
    double x;
}

export auto uncopiablePtr(double d = 33.3) {
    return new Uncopiable(d);
}

Calling this from python:

assert uncopiable_ptr(33.3).d == 33.3

Yields:

RuntimeError: D conversion function d_to_python failed with type issues.Uncopiable*

Uncopiable isn't wrappable due to not being copiable, and as such it's not registered.

atilaneves avatar Dec 18 '18 10:12 atilaneves

Possibly linked to #44

atilaneves avatar Dec 18 '18 11:12 atilaneves

Okay so you specifically disable registering uncopyable structs because I asked you to so we could have the thing compiling. That's no longer necessary and I deleted the check on my branch. You just need to avoid registering methods that cause problems like opAssign and possibly constructors. Also it failed for dumb reasons because it got confused by the __ fields. Just don't try to wrap those - see my branch.

Laeeth avatar Dec 19 '18 00:12 Laeeth

A pointer to a noncopyable struct should be fine - in fact if you can't copy it deal with a refcounted pointer to it. Pyd does ref counting automatically. Y

Laeeth avatar Dec 19 '18 00:12 Laeeth

Maybe autowrap should have a way to have a type registered by reference. A raw pointer is hairy - who knows who owns it or where it came from.

Laeeth avatar Dec 19 '18 00:12 Laeeth

You just need to avoid registering methods that cause problems like opAssign

This is done since yesterday.

possibly constructors

No, otherwise it wouldn't be possible to create objects from Python.

Also it failed for dumb reasons because it got confused by the __ fields

Fixed yesterday.

A pointer to a noncopyable struct should be fine

It should. However, pyd doesn't accept non-copyable structs so we can't register it.

Maybe autowrap should have a way to have a type registered by reference

pyd doesn't have a way to register by reference that I know of, so there's not much autowrap can do.

A raw pointer is hairy - who knows who owns it or where it came from.

Unless stated otherwise, it's GC allocated and so ok. If it's not GC allocated it probably shouldn't be a raw pointer.

atilaneves avatar Dec 19 '18 08:12 atilaneves

Can we wrap non-copyable structs as private members in classes and use std.typecons.Proxy to forward everything? Or will pyd still choke on the non-copyable private member?

John-Colvin avatar Dec 19 '18 09:12 John-Colvin

@John-Colvin I'd have to try, I don't know. And why std.typecons.Proxy instead of alias this?

atilaneves avatar Dec 19 '18 09:12 atilaneves

Not all objects need to be created from python btw. Sometimes you just need to be able to handle the result of calling D.

Laeeth avatar Dec 20 '18 03:12 Laeeth

You can register a Wrapper!T where it's beneath that a pointer and give Python access to same fields and methods (using getters and setters if fields not possible) as underlying type has.

Laeeth avatar Dec 20 '18 03:12 Laeeth

Does pyd know about alias this?

Laeeth avatar Dec 20 '18 03:12 Laeeth

I won't know what works in pyd until I try.

atilaneves avatar Dec 20 '18 10:12 atilaneves