-
Notifications
You must be signed in to change notification settings - Fork 2.2k
native_enum: add capsule containing enum information and cleanup logic #5871
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import sysconfig | ||
import textwrap | ||
import traceback | ||
import weakref | ||
from typing import Callable | ||
|
||
import pytest | ||
|
@@ -218,9 +219,33 @@ def gc_collect(): | |
gc.collect() | ||
|
||
|
||
def delattr_and_ensure_destroyed(*specs): | ||
"""For each of the given *specs* (a tuple of the form ``(scope, name)``), | ||
perform ``delattr(scope, name)``, then do enough GC collections that the | ||
deleted reference has actually caused the target to be destroyed. This is | ||
typically used to test what happens when a type object is destroyed; if you | ||
use it for that, you should be aware that extension types, or all types, | ||
are immortal on some Python versions. See ``env.TYPES_ARE_IMMORTAL``. | ||
""" | ||
wrs = [] | ||
for mod, name in specs: | ||
wrs.append(weakref.ref(getattr(mod, name))) | ||
delattr(mod, name) | ||
|
||
for _ in range(5): | ||
oremanj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
gc.collect() | ||
if all(wr() is None for wr in wrs): | ||
break | ||
else: | ||
pytest.fail( | ||
f"Could not delete bindings such as {next(wr for wr in wrs if wr() is not None)!r}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's mainly to insert a hint for future maintainers, to more immediately realize that we're only trying a certain number of cycles. BTW: Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've seen this idiom in many projects and none of them even went as many as 5 times (2-4 is common). The only reason it's necessary to do more than once is because finalizers ( The comment on our At some point, pounding it harder isn't going to help, and the extra GC passes take a noticeable amount of time. I added a comment on the definition of |
||
) | ||
|
||
|
||
def pytest_configure(): | ||
pytest.suppress = contextlib.suppress | ||
pytest.gc_collect = gc_collect | ||
pytest.delattr_and_ensure_destroyed = delattr_and_ensure_destroyed | ||
|
||
|
||
def pytest_report_header(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,10 +93,15 @@ TEST_SUBMODULE(native_enum, m) { | |
.value("blue", color::blue) | ||
.finalize(); | ||
|
||
py::native_enum<altitude>(m, "altitude", "enum.Enum") | ||
.value("high", altitude::high) | ||
.value("low", altitude::low) | ||
.finalize(); | ||
m.def("bind_altitude", [](const py::module_ &mod) { | ||
py::native_enum<altitude>(mod, "altitude", "enum.Enum") | ||
.value("high", altitude::high) | ||
.value("low", altitude::low) | ||
.finalize(); | ||
}); | ||
m.attr("bind_altitude")(m); | ||
|
||
m.def("is_high_altitude", [](altitude alt) { return alt == altitude::high; }); | ||
m.def("get_altitude", []() -> altitude { return altitude::high; }); | ||
|
||
py::native_enum<flags_uchar>(m, "flags_uchar", "enum.Flag") | ||
.value("bit0", flags_uchar::bit0) | ||
|
Uh oh!
There was an error while loading. Please reload this page.