churchyard / rpms / python38

Forked from rpms/python38 3 years ago
Clone
d623e92
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
d623e92
index 847b50140a6..570dc3ed6fe 100644
d623e92
--- a/Doc/whatsnew/3.6.rst
d623e92
+++ b/Doc/whatsnew/3.6.rst
d623e92
@@ -1852,10 +1852,10 @@ Build and C API Changes
d623e92
 * The :c:func:`PyUnicode_FSConverter` and :c:func:`PyUnicode_FSDecoder`
d623e92
   functions will now accept :term:`path-like objects <path-like object>`.
d623e92
 
d623e92
-* The ``PyExc_RecursionErrorInst`` singleton that was part of the public API
d623e92
-  has been removed as its members being never cleared may cause a segfault
d623e92
-  during finalization of the interpreter. Contributed by Xavier de Gaye in
d623e92
-  :issue:`22898` and :issue:`30697`.
d623e92
+* The ``PyExc_RecursionErrorInst`` singleton is not used anymore as its members
d623e92
+  being never cleared may cause a segfault during finalization of the
d623e92
+  interpreter. Contributed by Xavier de Gaye in :issue:`22898` and
d623e92
+  :issue:`30697`.
d623e92
 
d623e92
 
d623e92
 Other Improvements
d623e92
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
d623e92
index c28c1373f82..8c1dbc5047b 100644
d623e92
--- a/Include/pyerrors.h
d623e92
+++ b/Include/pyerrors.h
d623e92
@@ -219,6 +219,8 @@ PyAPI_DATA(PyObject *) PyExc_IOError;
d623e92
 PyAPI_DATA(PyObject *) PyExc_WindowsError;
d623e92
 #endif
d623e92
 
d623e92
+PyAPI_DATA(PyObject *) PyExc_RecursionErrorInst;
d623e92
+
d623e92
 /* Predefined warning categories */
d623e92
 PyAPI_DATA(PyObject *) PyExc_Warning;
d623e92
 PyAPI_DATA(PyObject *) PyExc_UserWarning;
d623e92
diff --git a/Misc/NEWS.d/next/C API/2017-12-20-15-23-06.bpo-30697.v9FmgG.rst b/Misc/NEWS.d/next/C API/2017-12-20-15-23-06.bpo-30697.v9FmgG.rst
d623e92
new file mode 100644
d623e92
index 00000000000..28f74ad4f30
d623e92
--- /dev/null
d623e92
+++ b/Misc/NEWS.d/next/C API/2017-12-20-15-23-06.bpo-30697.v9FmgG.rst	
d623e92
@@ -0,0 +1 @@
d623e92
+Restore PyExc_RecursionErrorInst in 3.6
d623e92
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
d623e92
index df4899372a5..271e293e325 100644
d623e92
--- a/Objects/exceptions.c
d623e92
+++ b/Objects/exceptions.c
d623e92
@@ -2430,6 +2430,12 @@ SimpleExtendsException(PyExc_Warning, ResourceWarning,
d623e92
 
d623e92
 
d623e92
 
d623e92
+/* Pre-computed RecursionError instance for when recursion depth is reached.
d623e92
+   Meant to be used when normalizing the exception for exceeding the recursion
d623e92
+   depth will cause its own infinite recursion.
d623e92
+*/
d623e92
+PyObject *PyExc_RecursionErrorInst = NULL;
d623e92
+
d623e92
 #define PRE_INIT(TYPE) \
d623e92
     if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
d623e92
         if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
d623e92
@@ -2691,11 +2697,37 @@ _PyExc_Init(PyObject *bltinmod)
d623e92
     ADD_ERRNO(TimeoutError, ETIMEDOUT);
d623e92
 
d623e92
     preallocate_memerrors();
d623e92
+
d623e92
+    if (!PyExc_RecursionErrorInst) {
d623e92
+        PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RecursionError, NULL, NULL);
d623e92
+        if (!PyExc_RecursionErrorInst)
d623e92
+            Py_FatalError("Cannot pre-allocate RecursionError instance for "
d623e92
+                            "recursion errors");
d623e92
+        else {
d623e92
+            PyBaseExceptionObject *err_inst =
d623e92
+                (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
d623e92
+            PyObject *args_tuple;
d623e92
+            PyObject *exc_message;
d623e92
+            exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
d623e92
+            if (!exc_message)
d623e92
+                Py_FatalError("cannot allocate argument for RecursionError "
d623e92
+                                "pre-allocation");
d623e92
+            args_tuple = PyTuple_Pack(1, exc_message);
d623e92
+            if (!args_tuple)
d623e92
+                Py_FatalError("cannot allocate tuple for RecursionError "
d623e92
+                                "pre-allocation");
d623e92
+            Py_DECREF(exc_message);
d623e92
+            if (BaseException_init(err_inst, args_tuple, NULL))
d623e92
+                Py_FatalError("init of pre-allocated RecursionError failed");
d623e92
+            Py_DECREF(args_tuple);
d623e92
+        }
d623e92
+    }
d623e92
 }
d623e92
 
d623e92
 void
d623e92
 _PyExc_Fini(void)
d623e92
 {
d623e92
+    Py_CLEAR(PyExc_RecursionErrorInst);
d623e92
     free_preallocated_memerrors();
d623e92
     Py_CLEAR(errnomap);
d623e92
 }
d623e92
diff --git a/PC/python3.def b/PC/python3.def
d623e92
index 4fc4a6814ee..ff70718fc37 100644
d623e92
--- a/PC/python3.def
d623e92
+++ b/PC/python3.def
d623e92
@@ -224,6 +224,7 @@ EXPORTS
d623e92
   PyExc_PermissionError=python36.PyExc_PermissionError DATA
d623e92
   PyExc_ProcessLookupError=python36.PyExc_ProcessLookupError DATA
d623e92
   PyExc_RecursionError=python36.PyExc_RecursionError DATA
d623e92
+  PyExc_RecursionErrorInst=python36.PyExc_RecursionErrorInst DATA
d623e92
   PyExc_ReferenceError=python36.PyExc_ReferenceError DATA
d623e92
   PyExc_ResourceWarning=python36.PyExc_ResourceWarning DATA
d623e92
   PyExc_RuntimeError=python36.PyExc_RuntimeError DATA