Skip to content

Commit bf72557

Browse files
author
christian.heimes
committed
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
git-svn-id: http://svn.python.org/projects/python/trunk@60381 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent e6bc5fb commit bf72557

6 files changed

Lines changed: 17 additions & 16 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,9 +1529,9 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
15291529

15301530
if (suffix == NULL)
15311531
#ifdef WORDS_BIGENDIAN
1532-
suffix = PyString_FromString("_le");
1532+
suffix = PyString_InternFromString("_le");
15331533
#else
1534-
suffix = PyString_FromString("_be");
1534+
suffix = PyString_InternFromString("_be");
15351535
#endif
15361536

15371537
Py_INCREF(name);
@@ -4416,7 +4416,7 @@ Simple_repr(CDataObject *self)
44164416
}
44174417

44184418
if (format == NULL) {
4419-
format = PyString_FromString("%s(%r)");
4419+
format = PyString_InternFromString("%s(%r)");
44204420
if (format == NULL)
44214421
return NULL;
44224422
}

Modules/_ctypes/callbacks.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
368368
static PyObject *context;
369369

370370
if (context == NULL)
371-
context = PyString_FromString("_ctypes.DllGetClassObject");
371+
context = PyString_InternFromString("_ctypes.DllGetClassObject");
372372

373373
mod = PyImport_ImportModuleNoBlock("ctypes");
374374
if (!mod) {
@@ -447,7 +447,7 @@ long Call_CanUnloadNow(void)
447447
static PyObject *context;
448448

449449
if (context == NULL)
450-
context = PyString_FromString("_ctypes.DllCanUnloadNow");
450+
context = PyString_InternFromString("_ctypes.DllCanUnloadNow");
451451

452452
mod = PyImport_ImportModuleNoBlock("ctypes");
453453
if (!mod) {

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ abstract_get_bases(PyObject *cls)
21422142
PyObject *bases;
21432143

21442144
if (__bases__ == NULL) {
2145-
__bases__ = PyString_FromString("__bases__");
2145+
__bases__ = PyString_InternFromString("__bases__");
21462146
if (__bases__ == NULL)
21472147
return NULL;
21482148
}
@@ -2220,7 +2220,7 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
22202220
int retval = 0;
22212221

22222222
if (__class__ == NULL) {
2223-
__class__ = PyString_FromString("__class__");
2223+
__class__ = PyString_InternFromString("__class__");
22242224
if (__class__ == NULL)
22252225
return -1;
22262226
}

Objects/complexobject.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,25 +261,26 @@ PyComplex_AsCComplex(PyObject *op)
261261
return ((PyComplexObject *)op)->cval;
262262
}
263263
/* If not, use op's __complex__ method, if it exists */
264-
264+
265265
/* return -1 on failure */
266266
cv.real = -1.;
267267
cv.imag = 0.;
268+
269+
if (complex_str == NULL) {
270+
if (!(complex_str = PyString_InternFromString("__complex__")))
271+
return cv;
272+
}
268273

269274
if (PyInstance_Check(op)) {
270275
/* this can go away in python 3000 */
271-
if (PyObject_HasAttrString(op, "__complex__")) {
276+
if (PyObject_HasAttr(op, complex_str)) {
272277
newop = PyObject_CallMethod(op, "__complex__", NULL);
273278
if (!newop)
274279
return cv;
275280
}
276281
/* else try __float__ */
277282
} else {
278283
PyObject *complexfunc;
279-
if (!complex_str) {
280-
if (!(complex_str = PyString_FromString("__complex__")))
281-
return cv;
282-
}
283284
complexfunc = _PyType_Lookup(op->ob_type, complex_str);
284285
/* complexfunc is a borrowed reference */
285286
if (complexfunc) {

Objects/fileobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,7 @@ file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
19651965
assert(type != NULL && type->tp_alloc != NULL);
19661966

19671967
if (not_yet_string == NULL) {
1968-
not_yet_string = PyString_FromString("<uninitialized file>");
1968+
not_yet_string = PyString_InternFromString("<uninitialized file>");
19691969
if (not_yet_string == NULL)
19701970
return NULL;
19711971
}

Python/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ compiler_mod(struct compiler *c, mod_ty mod)
11531153
int addNone = 1;
11541154
static PyObject *module;
11551155
if (!module) {
1156-
module = PyString_FromString("<module>");
1156+
module = PyString_InternFromString("<module>");
11571157
if (!module)
11581158
return NULL;
11591159
}
@@ -2001,7 +2001,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
20012001
if (Py_OptimizeFlag)
20022002
return 1;
20032003
if (assertion_error == NULL) {
2004-
assertion_error = PyString_FromString("AssertionError");
2004+
assertion_error = PyString_InternFromString("AssertionError");
20052005
if (assertion_error == NULL)
20062006
return 0;
20072007
}

0 commit comments

Comments
 (0)