Skip to content
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

Add custom_type_setup attribute #3287

Merged
merged 2 commits into from
Sep 24, 2021
Merged

Conversation

jbms
Copy link
Contributor

@jbms jbms commented Sep 20, 2021

This allows for custom modifications to the PyHeapTypeObject prior to
calling PyType_Ready. This may be used, for example, to define
tp_traverse and tp_clear functions.

Description

Suggested changelog entry:

``pybind11::custom_type_setup`` was added, for customizing the ``PyHeapTypeObject`` corresponding to a class, which may be useful for enabling garbage collection support, among other things.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Initially the suggested changelog entry made me wonder: does custom_type_setup exist already? Is this PR enhancing the functionality?
But no, it's new.
How about:

pybind11::custom_type_setup was added, for customizing the PyHeapTypeObject corresponding to a class_, which may be useful for enabling garbage collection support, among other things.

@jbms
Copy link
Contributor Author

jbms commented Sep 21, 2021

Thanks, I updated the "suggested changelog" per your suggestion.

An alternative is to add direct support for garbage collection, similar to the other protocols. But still I think custom_type_setup is useful as an escape hatch, so that users can implement any protocol they like without waiting for pybind11 to specifically support it.

@jbms jbms force-pushed the feat-custom-type-setup branch 3 times, most recently from 8e0d428 to dc4fbfe Compare September 21, 2021 06:01
@jbms jbms force-pushed the feat-custom-type-setup branch 5 times, most recently from e295d8c to e06f7ca Compare September 21, 2021 20:17
@jbms
Copy link
Contributor Author

jbms commented Sep 21, 2021

All tests pass now.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

I believe this is a useful addition, although I also wonder if this will lead to N almost correct implementations for adding GC support (in other repos), instead of 1 firmly correct implementation (in this repo). — On balance I'm coming down on the side of "let it grow and maybe clean up later" vs slipping into over-engineering.

docs/advanced/classes.rst Show resolved Hide resolved
include/pybind11/detail/function_view.h Outdated Show resolved Hide resolved
}

R (*erased_fn_)(void *, Arg...) = nullptr;
const void *erased_obj_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

type_erased_obj_?
If that's correct, I'd definitely spend the extra few characters for clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tests/test_custom_type_setup.py Outdated Show resolved Hide resolved
tests/test_custom_type_setup.py Outdated Show resolved Hide resolved
tests/test_custom_type_setup.py Show resolved Hide resolved
@jbms
Copy link
Contributor Author

jbms commented Sep 22, 2021

It looks like all tests now pass --- the one failure was just a spurious one.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

I'd feel much more comfortable with this PR if the documentation had a pointer to tested code elsewhere, and if we either reuse std::function, or have measurements to show that the function_view optimization is worth the extra code.

But I think it'll be best to first let @henryiii and @Skylion007 weigh in. I'm OK with this PR as-is if it gets support from a majority.

include/pybind11/detail/function_view.h Outdated Show resolved Hide resolved
docs/advanced/classes.rst Show resolved Hide resolved
@henryiii
Copy link
Collaborator

I'd feel much more comfortable with this PR if the documentation had a pointer to tested code elsewhere

The current documentation does not do this. We probably should use include markers and test the documentation examples that way, but we don't, and I don't think we should change styles in the middle of an unrelated PR. We in general have to be careful changing any public interface, and this makes that a public interface, so the docs shouldn't bit-rot too fast.

include/pybind11/attr.h Outdated Show resolved Hide resolved
include/pybind11/detail/function_view.h Outdated Show resolved Hide resolved
@jbms jbms force-pushed the feat-custom-type-setup branch 2 times, most recently from cdfbcf8 to f808a98 Compare September 23, 2021 18:53
@jbms
Copy link
Contributor Author

jbms commented Sep 23, 2021

Note: Tests are now failing on Windows, due to an unrelated warning, presumably due to a compiler upgrade on github actions.

@henryiii
Copy link
Collaborator

henryiii commented Sep 23, 2021

The last build on master compiled after this, with no errors. Also Appveyor also failed. Notice it's only 2.7 failing (yuck).

include/pybind11/attr.h Outdated Show resolved Hide resolved
@jbms
Copy link
Contributor Author

jbms commented Sep 23, 2021

This is the error I'm seeing on both AppVeyor and github actions:

C:\projects\pybind11\include\pybind11/embed.h(108): warning C4996: 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\projects\pybind11\tests\test_embed\test_embed.vcxproj]
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdlib.h(920): note: see declaration of 'mbstowcs'
C:\projects\pybind11\include\pybind11/embed.h(112): warning C4996: 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\projects\pybind11\tests\test_embed\test_embed.vcxproj]
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdlib.h(920): note: see declaration of 'mbstowcs'
  test_interpreter.cpp
C:\projects\pybind11\include\pybind11/embed.h(108): error C2220: warning treated as error - no 'object' file generated [C:\projects\pybind11\tests\test_embed\test_embed.vcxproj]
C:\projects\pybind11\include\pybind11/embed.h(108): warning C4996: 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\projects\pybind11\tests\test_embed\test_embed.vcxproj]
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdlib.h(920): note: see declaration of 'mbstowcs'
C:\projects\pybind11\include\pybind11/embed.h(112): warning C4996: 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\projects\pybind11\tests\test_embed\test_embed.vcxproj]
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\stdlib.h(920): note: see declaration of 'mbstowcs'

@jbms jbms closed this Sep 23, 2021
@jbms jbms reopened this Sep 23, 2021
include/pybind11/attr.h Outdated Show resolved Hide resolved
@jbms
Copy link
Contributor Author

jbms commented Sep 24, 2021

Okay, all tests are passing now.

docs/advanced/classes.rst Outdated Show resolved Hide resolved
tests/test_custom_type_setup.cpp Outdated Show resolved Hide resolved
include/pybind11/detail/function_view.h Outdated Show resolved Hide resolved
This allows for custom modifications to the PyHeapTypeObject prior to
calling `PyType_Ready`.  This may be used, for example, to define
`tp_traverse` and `tp_clear` functions.
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Potentially adding function_view in another PR sounds fine, although only if we can show that the extra code has some measurable benefit.

@jbms
Copy link
Contributor Author

jbms commented Sep 24, 2021

Is there anything left to do here?

@rwgk
Copy link
Collaborator

rwgk commented Sep 24, 2021

Is there anything left to do here?

@henryiii, @Skylion007, are you OK with this PR as it is now?

@rwgk
Copy link
Collaborator

rwgk commented Sep 24, 2021

Thanks @henryiii !

@rwgk rwgk merged commit 62c4909 into pybind:master Sep 24, 2021
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Sep 24, 2021
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Sep 24, 2021
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.

4 participants