-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
2cc543c
17ccccf
fb997ab
5027d35
ba2c974
658e731
e725c7f
1cdc798
dbc30b7
e7d06b6
ff34816
4e1946f
2a0eb8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, at least for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.