From 553159664cd8ca1b637c1c2be0e99c47afe62736 Mon Sep 17 00:00:00 2001 From: xiejunyi Date: Mon, 22 Mar 2021 18:21:55 +0800 Subject: [PATCH] fix boolobject.c static variables under building under building Python with --with-experimental-isolated-subinterpreters may cause crash --- Objects/boolobject.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Objects/boolobject.c b/Objects/boolobject.c index b786966533e1d6..0601c47eb3535b 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -4,22 +4,25 @@ #include "longintrepr.h" /* We define bool_repr to return "False" or "True" */ - -static PyObject *false_str = NULL; -static PyObject *true_str = NULL; - static PyObject * bool_repr(PyObject *self) { + _Py_IDENTIFIER(True); + _Py_IDENTIFIER(False); + PyObject *s; if (self == Py_True) - s = true_str ? true_str : - (true_str = PyUnicode_InternFromString("True")); - else - s = false_str ? false_str : - (false_str = PyUnicode_InternFromString("False")); - Py_XINCREF(s); + { + s = _PyUnicode_FromId(&PyId_True); // borrowed ref + } else { + s = _PyUnicode_FromId(&PyId_False); // borrowed ref + } + + if (s != NULL) { + return Py_NewRef(s); + } + return s; }