Skip to content
Closed
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
Prev Previous commit
Next Next commit
Add set_object_tag
  • Loading branch information
corona10 committed Jun 3, 2025
commit fb997ab1ce4653dd0a927ec3946b69cae1040fdd
14 changes: 14 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,20 @@ def test_get_object_tags(self):
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:
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
def test_subinterp_intern_dynamically_allocated(self):
Expand Down
82 changes: 81 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.

29 changes: 28 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ sys_get_object_tags(PyObject *module, PyObject *op)
}
}

if (PyUnicode_CHECK_INTERNED(op)) {
if (PyUnicode_Check(op) && PyUnicode_CHECK_INTERNED(op)) {
if (PyDict_SetItemString(dict, "interned", Py_True) < 0) {
Py_DECREF(dict);
return NULL;
Expand Down Expand Up @@ -1111,6 +1111,32 @@ sys_get_object_tags(PyObject *module, PyObject *op)
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
Copy Markdown
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.)

@corona10 corona10 Jun 3, 2025 •

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.

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

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.

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

@corona10 corona10 Jun 3, 2025 •

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.

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 @@ -2856,6 +2882,7 @@ static PyMethodDef sys_methods[] = {
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