Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 45f7008

Browse filesBrowse files
authored
bpo-1635741: Port resource extension module to multiphase initialization (PEP 489) (GH-19252)
Fix also reference leaks on error.
1 parent 8ec7cb5 commit 45f7008
Copy full SHA for 45f7008

File tree

2 files changed

+73
-53
lines changed
Filter options

2 files changed

+73
-53
lines changed
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port :mod:`resource` to multiphase initialization (:pep:`489`).

‎Modules/resource.c

Copy file name to clipboardExpand all lines: Modules/resource.c
+72-53Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -340,155 +340,174 @@ resource_methods[] = {
340340
/* Module initialization */
341341

342342

343-
static struct PyModuleDef resourcemodule = {
344-
PyModuleDef_HEAD_INIT,
345-
"resource",
346-
NULL,
347-
-1,
348-
resource_methods,
349-
NULL,
350-
NULL,
351-
NULL,
352-
NULL
353-
};
354-
355-
PyMODINIT_FUNC
356-
PyInit_resource(void)
343+
static int
344+
resource_exec(PyObject *module)
357345
{
358-
PyObject *m, *v;
359-
360-
/* Create the module and add the functions */
361-
m = PyModule_Create(&resourcemodule);
362-
if (m == NULL)
363-
return NULL;
346+
#define ADD_INT(module, value) \
347+
do { \
348+
if (PyModule_AddIntConstant(module, #value, value) < 0) { \
349+
return -1; \
350+
} \
351+
} while (0)
364352

365353
/* Add some symbolic constants to the module */
366354
Py_INCREF(PyExc_OSError);
367-
PyModule_AddObject(m, "error", PyExc_OSError);
355+
if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) {
356+
Py_DECREF(PyExc_OSError);
357+
return -1;
358+
}
368359
if (!initialized) {
369360
if (PyStructSequence_InitType2(&StructRUsageType,
370361
&struct_rusage_desc) < 0)
371-
return NULL;
362+
return -1;
372363
}
373364

374-
Py_INCREF(&StructRUsageType);
375-
PyModule_AddObject(m, "struct_rusage",
376-
(PyObject*) &StructRUsageType);
365+
if(PyModule_AddType(module, &StructRUsageType) < 0) {
366+
return -1;
367+
}
377368

378369
/* insert constants */
379370
#ifdef RLIMIT_CPU
380-
PyModule_AddIntMacro(m, RLIMIT_CPU);
371+
ADD_INT(module, RLIMIT_CPU);
381372
#endif
382373

383374
#ifdef RLIMIT_FSIZE
384-
PyModule_AddIntMacro(m, RLIMIT_FSIZE);
375+
ADD_INT(module, RLIMIT_FSIZE);
385376
#endif
386377

387378
#ifdef RLIMIT_DATA
388-
PyModule_AddIntMacro(m, RLIMIT_DATA);
379+
ADD_INT(module, RLIMIT_DATA);
389380
#endif
390381

391382
#ifdef RLIMIT_STACK
392-
PyModule_AddIntMacro(m, RLIMIT_STACK);
383+
ADD_INT(module, RLIMIT_STACK);
393384
#endif
394385

395386
#ifdef RLIMIT_CORE
396-
PyModule_AddIntMacro(m, RLIMIT_CORE);
387+
ADD_INT(module, RLIMIT_CORE);
397388
#endif
398389

399390
#ifdef RLIMIT_NOFILE
400-
PyModule_AddIntMacro(m, RLIMIT_NOFILE);
391+
ADD_INT(module, RLIMIT_NOFILE);
401392
#endif
402393

403394
#ifdef RLIMIT_OFILE
404-
PyModule_AddIntMacro(m, RLIMIT_OFILE);
395+
ADD_INT(module, RLIMIT_OFILE);
405396
#endif
406397

407398
#ifdef RLIMIT_VMEM
408-
PyModule_AddIntMacro(m, RLIMIT_VMEM);
399+
ADD_INT(module, RLIMIT_VMEM);
409400
#endif
410401

411402
#ifdef RLIMIT_AS
412-
PyModule_AddIntMacro(m, RLIMIT_AS);
403+
ADD_INT(module, RLIMIT_AS);
413404
#endif
414405

415406
#ifdef RLIMIT_RSS
416-
PyModule_AddIntMacro(m, RLIMIT_RSS);
407+
ADD_INT(module, RLIMIT_RSS);
417408
#endif
418409

419410
#ifdef RLIMIT_NPROC
420-
PyModule_AddIntMacro(m, RLIMIT_NPROC);
411+
ADD_INT(module, RLIMIT_NPROC);
421412
#endif
422413

423414
#ifdef RLIMIT_MEMLOCK
424-
PyModule_AddIntMacro(m, RLIMIT_MEMLOCK);
415+
ADD_INT(module, RLIMIT_MEMLOCK);
425416
#endif
426417

427418
#ifdef RLIMIT_SBSIZE
428-
PyModule_AddIntMacro(m, RLIMIT_SBSIZE);
419+
ADD_INT(module, RLIMIT_SBSIZE);
429420
#endif
430421

431422
/* Linux specific */
432423
#ifdef RLIMIT_MSGQUEUE
433-
PyModule_AddIntMacro(m, RLIMIT_MSGQUEUE);
424+
ADD_INT(module, RLIMIT_MSGQUEUE);
434425
#endif
435426

436427
#ifdef RLIMIT_NICE
437-
PyModule_AddIntMacro(m, RLIMIT_NICE);
428+
ADD_INT(module, RLIMIT_NICE);
438429
#endif
439430

440431
#ifdef RLIMIT_RTPRIO
441-
PyModule_AddIntMacro(m, RLIMIT_RTPRIO);
432+
ADD_INT(module, RLIMIT_RTPRIO);
442433
#endif
443434

444435
#ifdef RLIMIT_RTTIME
445-
PyModule_AddIntMacro(m, RLIMIT_RTTIME);
436+
ADD_INT(module, RLIMIT_RTTIME);
446437
#endif
447438

448439
#ifdef RLIMIT_SIGPENDING
449-
PyModule_AddIntMacro(m, RLIMIT_SIGPENDING);
440+
ADD_INT(module, RLIMIT_SIGPENDING);
450441
#endif
451442

452443
/* target */
453444
#ifdef RUSAGE_SELF
454-
PyModule_AddIntMacro(m, RUSAGE_SELF);
445+
ADD_INT(module, RUSAGE_SELF);
455446
#endif
456447

457448
#ifdef RUSAGE_CHILDREN
458-
PyModule_AddIntMacro(m, RUSAGE_CHILDREN);
449+
ADD_INT(module, RUSAGE_CHILDREN);
459450
#endif
460451

461452
#ifdef RUSAGE_BOTH
462-
PyModule_AddIntMacro(m, RUSAGE_BOTH);
453+
ADD_INT(module, RUSAGE_BOTH);
463454
#endif
464455

465456
#ifdef RUSAGE_THREAD
466-
PyModule_AddIntMacro(m, RUSAGE_THREAD);
457+
ADD_INT(module, RUSAGE_THREAD);
467458
#endif
468459

469460
/* FreeBSD specific */
470461

471462
#ifdef RLIMIT_SWAP
472-
PyModule_AddIntMacro(m, RLIMIT_SWAP);
463+
ADD_INT(module, RLIMIT_SWAP);
473464
#endif
474465

475466
#ifdef RLIMIT_SBSIZE
476-
PyModule_AddIntMacro(m, RLIMIT_SBSIZE);
467+
ADD_INT(module, RLIMIT_SBSIZE);
477468
#endif
478469

479470
#ifdef RLIMIT_NPTS
480-
PyModule_AddIntMacro(m, RLIMIT_NPTS);
471+
ADD_INT(module, RLIMIT_NPTS);
481472
#endif
482473

474+
PyObject *v;
483475
if (sizeof(RLIM_INFINITY) > sizeof(long)) {
484476
v = PyLong_FromLongLong((long long) RLIM_INFINITY);
485477
} else
486478
{
487479
v = PyLong_FromLong((long) RLIM_INFINITY);
488480
}
489-
if (v) {
490-
PyModule_AddObject(m, "RLIM_INFINITY", v);
481+
if (!v) {
482+
return -1;
483+
}
484+
485+
if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) {
486+
Py_DECREF(v);
487+
return -1;
491488
}
489+
492490
initialized = 1;
493-
return m;
491+
return 0;
492+
493+
#undef ADD_INT
494+
}
495+
496+
static struct PyModuleDef_Slot resource_slots[] = {
497+
{Py_mod_exec, resource_exec},
498+
{0, NULL}
499+
};
500+
501+
static struct PyModuleDef resourcemodule = {
502+
PyModuleDef_HEAD_INIT,
503+
.m_name = "resource",
504+
.m_size = 0,
505+
.m_methods = resource_methods,
506+
.m_slots = resource_slots,
507+
};
508+
509+
PyMODINIT_FUNC
510+
PyInit_resource(void)
511+
{
512+
return PyModuleDef_Init(&resourcemodule);
494513
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.