Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc1b6f0

Browse files
committed
Fix the _io module leaking references when a sub-interpreter is created.
1 parent 1c7ade5 commit fc1b6f0

1 file changed

Lines changed: 36 additions & 48 deletions

File tree

Modules/_io/_iomodule.c

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -741,58 +741,46 @@ PyInit__io(void)
741741
ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
742742

743743
/* Interned strings */
744-
if (!(_PyIO_str_close = PyUnicode_InternFromString("close")))
744+
#define ADD_INTERNED(name) \
745+
if (!_PyIO_str_ ## name && \
746+
!(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
745747
goto fail;
746-
if (!(_PyIO_str_closed = PyUnicode_InternFromString("closed")))
747-
goto fail;
748-
if (!(_PyIO_str_decode = PyUnicode_InternFromString("decode")))
749-
goto fail;
750-
if (!(_PyIO_str_encode = PyUnicode_InternFromString("encode")))
751-
goto fail;
752-
if (!(_PyIO_str_fileno = PyUnicode_InternFromString("fileno")))
753-
goto fail;
754-
if (!(_PyIO_str_flush = PyUnicode_InternFromString("flush")))
755-
goto fail;
756-
if (!(_PyIO_str_getstate = PyUnicode_InternFromString("getstate")))
757-
goto fail;
758-
if (!(_PyIO_str_isatty = PyUnicode_InternFromString("isatty")))
759-
goto fail;
760-
if (!(_PyIO_str_newlines = PyUnicode_InternFromString("newlines")))
761-
goto fail;
762-
if (!(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
763-
goto fail;
764-
if (!(_PyIO_str_read = PyUnicode_InternFromString("read")))
765-
goto fail;
766-
if (!(_PyIO_str_read1 = PyUnicode_InternFromString("read1")))
767-
goto fail;
768-
if (!(_PyIO_str_readable = PyUnicode_InternFromString("readable")))
769-
goto fail;
770-
if (!(_PyIO_str_readinto = PyUnicode_InternFromString("readinto")))
771-
goto fail;
772-
if (!(_PyIO_str_readline = PyUnicode_InternFromString("readline")))
773-
goto fail;
774-
if (!(_PyIO_str_reset = PyUnicode_InternFromString("reset")))
775-
goto fail;
776-
if (!(_PyIO_str_seek = PyUnicode_InternFromString("seek")))
777-
goto fail;
778-
if (!(_PyIO_str_seekable = PyUnicode_InternFromString("seekable")))
779-
goto fail;
780-
if (!(_PyIO_str_setstate = PyUnicode_InternFromString("setstate")))
781-
goto fail;
782-
if (!(_PyIO_str_tell = PyUnicode_InternFromString("tell")))
783-
goto fail;
784-
if (!(_PyIO_str_truncate = PyUnicode_InternFromString("truncate")))
785-
goto fail;
786-
if (!(_PyIO_str_write = PyUnicode_InternFromString("write")))
787-
goto fail;
788-
if (!(_PyIO_str_writable = PyUnicode_InternFromString("writable")))
748+
749+
ADD_INTERNED(close)
750+
ADD_INTERNED(closed)
751+
ADD_INTERNED(decode)
752+
ADD_INTERNED(encode)
753+
ADD_INTERNED(fileno)
754+
ADD_INTERNED(flush)
755+
ADD_INTERNED(getstate)
756+
ADD_INTERNED(isatty)
757+
ADD_INTERNED(newlines)
758+
ADD_INTERNED(read)
759+
ADD_INTERNED(read1)
760+
ADD_INTERNED(readable)
761+
ADD_INTERNED(readinto)
762+
ADD_INTERNED(readline)
763+
ADD_INTERNED(reset)
764+
ADD_INTERNED(seek)
765+
ADD_INTERNED(seekable)
766+
ADD_INTERNED(setstate)
767+
ADD_INTERNED(tell)
768+
ADD_INTERNED(truncate)
769+
ADD_INTERNED(write)
770+
ADD_INTERNED(writable)
771+
772+
if (!_PyIO_str_nl &&
773+
!(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
789774
goto fail;
790-
791-
if (!(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
775+
776+
if (!_PyIO_empty_str &&
777+
!(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
792778
goto fail;
793-
if (!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
779+
if (!_PyIO_empty_bytes &&
780+
!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
794781
goto fail;
795-
if (!(_PyIO_zero = PyLong_FromLong(0L)))
782+
if (!_PyIO_zero &&
783+
!(_PyIO_zero = PyLong_FromLong(0L)))
796784
goto fail;
797785

798786
state->initialized = 1;

0 commit comments

Comments
 (0)