Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d10759f

Browse files
committed
Make _PyUnicode_FromId return borrowed references.
http://mail.python.org/pipermail/python-dev/2011-November/114347.html
1 parent e9b11c1 commit d10759f

5 files changed

Lines changed: 5 additions & 12 deletions

File tree

Include/unicodeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
20242024
shutdown, all strings are released (through _PyUnicode_ClearStaticStrings).
20252025
20262026
Alternatively, _Py_static_string allows to choose the variable name.
2027-
_PyUnicode_FromId returns a new reference to the interned string.
2027+
_PyUnicode_FromId returns a borrowed reference to the interned string.
20282028
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
20292029
*/
20302030
typedef struct _Py_Identifier {

Objects/object.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -814,35 +814,32 @@ PyObject *
814814
_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
815815
{
816816
PyObject *result;
817-
PyObject *oname = _PyUnicode_FromId(name);
817+
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
818818
if (!oname)
819819
return NULL;
820820
result = PyObject_GetAttr(v, oname);
821-
Py_DECREF(oname);
822821
return result;
823822
}
824823

825824
int
826825
_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
827826
{
828827
int result;
829-
PyObject *oname = _PyUnicode_FromId(name);
828+
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
830829
if (!oname)
831830
return -1;
832831
result = PyObject_HasAttr(v, oname);
833-
Py_DECREF(oname);
834832
return result;
835833
}
836834

837835
int
838836
_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
839837
{
840838
int result;
841-
PyObject *oname = _PyUnicode_FromId(name);
839+
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
842840
if (!oname)
843841
return -1;
844842
result = PyObject_SetAttr(v, oname, w);
845-
Py_DECREF(oname);
846843
return result;
847844
}
848845

Objects/typeobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,11 +1716,10 @@ get_dict_descriptor(PyTypeObject *type)
17161716
PyObject *dict_str;
17171717
PyObject *descr;
17181718

1719-
dict_str = _PyUnicode_FromId(&PyId___dict__);
1719+
dict_str = _PyUnicode_FromId(&PyId___dict__); /* borrowed */
17201720
if (dict_str == NULL)
17211721
return NULL;
17221722
descr = _PyType_Lookup(type, dict_str);
1723-
Py_DECREF(dict_str);
17241723
if (descr == NULL || !PyDescr_IsData(descr))
17251724
return NULL;
17261725

Objects/unicodeobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,6 @@ _PyUnicode_FromId(_Py_Identifier *id)
17231723
id->next = static_strings;
17241724
static_strings = id;
17251725
}
1726-
Py_INCREF(id->object);
17271726
return id->object;
17281727
}
17291728

Python/_warnings.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,8 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
666666

667667
if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
668668
return NULL;
669-
Py_DECREF(tmp);
670669
if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
671670
return NULL;
672-
Py_DECREF(tmp);
673671

674672
/* Check/get the requisite pieces needed for the loader. */
675673
loader = PyDict_GetItemString(module_globals, "__loader__");

0 commit comments

Comments
 (0)