Skip to content

gh-134819: Add sys.set_object_tags and sys.get_object_tags #135073

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
48 changes: 37 additions & 11 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,23 +753,25 @@ def test_43581(self):
self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)

def test_intern(self):
has_is_interned = (test.support.check_impl_detail(cpython=True)
or hasattr(sys, '_is_interned'))
self.assertRaises(TypeError, sys.intern)
self.assertRaises(TypeError, sys.intern, b'abc')
has_is_interned = (test.support.check_impl_detail(cpython=True)
or hasattr(sys, '_is_interned'))
if has_is_interned:
self.assertRaises(TypeError, sys._is_interned)
self.assertRaises(TypeError, sys._is_interned, b'abc')

def _is_interned(obj):
tags = sys.get_object_tags(obj)
return tags.get("interned", False)

s = "never interned before" + str(random.randrange(0, 10**9))
self.assertTrue(sys.intern(s) is s)
if has_is_interned:
self.assertIs(sys._is_interned(s), True)
self.assertIs(_is_interned(s), True)
s2 = s.swapcase().swapcase()
if has_is_interned:
self.assertIs(sys._is_interned(s2), False)
self.assertIs(_is_interned(s2), False)
self.assertTrue(sys.intern(s2) is s)
if has_is_interned:
self.assertIs(sys._is_interned(s2), False)
self.assertIs(_is_interned(s2), False)

# Subclasses of string can't be interned, because they
# provide too much opportunity for insane things to happen.
Expand All @@ -781,8 +783,31 @@ def __hash__(self):
return 123

self.assertRaises(TypeError, sys.intern, S("abc"))
if has_is_interned:
self.assertIs(sys._is_interned(S("abc")), False)
self.assertIs(_is_interned(S("abc")), False)

@support.cpython_only
def test_get_object_tags(self):
keys = ("immortal", "interned", "deferred_refcount")
s = "foobar"
tags = sys.get_object_tags(s)
self.assertEqual(len(tags), len(keys))
for k in keys:
self.assertIn(k, tags)

@support.cpython_only
def test_set_object_tags(self):
keys = ("immortal", "interned")
s = "should never interned before" + str(random.randrange(0, 10**9))
origin_tags = sys.get_object_tags(s)
for k in keys:
self.assertFalse(origin_tags[k])
sys.set_object_tag(s, k)
sys.set_object_tag(s, "unknown")
after_tags = sys.get_object_tags(s)
self.assertEqual(len(origin_tags), len(after_tags))
for k in keys:
self.assertIn(k, after_tags)
self.assertTrue(after_tags[k])

@support.cpython_only
@requires_subinterpreters
Expand Down Expand Up @@ -847,7 +872,8 @@ def test_subinterp_intern_singleton(self):
assert id(s) == {id(s)}
t = sys.intern(s)
'''))
self.assertTrue(sys._is_interned(s))
tags = sys.get_object_tags(s)
self.assertTrue(tags.get("interned", False))

def test_sys_flags(self):
self.assertTrue(sys.flags)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`sys.get_object_tags` and :func:`sys.set_object_tags` for handling
CPython object implementation detail. Patch By Donghee Na.
91 changes: 90 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.

87 changes: 87 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,91 @@ sys__is_immortal_impl(PyObject *module, PyObject *op)
return PyUnstable_IsImmortal(op);
}


/*[clinic input]
sys.get_object_tags -> object

op: object
/
Return the tags of the given object.
[clinic start generated code]*/

static PyObject *
sys_get_object_tags(PyObject *module, PyObject *op)
/*[clinic end generated code: output=a68da7f1805c9216 input=75993fb67096e2ff]*/
{
assert(op != NULL);
PyObject *dict = PyDict_New();
if (dict == NULL) {
return NULL;
}
if (PyUnstable_IsImmortal(op)) {
if (PyDict_SetItemString(dict, "immortal", Py_True) < 0) {
Py_DECREF(dict);
return NULL;
}
}
else {
if (PyDict_SetItemString(dict, "immortal", Py_False) < 0) {
Py_DECREF(dict);
return NULL;
}
}

if (PyUnicode_Check(op) && PyUnicode_CHECK_INTERNED(op)) {
if (PyDict_SetItemString(dict, "interned", Py_True) < 0) {
Py_DECREF(dict);
return NULL;
}
}
else {
if (PyDict_SetItemString(dict, "interned", Py_False) < 0) {
Py_DECREF(dict);
return NULL;
}
}

if (PyUnstable_Object_EnableDeferredRefcount(op)) {
if (PyDict_SetItemString(dict, "deferred_refcount", Py_True) < 0) {
Py_DECREF(dict);
return NULL;
}
}
else {
if (PyDict_SetItemString(dict, "deferred_refcount", Py_False) < 0) {
Py_DECREF(dict);
return NULL;
}
}
return dict;
}

/*[clinic input]
sys.set_object_tag -> object

object: object
tag: str
*
options: object = None

Set the tags of the given object.
[clinic start generated code]*/

static PyObject *
sys_set_object_tag_impl(PyObject *module, PyObject *object, const char *tag,
PyObject *options)
/*[clinic end generated code: output=b0fb5e9931feb4aa input=b64c9bd958c75f11]*/
{
assert(object != NULL);
if (strcmp(tag, "immortal") == 0) {
_Py_SetImmortal(object);
Copy link
Member

Choose a reason for hiding this comment

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

This definitely isn't safe on its own. (Trust me, I've gone down quite the rabbithole in getting arbitrary object immortalization working. It's extraordinarily complex to do safely.)

Copy link
Member Author

@corona10 corona10 Jun 3, 2025

Choose a reason for hiding this comment

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

Hmm, do you think that we should not allow setting "immortal" tag at this moment?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, at least for now. sys.set_object_tags is allowed to ignore tags, right?

Copy link
Member Author

@corona10 corona10 Jun 3, 2025

Choose a reason for hiding this comment

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

Yeah, we will ignore (and it's intended behavior)

}
else if (strcmp(tag, "interned") == 0) {
_PyUnicode_InternMortal(_PyInterpreterState_GET(), &object);
}
Py_RETURN_NONE;
}

/*
* Cached interned string objects used for calling the profile and
* trace functions.
Expand Down Expand Up @@ -2796,6 +2881,8 @@ static PyMethodDef sys_methods[] = {
SYS__IS_IMMORTAL_METHODDEF
SYS_INTERN_METHODDEF
SYS__IS_INTERNED_METHODDEF
SYS_GET_OBJECT_TAGS_METHODDEF
SYS_SET_OBJECT_TAG_METHODDEF
SYS_IS_FINALIZING_METHODDEF
SYS_MDEBUG_METHODDEF
SYS_SETSWITCHINTERVAL_METHODDEF
Expand Down
Loading