|
11 | 11 | #include "Python.h"
|
12 | 12 | #include "_iomodule.h"
|
13 | 13 | #include "pycore_pystate.h" // _PyInterpreterState_GET()
|
| 14 | +#include "pycore_initconfig.h" // _PyStatus_OK() |
14 | 15 |
|
15 | 16 | #ifdef HAVE_SYS_TYPES_H
|
16 | 17 | #include <sys/types.h>
|
@@ -666,12 +667,40 @@ static PyTypeObject* static_types[] = {
|
666 | 667 | };
|
667 | 668 |
|
668 | 669 |
|
| 670 | +PyStatus |
| 671 | +_PyIO_InitTypes(PyInterpreterState *interp) |
| 672 | +{ |
| 673 | + if (!_Py_IsMainInterpreter(interp)) { |
| 674 | + return _PyStatus_OK(); |
| 675 | + } |
| 676 | + |
| 677 | + // Set type base classes |
| 678 | +#ifdef HAVE_WINDOWS_CONSOLE_IO |
| 679 | + PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; |
| 680 | +#endif |
| 681 | + |
| 682 | + for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { |
| 683 | + PyTypeObject *type = static_types[i]; |
| 684 | + if (_PyStaticType_InitBuiltin(type) < 0) { |
| 685 | + return _PyStatus_ERR("Can't initialize builtin type"); |
| 686 | + } |
| 687 | + } |
| 688 | + |
| 689 | + return _PyStatus_OK(); |
| 690 | +} |
| 691 | + |
669 | 692 | void
|
670 |
| -_PyIO_Fini(void) |
| 693 | +_PyIO_FiniTypes(PyInterpreterState *interp) |
671 | 694 | {
|
| 695 | + if (!_Py_IsMainInterpreter(interp)) { |
| 696 | + return; |
| 697 | + } |
| 698 | + |
| 699 | + // Deallocate types in the reverse order to deallocate subclasses before |
| 700 | + // their base classes. |
672 | 701 | for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types) - 1; i >= 0; i--) {
|
673 |
| - PyTypeObject *exc = static_types[i]; |
674 |
| - _PyStaticType_Dealloc(exc); |
| 702 | + PyTypeObject *type = static_types[i]; |
| 703 | + _PyStaticType_Dealloc(type); |
675 | 704 | }
|
676 | 705 | }
|
677 | 706 |
|
@@ -717,11 +746,6 @@ PyInit__io(void)
|
717 | 746 | goto fail;
|
718 | 747 | }
|
719 | 748 |
|
720 |
| - // Set type base classes |
721 |
| -#ifdef HAVE_WINDOWS_CONSOLE_IO |
722 |
| - PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; |
723 |
| -#endif |
724 |
| - |
725 | 749 | // Add types
|
726 | 750 | for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
|
727 | 751 | PyTypeObject *type = static_types[i];
|
|
0 commit comments