Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] gh-113993: Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364)#121854

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 1 commit intopython:3.13fromencukou:backport-b4aedb2-3.13
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
[3.13]gh-113993: Don't immortalize in PyUnicode_InternInPlace; keep …
…immortalizing in other API (GH-121364)* Switch PyUnicode_InternInPlace to _PyUnicode_InternMortal, clarify docs* Document immortality in some functions that take `const char *`This is PyUnicode_InternFromString;PyDict_SetItemString, PyObject_SetAttrString;PyObject_DelAttrString; PyUnicode_InternFromString;and the PyModule_Add convenience functions.Always point out a non-immortalizing alternative.* Don't immortalize user-provided attr names in _ctypes(cherry picked from commitb4aedb2)Co-authored-by: Petr Viktorin <encukou@gmail.com>
  • Loading branch information
@encukou
encukou committedJul 16, 2024
commite65f4dac09c69edb58ca8266fae0a84434f39e6f
15 changes: 15 additions & 0 deletionsDoc/c-api/module.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -549,6 +549,14 @@ state:
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
this case, since *obj* can be ``NULL``.

The number of different *name* strings passed to this function
should be kept small, usually by only using statically allocated strings
as *name*.
For names that aren't known at compile time, prefer calling
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly.
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
used internally to create a key object.

.. versionadded:: 3.10


Expand DownExpand Up@@ -610,6 +618,9 @@ state:
used from the module's initialization function.
Return ``-1`` with an exception set on error, ``0`` on success.

This is a convenience function that calls :c:func:`PyLong_FromLong` and
:c:func:`PyModule_AddObjectRef`; see their documentation for details.


.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)

Expand All@@ -618,6 +629,10 @@ state:
``NULL``-terminated.
Return ``-1`` with an exception set on error, ``0`` on success.

This is a convenience function that calls
:c:func:`PyUnicode_InternFromString` and :c:func:`PyModule_AddObjectRef`;
see their documentation for details.


.. c:macro:: PyModule_AddIntMacro(module, macro)

Expand Down
15 changes: 15 additions & 0 deletionsDoc/c-api/object.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -206,6 +206,13 @@ Object Protocol
If *v* is ``NULL``, the attribute is deleted, but this feature is
deprecated in favour of using :c:func:`PyObject_DelAttrString`.

The number of different attribute names passed to this function
should be kept small, usually by using a statically allocated string
as *attr_name*.
For attribute names that aren't known at compile time, prefer calling
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly.
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
used internally to create a key object.

.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)

Expand All@@ -231,6 +238,14 @@ Object Protocol
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
rather than a :c:expr:`PyObject*`.

The number of different attribute names passed to this function
should be kept small, usually by using a statically allocated string
as *attr_name*.
For attribute names that aren't known at compile time, prefer calling
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_DelAttr` directly.
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
used internally to create a key object for lookup.


.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)

Expand Down
38 changes: 32 additions & 6 deletionsDoc/c-api/unicode.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1490,15 +1490,41 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
existing interned string that is the same as :c:expr:`*p_unicode`, it sets :c:expr:`*p_unicode` to
it (releasing the reference to the old string object and creating a new
:term:`strong reference` to the interned string object), otherwise it leaves
:c:expr:`*p_unicode` alone and interns it (creating a new :term:`strong reference`).
:c:expr:`*p_unicode` alone and interns it.

(Clarification: even though there is a lot of talk about references, think
of this function as reference-neutral; you own the object after the call
if and only if you owned it before the call.)
of this function as reference-neutral. You must own the object you pass in;
after the call you no longer own the passed-in reference, but you newly own
the result.)

This function never raises an exception.
On error, it leaves its argument unchanged without interning it.

Instances of subclasses of :py:class:`str` may not be interned, that is,
:c:expr:`PyUnicode_CheckExact(*p_unicode)` must be true. If it is not,
then -- as with any other error -- the argument is left unchanged.

Note that interned strings are not “immortal”.
You must keep a reference to the result to benefit from interning.


.. c:function:: PyObject* PyUnicode_InternFromString(const char *str)

A combination of :c:func:`PyUnicode_FromString` and
:c:func:`PyUnicode_InternInPlace`, returning either a new Unicode string
object that has been interned, or a new ("owned") reference to an earlier
interned string object with the same value.
:c:func:`PyUnicode_InternInPlace`, meant for statically allocated strings.

Return a new ("owned") reference to either a new Unicode string object
that has been interned, or an earlier interned string object with the
same value.

Python may keep a reference to the result, or make it :term:`immortal`,
preventing it from being garbage-collected promptly.
For interning an unbounded number of different strings, such as ones coming
from user input, prefer calling :c:func:`PyUnicode_FromString` and
:c:func:`PyUnicode_InternInPlace` directly.

.. impl-detail::

Strings interned this way are made :term:`immortal`.


View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
:c:func:`PyUnicode_InternInPlace` no longer prevents its argument from being
garbage collected.

Several functions that take ``char *`` are now
documented as possibly preventing string objects from being garbage
collected; refer to their documentation for details:
:c:func:`PyUnicode_InternFromString`,
:c:func:`PyDict_SetItemString`,
:c:func:`PyObject_SetAttrString`,
:c:func:`PyObject_DelAttrString`,
:c:func:`PyUnicode_InternFromString`,
and ``PyModule_Add*`` convenience functions.
11 changes: 8 additions & 3 deletionsModules/_ctypes/_ctypes.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2288,9 +2288,14 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
if (!meth) {
return -1;
}
x = PyDict_SetItemString(((PyTypeObject*)self)->tp_dict,
ml->ml_name,
meth);
PyObject *name = PyUnicode_FromString(ml->ml_name);
if (name == NULL) {
Py_DECREF(meth);
return -1;
}
PyUnicode_InternInPlace(&name);
x = PyDict_SetItem(((PyTypeObject*)self)->tp_dict, name, meth);
Py_DECREF(name);
Py_DECREF(meth);
if (x == -1) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletionObjects/unicodeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15280,7 +15280,7 @@ void
PyUnicode_InternInPlace(PyObject **p)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyUnicode_InternImmortal(interp, p);
_PyUnicode_InternMortal(interp, p);
}

// Public-looking name kept for the stable ABI; user should not call this:
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp