]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127604: Replace dprintf() with _Py_write_noraise() (#132854)
authorVictor Stinner <vstinner@python.org>
Wed, 23 Apr 2025 20:02:25 +0000 (22:02 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Apr 2025 20:02:25 +0000 (20:02 +0000)
Python/traceback.c

index 56d94d6431befece0edfbf094efad3546c8e992e..0a9f571cac7fb03774b6500b381d2e4149b57ecb 100644 (file)
@@ -842,11 +842,11 @@ _Py_DumpDecimal(int fd, size_t value)
 
 /* Format an integer as hexadecimal with width digits into fd file descriptor.
    The function is signal safe. */
-void
-_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
+static void
+dump_hexadecimal(int fd, uintptr_t value, Py_ssize_t width, int strip_zeros)
 {
     char buffer[sizeof(uintptr_t) * 2 + 1], *ptr, *end;
-    const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
+    Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
 
     if (width > size)
         width = size;
@@ -862,7 +862,35 @@ _Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
         value >>= 4;
     } while ((end - ptr) < width || value);
 
-    (void)_Py_write_noraise(fd, ptr, end - ptr);
+    size = end - ptr;
+    if (strip_zeros) {
+        while (*ptr == '0' && size >= 2) {
+            ptr++;
+            size--;
+        }
+    }
+
+    (void)_Py_write_noraise(fd, ptr, size);
+}
+
+void
+_Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)
+{
+    dump_hexadecimal(fd, value, width, 0);
+}
+
+static void
+dump_pointer(int fd, void *ptr)
+{
+    PUTS(fd, "0x");
+    dump_hexadecimal(fd, (uintptr_t)ptr, sizeof(void*), 1);
+}
+
+static void
+dump_char(int fd, char ch)
+{
+    char buf[1] = {ch};
+    (void)_Py_write_noraise(fd, buf, 1);
 }
 
 void
@@ -924,8 +952,7 @@ _Py_DumpASCII(int fd, PyObject *text)
         ch = PyUnicode_READ(kind, data, i);
         if (' ' <= ch && ch <= 126) {
             /* printable ASCII character */
-            char c = (char)ch;
-            (void)_Py_write_noraise(fd, &c, 1);
+            dump_char(fd, (char)ch);
         }
         else if (ch <= 0xff) {
             PUTS(fd, "\\x");
@@ -1227,7 +1254,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
             || info[i].dli_fname == NULL
             || info[i].dli_fname[0] == '\0'
         ) {
-            dprintf(fd, "  Binary file '<unknown>' [%p]\n", array[i]);
+            PUTS(fd, "  Binary file '<unknown>' [");
+            dump_pointer(fd, array[i]);
+            PUTS(fd, "]\n");
             continue;
         }
 
@@ -1237,11 +1266,12 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
             info[i].dli_saddr = info[i].dli_fbase;
         }
 
-        if (info[i].dli_sname == NULL
-            && info[i].dli_saddr == 0) {
-            dprintf(fd, "  Binary file \"%s\" [%p]\n",
-                    info[i].dli_fname,
-                    array[i]);
+        if (info[i].dli_sname == NULL && info[i].dli_saddr == 0) {
+            PUTS(fd, "  Binary file \"");
+            PUTS(fd, info[i].dli_fname);
+            PUTS(fd, "\" [");
+            dump_pointer(fd, array[i]);
+            PUTS(fd, "]\n");
         }
         else {
             char sign;
@@ -1255,10 +1285,16 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)
                 offset = info[i].dli_saddr - array[i];
             }
             const char *symbol_name = info[i].dli_sname != NULL ? info[i].dli_sname : "";
-            dprintf(fd, "  Binary file \"%s\", at %s%c%#tx [%p]\n",
-                    info[i].dli_fname,
-                    symbol_name,
-                    sign, offset, array[i]);
+            PUTS(fd, "  Binary file \"");
+            PUTS(fd, info[i].dli_fname);
+            PUTS(fd, "\", at ");
+            PUTS(fd, symbol_name);
+            dump_char(fd, sign);
+            PUTS(fd, "0x");
+            dump_hexadecimal(fd, offset, sizeof(offset), 1);
+            PUTS(fd, " [");
+            dump_pointer(fd, array[i]);
+            PUTS(fd, "]\n");
         }
     }
 }