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 all 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
45 changes: 34 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["interned"]

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,28 @@ 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", "deferred_refcount")
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))

@support.cpython_only
@requires_subinterpreters
Expand Down Expand Up @@ -847,7 +869,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["interned"])

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.

67 changes: 67 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Data members:
#include "pycore_audit.h" // _Py_AuditHookEntry
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetAsyncGenFinalizer()
#include "pycore_object_deferred.h" // _PyObject_HasDeferredRefcount
#include "pycore_frame.h" // _PyInterpreterFrame
#include "pycore_import.h" // _PyImport_SetDLOpenFlags()
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
Expand Down Expand Up @@ -1052,6 +1053,70 @@ 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 (PyDict_SetItemString(dict, "immortal", PyBool_FromLong(PyUnstable_IsImmortal(op))) < 0) {
Py_DECREF(dict);
return NULL;
}

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

if (PyDict_SetItemString(dict, "deferred_refcount", PyBool_FromLong(_PyObject_HasDeferredRefcount(op))) < 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, "interned") == 0) {
Py_INCREF(object);
_PyUnicode_InternMortal(_PyInterpreterState_GET(), &object);
}
else if(strcmp(tag, "deferred_refcount") == 0) {
PyUnstable_Object_EnableDeferredRefcount(object);
}
Py_RETURN_NONE;
}

/*
* Cached interned string objects used for calling the profile and
* trace functions.
Expand Down Expand Up @@ -2796,6 +2861,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