-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-128509: Add sys._is_immortal
for identifying immortal objects
#128510
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 11 commits
120bc73
ac45261
b545f24
49b0ea8
b31efe5
45dc9e0
32d7db0
8bb4f62
638edd0
20bb910
64c9a52
7e416e7
10560ef
51daa10
fdcafec
380bca7
74a751a
be49c26
1154cfe
a071b86
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 |
---|---|---|
@@ -1,26 +1,32 @@ | ||
import unittest | ||
from test.support import import_helper | ||
import sys | ||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
_testinternalcapi = import_helper.import_module('_testinternalcapi') | ||
|
||
|
||
class TestUnstableCAPI(unittest.TestCase): | ||
def test_immortal(self): | ||
class TestImmortalAPI(unittest.TestCase): | ||
def immortal_checking(self, func): | ||
# Not extensive | ||
known_immortals = (True, False, None, 0, ()) | ||
for immortal in known_immortals: | ||
with self.subTest(immortal=immortal): | ||
self.assertTrue(_testcapi.is_immortal(immortal)) | ||
self.assertTrue(func(immortal)) | ||
|
||
# Some arbitrary mutable objects | ||
non_immortals = (object(), self, [object()]) | ||
for non_immortal in non_immortals: | ||
with self.subTest(non_immortal=non_immortal): | ||
self.assertFalse(_testcapi.is_immortal(non_immortal)) | ||
self.assertFalse(func(non_immortal)) | ||
|
||
def test_unstable_c_api(self): | ||
self.immortal_checking(_testcapi.is_immortal) | ||
# CRASHES _testcapi.is_immortal(NULL) | ||
|
||
def test_sys(self): | ||
self.immortal_checking(sys._is_immortal) | ||
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. sys functions must be checked in test_sys. 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 intentionally moved the test from The 6D40 re 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'm ok with redundancy. test_capi can be skipped if _testcapi is missing. It's not the case for test_sys. Both tests are useful. |
||
|
||
|
||
class TestInternalCAPI(unittest.TestCase): | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :func:`sys._is_immortal` for identifying :term:`immortal` objects at | ||
runtime. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -955,6 +955,21 @@ sys_intern_impl(PyObject *module, PyObject *s) | |
} | ||
} | ||
|
||
/*[clinic input] | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sys._is_immortal -> bool | ||
|
||
op: object | ||
/ | ||
|
||
Return True if the given object is "immortal" per PEP 683. | ||
[clinic start generated code]*/ | ||
|
||
static int | ||
sys__is_immortal_impl(PyObject *module, PyObject *op) | ||
/*[clinic end generated code: output=c2f5d6a80efb8d1a input=83733fc356c78475]*/ | ||
{ | ||
return _Py_IsImmortal(op); | ||
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. Why not using PyUnstable_IsImmortal()? 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. The two are pretty similar. Would you prefer I use the unstable API? 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 would prefer to use the public unstable API, yes. |
||
} | ||
|
||
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. unrelated change, I suggest to leave this empty line :-) 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. Good catch! |
||
/*[clinic input] | ||
sys._is_interned -> bool | ||
|
@@ -2590,6 +2605,7 @@ static PyMethodDef sys_methods[] = { | |
SYS__GETFRAMEMODULENAME_METHODDEF | ||
SYS_GETWINDOWSVERSION_METHODDEF | ||
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF | ||
SYS__IS_IMMORTAL_METHODDEF | ||
SYS_INTERN_METHODDEF | ||
SYS__IS_INTERNED_METHODDEF | ||
SYS_IS_FINALIZING_METHODDEF | ||
|
Uh oh!
There was an error while loading. Please reload this page.