[Python-checkins] bpo-33818: PyExceptionClass_Name() will now return "const char *". (GH-7581)

Serhiy Storchaka webhook-mailer at python.org
Fri Jun 15 04:09:47 EDT 2018


https://github.com/python/cpython/commit/ceeef10cdbc08561f9954e13bbed1cb2299a8c72
commit: ceeef10cdbc08561f9954e13bbed1cb2299a8c72
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-06-15T11:09:43+03:00
summary:

bpo-33818: PyExceptionClass_Name() will now return "const char *". (GH-7581)

files:
A Misc/NEWS.d/next/C API/2018-06-10-09-42-31.bpo-33818.50nlf3.rst
M Doc/whatsnew/3.8.rst
M Include/pyerrors.h
M Objects/exceptions.c
M Python/errors.c
M Python/pythonrun.c

diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index 9ae52c241f11..10fa182faeb3 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -115,9 +115,13 @@ Optimizations
   first introduced in Python 3.4.  It offers better performance and smaller
   size compared to Protocol 3 available since Python 3.0.
 
+
 Build and C API Changes
 =======================
 
+* The result of :c:func:`PyExceptionClass_Name` is now of type
+  ``const char *`` rather of ``char *``.
+  (Contributed by Serhiy Storchaka in :issue:`33818`.)
 
 
 Deprecated
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 5b626584bb25..416d750d9b1f 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -140,10 +140,9 @@ PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
 #define PyExceptionInstance_Check(x)                    \
     PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
 
-PyAPI_FUNC(char *) PyExceptionClass_Name(PyObject *);
+PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
 #ifndef Py_LIMITED_API
-#define PyExceptionClass_Name(x) \
-     ((char *)(((PyTypeObject *)(x))->tp_name))
+#define PyExceptionClass_Name(x)  (((PyTypeObject*)(x))->tp_name)
 #endif
 
 #define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
diff --git a/Misc/NEWS.d/next/C API/2018-06-10-09-42-31.bpo-33818.50nlf3.rst b/Misc/NEWS.d/next/C API/2018-06-10-09-42-31.bpo-33818.50nlf3.rst
new file mode 100644
index 000000000000..0f30a6e380ef
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2018-06-10-09-42-31.bpo-33818.50nlf3.rst	
@@ -0,0 +1,2 @@
+:c:func:`PyExceptionClass_Name` will now return ``const char *`` instead of
+``char *``.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index e1615b2a8f3a..bb50c1c36964 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -344,10 +344,10 @@ PyException_SetContext(PyObject *self, PyObject *context)
 
 #undef PyExceptionClass_Name
 
-char *
+const char *
 PyExceptionClass_Name(PyObject *ob)
 {
-    return (char *)((PyTypeObject*)ob)->tp_name;
+    return ((PyTypeObject*)ob)->tp_name;
 }
 
 static struct PyMemberDef BaseException_members[] = {
diff --git a/Python/errors.c b/Python/errors.c
index 15e6ba057143..98910b449087 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -947,7 +947,7 @@ PyErr_WriteUnraisable(PyObject *obj)
     _Py_IDENTIFIER(__module__);
     PyObject *f, *t, *v, *tb;
     PyObject *moduleName = NULL;
-    char* className;
+    const char *className;
 
     PyErr_Fetch(&t, &v, &tb);
 
@@ -977,7 +977,7 @@ PyErr_WriteUnraisable(PyObject *obj)
     assert(PyExceptionClass_Check(t));
     className = PyExceptionClass_Name(t);
     if (className != NULL) {
-        char *dot = strrchr(className, '.');
+        const char *dot = strrchr(className, '.');
         if (dot != NULL)
             className = dot+1;
     }
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 26f74c80d032..3d40c79bc1be 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -774,12 +774,12 @@ print_exception(PyObject *f, PyObject *value)
     }
     else {
         PyObject* moduleName;
-        char* className;
+        const char *className;
         _Py_IDENTIFIER(__module__);
         assert(PyExceptionClass_Check(type));
         className = PyExceptionClass_Name(type);
         if (className != NULL) {
-            char *dot = strrchr(className, '.');
+            const char *dot = strrchr(className, '.');
             if (dot != NULL)
                 className = dot+1;
         }



More information about the Python-checkins mailing list