Skip to content

Commit 1c8f059

Browse files
committed
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
1 parent 2a54509 commit 1c8f059

File tree

17 files changed

+117
-54
lines changed

17 files changed

+117
-54
lines changed

Include/pythonrun.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ PyAPI_FUNC(void) _PyImport_Init(void);
197197
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
198198
PyAPI_FUNC(void) _PyImportHooks_Init(void);
199199
PyAPI_FUNC(int) _PyFrame_Init(void);
200-
PyAPI_FUNC(void) _PyFloat_Init(void);
200+
PyAPI_FUNC(int) _PyFloat_Init(void);
201201
PyAPI_FUNC(int) PyByteArray_Init(void);
202202
PyAPI_FUNC(void) _PyRandom_Init(void);
203203
#endif

Include/structseq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern char* PyStructSequence_UnnamedField;
2424
#ifndef Py_LIMITED_API
2525
PyAPI_FUNC(void) PyStructSequence_InitType(PyTypeObject *type,
2626
PyStructSequence_Desc *desc);
27+
PyAPI_FUNC(int) PyStructSequence_InitType2(PyTypeObject *type,
28+
PyStructSequence_Desc *desc);
2729
#endif
2830
PyAPI_FUNC(PyTypeObject*) PyStructSequence_NewType(PyStructSequence_Desc *desc);
2931

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #18520: Add a new PyStructSequence_InitType2() function, same than
14+
PyStructSequence_InitType() except that it has a return value (0 on success,
15+
-1 on error).
16+
1317
- Issue #15905: Fix theoretical buffer overflow in handling of sys.argv[0],
1418
prefix and exec_prefix if the operation system does not obey MAXPATHLEN.
1519

Modules/_lsprof.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,12 @@ PyInit__lsprof(void)
884884
PyDict_SetItemString(d, "Profiler", (PyObject *)&PyProfiler_Type);
885885

886886
if (!initialized) {
887-
PyStructSequence_InitType(&StatsEntryType,
888-
&profiler_entry_desc);
889-
PyStructSequence_InitType(&StatsSubEntryType,
890-
&profiler_subentry_desc);
887+
if (PyStructSequence_InitType2(&StatsEntryType,
888+
&profiler_entry_desc) < 0)
889+
return NULL;
890+
if (PyStructSequence_InitType2(&StatsSubEntryType,
891+
&profiler_subentry_desc) < 0)
892+
return NULL;
891893
}
892894
Py_INCREF((PyObject*) &StatsEntryType);
893895
Py_INCREF((PyObject*) &StatsSubEntryType);

Modules/grpmodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ PyInit_grp(void)
210210
if (m == NULL)
211211
return NULL;
212212
d = PyModule_GetDict(m);
213-
if (!initialized)
214-
PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
215-
PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
213+
if (!initialized) {
214+
if (PyStructSequence_InitType2(&StructGrpType,
215+
&struct_group_type_desc) < 0)
216+
return NULL;
217+
}
218+
if (PyDict_SetItemString(d, "struct_group",
219+
(PyObject *)&StructGrpType) < 0)
220+
return NULL;
216221
initialized = 1;
217222
return m;
218223
}

Modules/posixmodule.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11518,19 +11518,23 @@ INITFUNC(void)
1151811518
if (!initialized) {
1151911519
#if defined(HAVE_WAITID) && !defined(__APPLE__)
1152011520
waitid_result_desc.name = MODNAME ".waitid_result";
11521-
PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11521+
if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
11522+
return NULL;
1152211523
#endif
1152311524

1152411525
stat_result_desc.name = MODNAME ".stat_result";
1152511526
stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
1152611527
stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
1152711528
stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11528-
PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11529+
if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
11530+
return NULL;
1152911531
structseq_new = StatResultType.tp_new;
1153011532
StatResultType.tp_new = statresult_new;
1153111533

1153211534
statvfs_result_desc.name = MODNAME ".statvfs_result";
11533-
PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
11535+
if (PyStructSequence_InitType2(&StatVFSResultType,
11536+
&statvfs_result_desc) < 0)
11537+
return NULL;
1153411538
#ifdef NEED_TICKS_PER_SECOND
1153511539
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1153611540
ticks_per_second = sysconf(_SC_CLK_TCK);
@@ -11543,12 +11547,15 @@ INITFUNC(void)
1154311547

1154411548
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
1154511549
sched_param_desc.name = MODNAME ".sched_param";
11546-
PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11550+
if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
11551+
return NULL;
1154711552
SchedParamType.tp_new = sched_param_new;
1154811553
#endif
1154911554

1155011555
/* initialize TerminalSize_info */
11551-
PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
11556+
if (PyStructSequence_InitType2(&TerminalSizeType,
11557+
&TerminalSize_desc) < 0)
11558+
return NULL;
1155211559
}
1155311560
#if defined(HAVE_WAITID) && !defined(__APPLE__)
1155411561
Py_INCREF((PyObject*) &WaitidResultType);
@@ -11566,11 +11573,13 @@ INITFUNC(void)
1156611573
#endif
1156711574

1156811575
times_result_desc.name = MODNAME ".times_result";
11569-
PyStructSequence_InitType(&TimesResultType, &times_result_desc);
11576+
if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
11577+
return NULL;
1157011578
PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
1157111579

1157211580
uname_result_desc.name = MODNAME ".uname_result";
11573-
PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
11581+
if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
11582+
return NULL;
1157411583
PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
1157511584

1157611585
#ifdef __APPLE__
@@ -11648,7 +11657,6 @@ INITFUNC(void)
1164811657
initialized = 1;
1164911658

1165011659
return m;
11651-
1165211660
}
1165311661

1165411662
#ifdef __cplusplus

Modules/pwdmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ PyInit_pwd(void)
216216
return NULL;
217217

218218
if (!initialized) {
219-
PyStructSequence_InitType(&StructPwdType,
220-
&struct_pwd_type_desc);
219+
if (PyStructSequence_InitType2(&StructPwdType,
220+
&struct_pwd_type_desc) < 0)
221+
return NULL;
221222
initialized = 1;
222223
}
223224
Py_INCREF((PyObject *) &StructPwdType);

Modules/resource.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ PyInit_resource(void)
263263
/* Add some symbolic constants to the module */
264264
Py_INCREF(PyExc_OSError);
265265
PyModule_AddObject(m, "error", PyExc_OSError);
266-
if (!initialized)
267-
PyStructSequence_InitType(&StructRUsageType,
268-
&struct_rusage_desc);
266+
if (!initialized) {
267+
if (PyStructSequence_InitType2(&StructRUsageType,
268+
&struct_rusage_desc) < 0)
269+
return NULL;
270+
}
271+
269272
Py_INCREF(&StructRUsageType);
270273
PyModule_AddObject(m, "struct_rusage",
271274
(PyObject*) &StructRUsageType);

Modules/signalmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,10 @@ PyInit_signal(void)
978978
return NULL;
979979

980980
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
981-
if (!initialized)
982-
PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
983-
981+
if (!initialized) {
982+
if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
983+
return NULL;
984+
}
984985
Py_INCREF((PyObject*) &SiginfoType);
985986
PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
986987
initialized = 1;

Modules/spwdmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ PyInit_spwd(void)
196196
m=PyModule_Create(&spwdmodule);
197197
if (m == NULL)
198198
return NULL;
199-
if (!initialized)
200-
PyStructSequence_InitType(&StructSpwdType,
201-
&struct_spwd_type_desc);
199+
if (!initialized) {
200+
if (PyStructSequence_InitType2(&StructSpwdType,
201+
&struct_spwd_type_desc) < 0)
202+
return NULL;
203+
}
202204
Py_INCREF((PyObject *) &StructSpwdType);
203205
PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
204206
initialized = 1;

0 commit comments

Comments
 (0)