Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add sys._is_immortal
  • Loading branch information
ZeroIntensity committed Jan 5, 2025
commit 120bc73de51660d23d06b461878adee875de7f21
14 changes: 14 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,20 @@ def test_43581(self):
# the test runs under regrtest.
self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)

@unittest.skipUnless(hasattr(sys, "_is_immortal"))
def test_immortal(self):
# Not extensive
known_immortals = (True, False, None, 0, ())
for immortal in known_immortals:
with self.subTest(immortal=immortal):
self.assertTrue(sys._is_immortal(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(sys._is_immortal(non_immortal))

def test_intern(self):
has_is_interned = (test.support.check_impl_detail(cpython=True)
or hasattr(sys, '_is_interned'))
Expand Down
30 changes: 29 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,21 @@ sys_intern_impl(PyObject *module, PyObject *s)
}
}

/*[clinic input]
Comment thread
ZeroIntensity marked this conversation as 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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not using PyUnstable_IsImmortal()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer to use the public unstable API, yes.

}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unrelated change, I suggest to leave this empty line :-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch!

/*[clinic input]
sys._is_interned -> bool
Expand Down Expand Up @@ -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
Expand Down