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

Skip to content

Commit 8a4eb29

Browse files
committed
Fix refleaks in test_unicode and test_string related to the new format code.
Stop polluting namespace.
1 parent 2bad970 commit 8a4eb29

4 files changed

Lines changed: 27 additions & 38 deletions

File tree

Include/unicodeobject.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,10 +1437,8 @@ PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strchr(
14371437
const Py_UNICODE *s, Py_UNICODE c
14381438
);
14391439

1440-
PyObject *
1441-
_unicodeformatter_iterator(PyObject *str);
1442-
PyObject *
1443-
_unicodeformatter_field_name_split(PyObject *field_name);
1440+
PyObject *_PyUnicode_FormatterIterator(PyObject *str);
1441+
PyObject *_PyUnicode_FormatterFieldNameSplit(PyObject *field_name);
14441442

14451443
#ifdef __cplusplus
14461444
}

Objects/stringlib/string_format.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs)
416416
Py_DECREF(key);
417417
goto error;
418418
}
419+
Py_DECREF(key);
419420
Py_INCREF(obj);
420421
} else {
421422
/* look up in args */

Objects/unicodeobject.c

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9206,11 +9206,11 @@ formatteriter_next(formatteriterobject *it)
92069206
PyObject *field_name_str = NULL;
92079207
PyObject *format_spec_str = NULL;
92089208
PyObject *conversion_str = NULL;
9209-
PyObject *result = NULL;
9209+
PyObject *tuple = NULL;
92109210

92119211
is_markup_bool = PyBool_FromLong(is_markup);
92129212
if (!is_markup_bool)
9213-
goto error;
9213+
return NULL;
92149214

92159215
if (is_markup) {
92169216
/* field_name, format_spec, and conversion are
@@ -9251,22 +9251,16 @@ formatteriter_next(formatteriterobject *it)
92519251
Py_INCREF(format_spec_str);
92529252
Py_INCREF(conversion_str);
92539253
}
9254-
/* return a tuple of values */
9255-
result = PyTuple_Pack(5, is_markup_bool, literal_str,
9256-
field_name_str, format_spec_str,
9257-
conversion_str);
9258-
if (result == NULL)
9259-
goto error;
9260-
9261-
return result;
9254+
tuple = PyTuple_Pack(5, is_markup_bool, literal_str,
9255+
field_name_str, format_spec_str,
9256+
conversion_str);
92629257
error:
92639258
Py_XDECREF(is_markup_bool);
92649259
Py_XDECREF(literal_str);
92659260
Py_XDECREF(field_name_str);
92669261
Py_XDECREF(format_spec_str);
92679262
Py_XDECREF(conversion_str);
9268-
Py_XDECREF(result);
9269-
return NULL;
9263+
return tuple;
92709264
}
92719265
}
92729266

@@ -9308,10 +9302,11 @@ PyTypeObject PyFormatterIter_Type = {
93089302
};
93099303

93109304
PyObject *
9311-
_unicodeformatter_iterator(PyObject *str)
9305+
_PyUnicode_FormatterIterator(PyObject *str)
93129306
{
93139307
formatteriterobject *it;
93149308

9309+
assert(PyUnicode_Check(str));
93159310
it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
93169311
if (it == NULL)
93179312
return NULL;
@@ -9440,21 +9435,24 @@ static PyTypeObject PyFieldNameIter_Type = {
94409435
0};
94419436

94429437
PyObject *
9443-
_unicodeformatter_field_name_split(PyObject *field_name)
9438+
_PyUnicode_FormatterFieldNameSplit(PyObject *field_name)
94449439
{
94459440
SubString first;
94469441
Py_ssize_t first_idx;
94479442
fieldnameiterobject *it;
94489443

94499444
PyObject *first_obj = NULL;
9450-
PyObject *it_obj = NULL;
9451-
PyObject *result;
9445+
PyObject *result = NULL;
94529446

9447+
assert(PyUnicode_Check(field_name));
94539448
it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
94549449
if (it == NULL)
9455-
goto error;
9456-
it->str = NULL;
9457-
it_obj = (PyObject *)it;
9450+
return NULL;
9451+
9452+
/* take ownership, give the object to the iterator. this is
9453+
just to keep the field_name alive */
9454+
Py_INCREF(field_name);
9455+
it->str = field_name;
94589456

94599457
if (!field_name_split(STRINGLIB_STR(field_name),
94609458
STRINGLIB_LEN(field_name),
@@ -9470,21 +9468,13 @@ _unicodeformatter_field_name_split(PyObject *field_name)
94709468
if (first_obj == NULL)
94719469
goto error;
94729470

9473-
/* take ownership, give the object to the iterator. this is
9474-
just to keep the field_name alive */
9475-
Py_INCREF(field_name);
9476-
it->str = field_name;
9477-
94789471
/* return a tuple of values */
9479-
result = PyTuple_Pack(2, first_obj, it_obj);
9480-
if (result == NULL)
9481-
goto error;
9472+
result = PyTuple_Pack(2, first_obj, it);
94829473

9483-
return result;
94849474
error:
9485-
Py_XDECREF(it_obj);
9475+
Py_XDECREF(it);
94869476
Py_XDECREF(first_obj);
9487-
return NULL;
9477+
return result;
94889478
}
94899479

94909480
/********************* Unicode Iterator **************************/

Python/sysmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ sys_current_frames(PyObject *self, PyObject *noargs)
663663
/* sys_formatter_iterator is used to implement
664664
string.Formatter.vformat. it parses a string and returns tuples
665665
describing the parsed elements. see unicodeobject.c's
666-
_unicodeformatter_iterator for details */
666+
_PyUnicode_FormatterIterator for details */
667667
static PyObject *
668668
sys_formatter_iterator(PyObject *self, PyObject *args)
669669
{
@@ -680,14 +680,14 @@ sys_formatter_iterator(PyObject *self, PyObject *args)
680680
return NULL;
681681
}
682682

683-
return _unicodeformatter_iterator(str);
683+
return _PyUnicode_FormatterIterator(str);
684684
}
685685

686686
/* sys_formatter_field_name_split is used to implement
687687
string.Formatter.vformat. it takes an PEP 3101 "field name", and
688688
returns a tuple of (first, rest): "first", the part before the
689689
first '.' or '['; and "rest", an iterator for the rest of the field
690-
name. see unicodeobjects' _unicode_formatter_field_name_split for
690+
name. see unicodeobjects' _PyUnicode_FormatterFieldNameSplit for
691691
details */
692692
static PyObject *
693693
sys_formatter_field_name_split(PyObject *self, PyObject *args)
@@ -704,7 +704,7 @@ sys_formatter_field_name_split(PyObject *self, PyObject *args)
704704
return NULL;
705705
}
706706

707-
return _unicodeformatter_field_name_split(field_name);
707+
return _PyUnicode_FormatterFieldNameSplit(field_name);
708708
}
709709

710710

0 commit comments

Comments
 (0)