FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128509: Add `PyUnstable_IsImmortal` for finding immortal objects by ZeroIntensity · Pull Request #129182 · python/cpython · GitHub

Repository navigation

Merged
Prev Previous commit
Next Next commit
Prevent tests from running twice and move is_immortal()
  • Loading branch information
ZeroIntensity committed Jan 24, 2025
commit 5b949b1b93f665dd27bd06b27fbba8a147cd1bb5
35 changes: 15 additions & 20 deletions Lib/test/test_capi/test_immortal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
_testinternalcapi = import_helper.import_module('_testinternalcapi')


class TestCAPI(unittest.TestCase):
def test_immortal_builtins(self):
_testcapi.test_immortal_builtins()
class TestUnstableCAPI(unittest.TestCase):
def test_immortal(self):
# Not extensive
known_immortals = (True, False, None, 0, ())
for immortal in known_immortals:
with self.subTest(immortal=immortal):
self.assertTrue(_testcapi.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(_testcapi.is_immortal(non_immortal))

# CRASHES _testcapi.is_immortal(NULL)

def test_immortal_small_ints(self):
_testcapi.test_immortal_small_ints()

class TestInternalCAPI(unittest.TestCase):

Expand All @@ -27,21 +37,6 @@ def test_immortal_builtins(self):
for obj in ([], {}, set()):
self.assertFalse(_testinternalcapi.is_static_immortal(obj))

def test_immortal(self):
# Not extensive
known_immortals = (True, False, None, 0, ())
for immortal in known_immortals:
with self.subTest(immortal=immortal):
self.assertTrue(_testcapi.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(_testcapi.is_immortal(non_immortal))

# CRASHES _testcapi.is_immortal(NULL)


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions Modules/_testcapi/immortal.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
Py_RETURN_NONE;
}

static PyObject *
is_immortal(PyObject *self, PyObject *op)
{
return PyBool_FromLong(PyUnstable_IsImmortal(op));
}

static PyMethodDef test_methods[] = {
{"test_immortal_builtins", test_immortal_builtins, METH_NOARGS},
{"test_immortal_small_ints", test_immortal_small_ints, METH_NOARGS},
{"is_immortal", is_immortal, METH_O}
{NULL},
};

Expand Down
8 changes: 0 additions & 8 deletions Modules/_testcapi/object.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,13 @@ pyobject_enable_deferred_refcount(PyObject *self, PyObject *obj)
}


static PyObject *
is_immortal(PyObject *self, PyObject *op)
{
return PyBool_FromLong(PyUnstable_IsImmortal(op));
}


static PyMethodDef test_methods[] = {
{"call_pyobject_print", call_pyobject_print, METH_VARARGS},
{"pyobject_print_null", pyobject_print_null, METH_VARARGS},
{"pyobject_print_noref_object", pyobject_print_noref_object, METH_VARARGS},
{"pyobject_print_os_error", pyobject_print_os_error, METH_VARARGS},
{"pyobject_clear_weakrefs_no_callbacks", pyobject_clear_weakrefs_no_callbacks, METH_O},
{"pyobject_enable_deferred_refcount", pyobject_enable_deferred_refcount, METH_O},
{"is_immortal", is_immortal, METH_O},
{NULL},
};

Expand Down

Back | FazBrowse Home | New Git URL