[Python-Dev] [Python-checkins] cpython: Issue #18520: Add a new PyStructSequence_InitType2() function, same than (original) (raw)
Benjamin Peterson benjamin at python.org
Tue Jul 23 02:01:46 CEST 2013
- Previous message: [Python-Dev] [Python-checkins] cpython: Issue #18520: Add a new PyStructSequence_InitType2() function, same than
- Next message: [Python-Dev] [Python-checkins] cpython: Issue #18520: Add a new PyStructSequence_InitType2() function, same than
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
We've cheerfully broken the ABI before on minor releases, though if it's part of the stable ABI, we can't be cavaliar about that anymore.
2013/7/22 Victor Stinner <victor.stinner at gmail.com>:
"Add a new PyStructSequenceInitType2()"
I added a new function because I guess that it would break the API (and ABI) to change the return type of a function in a minor release. Tell me if you have a better name than PyStructSequenceInitType2() ;-) "Ex" suffix is usually used when parameters are added. It is not the case here. Victor Le 22 juil. 2013 23:59, "victor.stinner" <python-checkins at python.org> a écrit :
http://hg.python.org/cpython/rev/fc718c177ee6 changeset: 84793:fc718c177ee6 user: Victor Stinner <victor.stinner at gmail.com> date: Mon Jul 22 22:24:54 2013 +0200 summary: Issue #18520: Add a new PyStructSequenceInitType2() function, same than PyStructSequenceInitType() except that it has a return value (0 on success, -1 on error). * PyStructSequenceInitType2() now raises MemoryError on memory allocation failure * Fix also some calls to PyDictSetItemString(): handle error files: Include/pythonrun.h | 2 +- Include/structseq.h | 2 + Misc/NEWS | 4 +++ Modules/lsprof.c | 10 ++++--- Modules/grpmodule.c | 11 ++++++-- Modules/posixmodule.c | 24 ++++++++++++------ Modules/pwdmodule.c | 5 ++- Modules/resource.c | 9 ++++-- Modules/signalmodule.c | 7 +++-- Modules/spwdmodule.c | 8 ++++-- Modules/timemodule.c | 5 ++- Objects/floatobject.c | 9 ++++-- Objects/longobject.c | 6 +++- Objects/structseq.c | 37 +++++++++++++++++++++-------- Python/pythonrun.c | 3 +- Python/sysmodule.c | 23 ++++++++++++----- Python/thread.c | 6 +++- 17 files changed, 117 insertions(+), 54 deletions(-)
diff --git a/Include/pythonrun.h b/Include/pythonrun.h --- a/Include/pythonrun.h +++ b/Include/pythonrun.h @@ -197,7 +197,7 @@ PyAPIFUNC(void) PyExcInit(PyObject * bltinmod); PyAPIFUNC(void) PyImportHooksInit(void); PyAPIFUNC(int) PyFrameInit(void); -PyAPIFUNC(void) PyFloatInit(void); +PyAPIFUNC(int) PyFloatInit(void); PyAPIFUNC(int) PyByteArrayInit(void); PyAPIFUNC(void) PyRandomInit(void); #endif diff --git a/Include/structseq.h b/Include/structseq.h --- a/Include/structseq.h +++ b/Include/structseq.h @@ -24,6 +24,8 @@ #ifndef PyLIMITEDAPI PyAPIFUNC(void) PyStructSequenceInitType(PyTypeObject *type, PyStructSequenceDesc *desc); +PyAPIFUNC(int) PyStructSequenceInitType2(PyTypeObject *type, + PyStructSequenceDesc *desc); #endif PyAPIFUNC(PyTypeObject*) PyStructSequenceNewType(PyStructSequenceDesc *desc); diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,10 @@ Core and Builtins ----------------- +- Issue #18520: Add a new PyStructSequenceInitType2() function, same than + PyStructSequenceInitType() except that it has a return value (0 on success, + -1 on error). + - Issue #15905: Fix theoretical buffer overflow in handling of sys.argv[0], prefix and execprefix if the operation system does not obey MAXPATHLEN. diff --git a/Modules/lsprof.c b/Modules/lsprof.c --- a/Modules/lsprof.c +++ b/Modules/lsprof.c @@ -884,10 +884,12 @@ PyDictSetItemString(d, "Profiler", (PyObject *)&PyProfilerType); if (!initialized) { - PyStructSequenceInitType(&StatsEntryType, - &profilerentrydesc); - PyStructSequenceInitType(&StatsSubEntryType, - &profilersubentrydesc); + if (PyStructSequenceInitType2(&StatsEntryType, + &profilerentrydesc) < 0)_ _+ return NULL;_ _+ if (PyStructSequenceInitType2(&StatsSubEntryType,_ _+ &profilersubentrydesc) < 0)_ _+ return NULL;_ _}_ _PyINCREF((PyObject*) &StatsEntryType);_ _PyINCREF((PyObject*) &StatsSubEntryType);_ _diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c_ _--- a/Modules/grpmodule.c_ _+++ b/Modules/grpmodule.c_ _@@ -210,9 +210,14 @@_ _if (m == NULL)_ _return NULL;_ _d = PyModuleGetDict(m);_ _- if (!initialized)_ _- PyStructSequenceInitType(&StructGrpType,_ _&structgrouptypedesc);_ _- PyDictSetItemString(d, "structgroup", (PyObject *) &StructGrpType);_ _+ if (!initialized) {_ _+ if (PyStructSequenceInitType2(&StructGrpType,_ _+ &structgrouptypedesc) < 0)_ _+ return NULL;_ _+ }_ _+ if (PyDictSetItemString(d, "structgroup",_ _+ (PyObject *)&StructGrpType) < 0)_ _+ return NULL;_ _initialized = 1;_ _return m;_ _}_ _diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c_ _--- a/Modules/posixmodule.c_ _+++ b/Modules/posixmodule.c_ _@@ -11518,19 +11518,23 @@_ _if (!initialized) {_ _#if defined(HAVEWAITID) && !defined(_APPLE_)_ _waitidresultdesc.name = MODNAME ".waitidresult";_ _- PyStructSequenceInitType(&WaitidResultType,_ _&waitidresultdesc);_ _+ if (PyStructSequenceInitType2(&WaitidResultType,_ _&waitidresultdesc) < 0)_ _+ return NULL;_ _#endif_ _statresultdesc.name = MODNAME ".statresult";_ _statresultdesc.fields[7].name = PyStructSequenceUnnamedField;_ _statresultdesc.fields[8].name = PyStructSequenceUnnamedField;_ _statresultdesc.fields[9].name = PyStructSequenceUnnamedField;_ _- PyStructSequenceInitType(&StatResultType, &statresultdesc);_ _+ if (PyStructSequenceInitType2(&StatResultType,_ _&statresultdesc) < 0)_ _+ return NULL;_ _structseqnew = StatResultType.tpnew;_ _StatResultType.tpnew = statresultnew;_ _statvfsresultdesc.name = MODNAME ".statvfsresult";_ _- PyStructSequenceInitType(&StatVFSResultType,_ _&statvfsresultdesc);_ _+ if (PyStructSequenceInitType2(&StatVFSResultType,_ _+ &statvfsresultdesc) < 0)_ _+ return NULL;_ _#ifdef NEEDTICKSPERSECOND_ _# if defined(HAVESYSCONF) && defined(SCCLKTCK)_ _tickspersecond = sysconf(SCCLKTCK);_ _@@ -11543,12 +11547,15 @@_ _#if defined(HAVESCHEDSETPARAM) || defined(HAVESCHEDSETSCHEDULER)_ _schedparamdesc.name = MODNAME ".schedparam";_ _- PyStructSequenceInitType(&SchedParamType, &schedparamdesc);_ _+ if (PyStructSequenceInitType2(&SchedParamType,_ _&schedparamdesc) < 0)_ _+ return NULL;_ _SchedParamType.tpnew = schedparamnew;_ _#endif_ _/* initialize TerminalSizeinfo */_ _- PyStructSequenceInitType(&TerminalSizeType, &TerminalSizedesc);_ _+ if (PyStructSequenceInitType2(&TerminalSizeType,_ _+ &TerminalSizedesc) < 0)_ _+ return NULL;_ _}_ _#if defined(HAVEWAITID) && !defined(_APPLE_)_ _PyINCREF((PyObject*) &WaitidResultType);_ _@@ -11566,11 +11573,13 @@_ _#endif_ _timesresultdesc.name = MODNAME ".timesresult";_ _- PyStructSequenceInitType(&TimesResultType, ×resultdesc);_ _+ if (PyStructSequenceInitType2(&TimesResultType, ×resultdesc)_ _< 0)_ _+ return NULL;_ _PyModuleAddObject(m, "timesresult", (PyObject *)&TimesResultType);_ _unameresultdesc.name = MODNAME ".unameresult";_ _- PyStructSequenceInitType(&UnameResultType, &unameresultdesc);_ _+ if (PyStructSequenceInitType2(&UnameResultType, &unameresultdesc)_ _< 0)_ _+ return NULL;_ _PyModuleAddObject(m, "unameresult", (PyObject *)&UnameResultType);_ _#ifdef _APPLE__ _@@ -11648,7 +11657,6 @@_ _initialized = 1;_ _return m;_ _-_ _}_ _#ifdef _cplusplus_ _diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c_ _--- a/Modules/pwdmodule.c_ _+++ b/Modules/pwdmodule.c_ _@@ -216,8 +216,9 @@_ _return NULL;_ _if (!initialized) {_ _- PyStructSequenceInitType(&StructPwdType,_ _- &structpwdtypedesc);_ _+ if (PyStructSequenceInitType2(&StructPwdType,_ _+ &structpwdtypedesc) < 0)_ _+ return NULL;_ _initialized = 1;_ _}_ _PyINCREF((PyObject *) &StructPwdType);_ _diff --git a/Modules/resource.c b/Modules/resource.c_ _--- a/Modules/resource.c_ _+++ b/Modules/resource.c_ _@@ -263,9 +263,12 @@_ _/* Add some symbolic constants to the module */_ _PyINCREF(PyExcOSError);_ _PyModuleAddObject(m, "error", PyExcOSError);_ _- if (!initialized)_ _- PyStructSequenceInitType(&StructRUsageType,_ _- &structrusagedesc);_ _+ if (!initialized) {_ _+ if (PyStructSequenceInitType2(&StructRUsageType,_ _+ &structrusagedesc) < 0)_ _+ return NULL;_ _+ }_ _+_ _PyINCREF(&StructRUsageType);_ _PyModuleAddObject(m, "structrusage",_ _(PyObject*) &StructRUsageType);_ _diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c_ _--- a/Modules/signalmodule.c_ _+++ b/Modules/signalmodule.c_ _@@ -978,9 +978,10 @@_ _return NULL;_ _#if defined(HAVESIGWAITINFO) || defined(HAVESIGTIMEDWAIT)_ _- if (!initialized)_ _- PyStructSequenceInitType(&SiginfoType, &structsiginfodesc);_ _-_ _+ if (!initialized) {_ _+ if (PyStructSequenceInitType2(&SiginfoType,_ _&structsiginfodesc) < 0)_ _+ return NULL;_ _+ }_ _PyINCREF((PyObject*) &SiginfoType);_ _PyModuleAddObject(m, "structsiginfo", (PyObject*) &SiginfoType);_ _initialized = 1;_ _diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c_ _--- a/Modules/spwdmodule.c_ _+++ b/Modules/spwdmodule.c_ _@@ -196,9 +196,11 @@_ _m=PyModuleCreate(&spwdmodule);_ _if (m == NULL)_ _return NULL;_ _- if (!initialized)_ _- PyStructSequenceInitType(&StructSpwdType,_ _- &structspwdtypedesc);_ _+ if (!initialized) {_ _+ if (PyStructSequenceInitType2(&StructSpwdType,_ _+ &structspwdtypedesc) < 0)_ _+ return NULL;_ _+ }_ _PyINCREF((PyObject *) &StructSpwdType);_ _PyModuleAddObject(m, "structspwd", (PyObject *) &StructSpwdType);_ _initialized = 1;_ _diff --git a/Modules/timemodule.c b/Modules/timemodule.c_ _--- a/Modules/timemodule.c_ _+++ b/Modules/timemodule.c_ _@@ -1476,8 +1476,9 @@_ _PyInittimezone(m);_ _if (!initialized) {_ _- PyStructSequenceInitType(&StructTimeType,_ _- &structtimetypedesc);_ _+ if (PyStructSequenceInitType2(&StructTimeType,_ _+ &structtimetypedesc) < 0)_ _+ return NULL;_ _#ifdef MSWINDOWS_ _winver.dwOSVersionInfoSize = sizeof(winver);_ _diff --git a/Objects/floatobject.c b/Objects/floatobject.c_ _--- a/Objects/floatobject.c_ _+++ b/Objects/floatobject.c_ _@@ -1853,7 +1853,7 @@_ _floatnew, /* tpnew */_ _};_ _-void_ _+int_ _PyFloatInit(void)_ _{_ _/* We attempt to determine if this machine is using IEEE_ _@@ -1903,8 +1903,11 @@_ _floatformat = detectedfloatformat;_ _/* Init float info */_ _- if (FloatInfoType.tpname == 0)_ _- PyStructSequenceInitType(&FloatInfoType, &floatinfodesc);_ _+ if (FloatInfoType.tpname == NULL) {_ _+ if (PyStructSequenceInitType2(&FloatInfoType, &floatinfodesc) <_ _0)_ _+ return 0;_ _+ }_ _+ return 1;_ _}_ _int_ _diff --git a/Objects/longobject.c b/Objects/longobject.c_ _--- a/Objects/longobject.c_ _+++ b/Objects/longobject.c_ _@@ -5059,8 +5059,10 @@_ _}_ _#endif_ _/* initialize intinfo */_ _- if (IntInfoType.tpname == 0)_ _- PyStructSequenceInitType(&IntInfoType, &intinfodesc);_ _+ if (IntInfoType.tpname == NULL) {_ _+ if (PyStructSequenceInitType2(&IntInfoType, &intinfodesc) <_ _0)_ _+ return 0;_ _+ }_ _return 1;_ _}_ _diff --git a/Objects/structseq.c b/Objects/structseq.c_ _--- a/Objects/structseq.c_ _+++ b/Objects/structseq.c_ _@@ -320,12 +320,13 @@_ _structseqnew, /* tpnew */_ _};_ _-void_ _-PyStructSequenceInitType(PyTypeObject *type, PyStructSequenceDesc_ _*desc)_ _+int_ _+PyStructSequenceInitType2(PyTypeObject *type, PyStructSequenceDesc_ _*desc)_ _{_ _PyObject *dict;_ _PyMemberDef* members;_ _int nmembers, nunnamedmembers, i, k;_ _+ PyObject *v;_ _#ifdef PyTRACEREFS_ _/* if the type object was chained, unchain it first_ _@@ -347,8 +348,10 @@_ _type->tpdoc = desc->doc; members = PyMemNEW(PyMemberDef, nmembers-nunnamedmembers+1); - if (members == NULL) - return; + if (members == NULL) { + PyErrNoMemory(); + return -1; + } for (i = k = 0; i < nmembers; ++i) {_ _if (desc->fields[i].name == PyStructSequenceUnnamedField) @@ -366,22 +369,33 @@ type->tpmembers = members; if (PyTypeReady(type) < 0)_ _- return;_ _+ return -1;_ _PyINCREF(type);_ _dict = type->tpdict; _#define SETDICTFROMINT(key, value) _ _do { _ _- PyObject *v = PyLongFromLong((long) value); _ _- if (v != NULL) { _ _- PyDictSetItemString(dict, key, v); _ _+ v = PyLongFromLong((long) value); _ _+ if (v == NULL) _ _+ return -1; _ + if (PyDictSetItemString(dict, key, v) < 0) { _ _PyDECREF(v); _ _+ return -1; _ _} _ _+ PyDECREF(v); _ _} while (0)_ _SETDICTFROMINT(visiblelengthkey, desc->ninsequence); SETDICTFROMINT(reallengthkey, nmembers); SETDICTFROMINT(unnamedfieldskey, nunnamedmembers); + + return 0; +} + +void +PyStructSequenceInitType(PyTypeObject *type, PyStructSequenceDesc *desc) +{ + (void)PyStructSequenceInitType2(type, desc); } PyTypeObject* @@ -390,8 +404,11 @@ PyTypeObject *result; result = (PyTypeObject*)PyTypeGenericAlloc(&PyTypeType, 0); - if (result != NULL) { - PyStructSequenceInitType(result, desc); + if (result == NULL) + return NULL; + if (PyStructSequenceInitType2(result, desc) < 0) {_ _+ PyDECREF(result);_ _+ return NULL;_ _}_ _return result;_ _}_ _diff --git a/Python/pythonrun.c b/Python/pythonrun.c_ _--- a/Python/pythonrun.c_ _+++ b/Python/pythonrun.c_ _@@ -328,7 +328,8 @@_ _if (!PyByteArrayInit())_ _PyFatalError("PyInitialize: can't init bytearray");_ _- PyFloatInit();_ _+ if (!PyFloatInit())_ _+ PyFatalError("PyInitialize: can't init float");_ _interp->modules = PyDictNew(); if (interp->modules == NULL) diff --git a/Python/sysmodule.c b/Python/sysmodule.c --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1634,8 +1634,10 @@ SETSYSFROMSTRING("intinfo", PyLongGetInfo()); /* initialize hashinfo */ - if (HashInfoType.tpname == 0) - PyStructSequenceInitType(&HashInfoType, &hashinfodesc); + if (HashInfoType.tpname == NULL) { + if (PyStructSequenceInitType2(&HashInfoType, &hashinfodesc) < 0) + return NULL; + } SETSYSFROMSTRING("hashinfo", gethashinfo()); SETSYSFROMSTRING("maxunicode", @@ -1676,8 +1678,11 @@ } /* versioninfo */ - if (VersionInfoType.tpname == 0) - PyStructSequenceInitType(&VersionInfoType, &versioninfodesc); + if (VersionInfoType.tpname == NULL) { + if (PyStructSequenceInitType2(&VersionInfoType, + &versioninfodesc) < 0) + return NULL; + } versioninfo = makeversioninfo(); SETSYSFROMSTRING("versioninfo", versioninfo); /* prevent user from creating new instances */ @@ -1688,8 +1693,10 @@ SETSYSFROMSTRING("implementation", makeimplinfo(versioninfo)); /* flags */ - if (FlagsType.tpname == 0) - PyStructSequenceInitType(&FlagsType, &flagsdesc); + if (FlagsType.tpname == 0) { + if (PyStructSequenceInitType2(&FlagsType, &flagsdesc) < 0) + return NULL; + } SETSYSFROMSTRING("flags", makeflags()); /* prevent user from creating new instances */ FlagsType.tpinit = NULL; @@ -1699,7 +1706,9 @@ #if defined(MSWINDOWS) /* getwindowsversion */ if (WindowsVersionType.tpname == 0) - PyStructSequenceInitType(&WindowsVersionType, &windowsversiondesc); + if (PyStructSequenceInitType2(&WindowsVersionType, + &windowsversiondesc) < 0) + return NULL; /* prevent user from creating new instances */ WindowsVersionType.tpinit = NULL; WindowsVersionType.tpnew = NULL; diff --git a/Python/thread.c b/Python/thread.c --- a/Python/thread.c +++ b/Python/thread.c @@ -399,8 +399,10 @@ int len; #endif - if (ThreadInfoType.tpname == 0) - PyStructSequenceInitType(&ThreadInfoType, &threadinfodesc); + if (ThreadInfoType.tpname == 0) { + if (PyStructSequenceInitType2(&ThreadInfoType, &threadinfodesc) < 0) + return NULL; + } threadinfo = PyStructSequenceNew(&ThreadInfoType); if (threadinfo == NULL) -- Repository URL: http://hg.python.org/cpython
Python-checkins mailing list Python-checkins at python.org http://mail.python.org/mailman/listinfo/python-checkins
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/benjamin%40python.org
-- Regards, Benjamin
- Previous message: [Python-Dev] [Python-checkins] cpython: Issue #18520: Add a new PyStructSequence_InitType2() function, same than
- Next message: [Python-Dev] [Python-checkins] cpython: Issue #18520: Add a new PyStructSequence_InitType2() function, same than
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]