diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-07-22 20:24:54 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-07-22 20:24:54 (GMT) |
commit | 1c8f059019d79f1891f42a2656a96919a1187967 (patch) | |
tree | bcec768e9ca0d0e1e9e1ab80e0dfc0fe29ae758d /Objects | |
parent | 2a545099f7ed45de1d45b45200d82c6298b75d2b (diff) | |
download | cpython-1c8f059019d79f1891f42a2656a96919a1187967.zip cpython-1c8f059019d79f1891f42a2656a96919a1187967.tar.gz cpython-1c8f059019d79f1891f42a2656a96919a1187967.tar.bz2 |
Issue #18520: Add a new PyStructSequence_InitType2() function, same than
PyStructSequence_InitType() except that it has a return value (0 on success,
-1 on error).
* PyStructSequence_InitType2() now raises MemoryError on memory allocation failure
* Fix also some calls to PyDict_SetItemString(): handle error
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/floatobject.c | 9 | ||||
-rw-r--r-- | Objects/longobject.c | 6 | ||||
-rw-r--r-- | Objects/structseq.c | 37 |
3 files changed, 37 insertions, 15 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 1398fa5..7ee2034 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1853,7 +1853,7 @@ PyTypeObject PyFloat_Type = { float_new, /* tp_new */ }; -void +int _PyFloat_Init(void) { /* We attempt to determine if this machine is using IEEE @@ -1903,8 +1903,11 @@ _PyFloat_Init(void) float_format = detected_float_format; /* Init float info */ - if (FloatInfoType.tp_name == 0) - PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc); + if (FloatInfoType.tp_name == NULL) { + if (PyStructSequence_InitType2(&FloatInfoType, &floatinfo_desc) < 0) + return 0; + } + return 1; } int diff --git a/Objects/longobject.c b/Objects/longobject.c index 925e55a..ce75888 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5059,8 +5059,10 @@ _PyLong_Init(void) } #endif /* initialize int_info */ - if (Int_InfoType.tp_name == 0) - PyStructSequence_InitType(&Int_InfoType, &int_info_desc); + if (Int_InfoType.tp_name == NULL) { + if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) + return 0; + } return 1; } diff --git a/Objects/structseq.c b/Objects/structseq.c index 5553267..664344b 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -320,12 +320,13 @@ static PyTypeObject _struct_sequence_template = { structseq_new, /* tp_new */ }; -void -PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) +int +PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) { PyObject *dict; PyMemberDef* members; int n_members, n_unnamed_members, i, k; + PyObject *v; #ifdef Py_TRACE_REFS /* if the type object was chained, unchain it first @@ -347,8 +348,10 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) type->tp_doc = desc->doc; members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1); - if (members == NULL) - return; + if (members == NULL) { + PyErr_NoMemory(); + return -1; + } for (i = k = 0; i < n_members; ++i) { if (desc->fields[i].name == PyStructSequence_UnnamedField) @@ -366,22 +369,33 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) type->tp_members = members; if (PyType_Ready(type) < 0) - return; + return -1; Py_INCREF(type); dict = type->tp_dict; #define SET_DICT_FROM_INT(key, value) \ do { \ - PyObject *v = PyLong_FromLong((long) value); \ - if (v != NULL) { \ - PyDict_SetItemString(dict, key, v); \ + v = PyLong_FromLong((long) value); \ + if (v == NULL) \ + return -1; \ + if (PyDict_SetItemString(dict, key, v) < 0) { \ Py_DECREF(v); \ + return -1; \ } \ + Py_DECREF(v); \ } while (0) SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence); SET_DICT_FROM_INT(real_length_key, n_members); SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members); + + return 0; +} + +void +PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) +{ + (void)PyStructSequence_InitType2(type, desc); } PyTypeObject* @@ -390,8 +404,11 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc) PyTypeObject *result; result = (PyTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); - if (result != NULL) { - PyStructSequence_InitType(result, desc); + if (result == NULL) + return NULL; + if (PyStructSequence_InitType2(result, desc) < 0) { + Py_DECREF(result); + return NULL; } return result; } |