Skip to content

add the capsule name to the py::capsule constructor #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 15, 2017

Conversation

phaustin
Copy link
Contributor

Here's a first attempt at adding a name to py::capsule

I've only changed the first constructor, because trying to add an optional name to the other two constructors produces an error message:

ValueError: PyCapsule_GetPointer called with incorrect name

if(ptr){
py::print("destructing capsule");
}
}));
auto name=PyCapsule_GetName(capsule.ptr());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide enough of the interface here that we can avoid hitting the internals, particularly because the test suite also serves as a source of examples. Let's add a simple const char *name() const { return PyCapsule_GetName(m_ptr); } method to get it.

auto name=PyCapsule_GetName(capsule.ptr());
py::print(name);
auto contents=PyCapsule_GetPointer(capsule.ptr(),name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here likewise you should be able to use the pybind API: void *contents = capsule;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed hopefully -- but the pytest is failing for some reason:

>       assert capture.unordered == """
            created capsule with name --pointer type description-- and contents 1234
            creating capsule
            destructing capsule
        """
E       assert --- actual / +++ expected
E           created capsule with name --pointer type description-- and contents 1234
E         + creating capsule
E           destructing capsule

not sure why it's unhappy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you removed the py::print("creating capsule"); but forgot to take that line out of the expected output in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I misunderstood the pytest output, added the line back in.

@@ -575,15 +575,18 @@ test_initializer python_types([](py::module &m) {

m.def("return_capsule_with_name_and_destructor_3",
[]() {
py::print("creating capsule");
auto capsule=py::object(py::capsule((void *) 1234, "pointer type description",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to wrap the capsule inside an object here? capsule is already an object subclass.

@jagerman
Copy link
Member

This looks good to me; any other comments?

@phaustin
Copy link
Contributor Author

Not directly related, but as long as I'm here, a note that my /bin/bash on OSX Sierra won't run tools/check-style.sh

$ ./tools/check-style.sh
./tools/check-style.sh: line 22: 3: ambiguous redirect
./tools/check-style.sh: line 23: read: 3: invalid file descriptor: Bad file descriptor
...

works fine on Ubuntu though.

best, Phil

@phaustin
Copy link
Contributor Author

I'll be putting together a Jupyter notebook to demonstrate how to use py::capsule and xtensor-python to pass functions to an ode integrator, will ping you when I post it to my class.

@jagerman
Copy link
Member

/bin/bash on OSX Sierra won't run tools/check-style.sh

Thanks for the report; that should be fixed by d080f83

@jagerman jagerman merged commit 13d8cd2 into pybind:master Jun 15, 2017
@jagerman
Copy link
Member

Merged; thanks for the PR!

@phaustin phaustin deleted the capsule branch June 15, 2017 14:55
@dean0x7d dean0x7d modified the milestone: v2.2 Aug 13, 2017
rwgk added a commit to rwgk/pybind11 that referenced this pull request Oct 12, 2022
Use `PyCapsule_Destructor` (part of the stable Python ABI) instead of spelling out the C `typedef`.

The deprecation message is misleading. Replace with a message pointing to another existing ctor.

Background: According to @wjacob the original motivation for deprecating the ctor (in PR pybind#752) was to hide Python C API details, but PR pybind#902 brought those back with a new ctor, it cannot be avoided. Having a `PyCapsule_Destructor` or a `void (*destructor)(void *)` are two separate and valid use cases.
rwgk added a commit to rwgk/pybind11 that referenced this pull request Oct 12, 2022
Use `PyCapsule_Destructor` (part of the stable Python ABI) instead of spelling out the C `typedef`.

The deprecation message is misleading. Replace with a message pointing to another existing ctor.

Background: According to @wjakob the original motivation for deprecating the ctor (in PR pybind#752) was to hide Python C API details, but PR pybind#902 brought those back with a new ctor, it cannot be avoided. Having a `PyCapsule_Destructor` or a `void (*destructor)(void *)` are two separate and valid use cases.
rwgk added a commit that referenced this pull request Oct 12, 2022
Use `PyCapsule_Destructor` (part of the stable Python ABI) instead of spelling out the C `typedef`.

The deprecation message is misleading. Replace with a message pointing to another existing ctor.

Background: According to @wjakob the original motivation for deprecating the ctor (in PR #752) was to hide Python C API details, but PR #902 brought those back with a new ctor, it cannot be avoided. Having a `PyCapsule_Destructor` or a `void (*destructor)(void *)` are two separate and valid use cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants