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 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
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 @@ -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
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy