gh-127604: Replace dprintf() with _Py_write_noraise() (#132854) · python/cpython@402dba2

GitHub

@@ -842,11 +842,11 @@ _Py_DumpDecimal(int fd, size_t value)

842842843843/* Format an integer as hexadecimal with width digits into fd file descriptor.

844844 The function is signal safe. */

845-void

846-_Py_DumpHexadecimal(intfd, uintptr_tvalue, Py_ssize_twidth)

845+staticvoid

846+dump_hexadecimal(intfd, uintptr_tvalue, Py_ssize_twidth, intstrip_zeros)

847847{

848848charbuffer[sizeof(uintptr_t) *2+1], *ptr, *end;

849-constPy_ssize_tsize=Py_ARRAY_LENGTH(buffer) -1;

849+Py_ssize_tsize=Py_ARRAY_LENGTH(buffer) -1;

850850851851if (width>size)

852852width=size;

@@ -862,7 +862,35 @@ _Py_DumpHexadecimal(int fd, uintptr_t value, Py_ssize_t width)

862862value >>= 4;

863863 } while ((end-ptr) <width||value);

864864865- (void)_Py_write_noraise(fd, ptr, end-ptr);

865+size=end-ptr;

866+if (strip_zeros) {

867+while (*ptr=='0'&&size >= 2) {

868+ptr++;

869+size--;

870+ }

871+ }

872+873+ (void)_Py_write_noraise(fd, ptr, size);

874+}

875+876+void

877+_Py_DumpHexadecimal(intfd, uintptr_tvalue, Py_ssize_twidth)

878+{

879+dump_hexadecimal(fd, value, width, 0);

880+}

881+882+staticvoid

883+dump_pointer(intfd, void*ptr)

884+{

885+PUTS(fd, "0x");

886+dump_hexadecimal(fd, (uintptr_t)ptr, sizeof(void*), 1);

887+}

888+889+staticvoid

890+dump_char(intfd, charch)

891+{

892+charbuf[1] = {ch};

893+ (void)_Py_write_noraise(fd, buf, 1);

866894}

867895868896void

@@ -924,8 +952,7 @@ _Py_DumpASCII(int fd, PyObject *text)

924952ch=PyUnicode_READ(kind, data, i);

925953if (' ' <= ch&&ch <= 126) {

926954/* printable ASCII character */

927-charc= (char)ch;

928- (void)_Py_write_noraise(fd, &c, 1);

955+dump_char(fd, (char)ch);

929956 }

930957elseif (ch <= 0xff) {

931958PUTS(fd, "\\x");

@@ -1227,7 +1254,9 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)

12271254||info[i].dli_fname==NULL

12281255||info[i].dli_fname[0] =='\0'

12291256 ) {

1230-dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);

1257+PUTS(fd, " Binary file '<unknown>' [");

1258+dump_pointer(fd, array[i]);

1259+PUTS(fd, "]\n");

12311260continue;

12321261 }

12331262@@ -1237,11 +1266,12 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)

12371266info[i].dli_saddr=info[i].dli_fbase;

12381267 }

123912681240-if (info[i].dli_sname==NULL

1241-&&info[i].dli_saddr==0) {

1242-dprintf(fd, " Binary file \"%s\" [%p]\n",

1243-info[i].dli_fname,

1244-array[i]);

1269+if (info[i].dli_sname==NULL&&info[i].dli_saddr==0) {

1270+PUTS(fd, " Binary file \"");

1271+PUTS(fd, info[i].dli_fname);

1272+PUTS(fd, "\" [");

1273+dump_pointer(fd, array[i]);

1274+PUTS(fd, "]\n");

12451275 }

12461276else {

12471277charsign;

@@ -1255,10 +1285,16 @@ _Py_backtrace_symbols_fd(int fd, void *const *array, Py_ssize_t size)

12551285offset=info[i].dli_saddr-array[i];

12561286 }

12571287constchar*symbol_name=info[i].dli_sname!=NULL ? info[i].dli_sname : "";

1258-dprintf(fd, " Binary file \"%s\", at %s%c%#tx [%p]\n",

1259-info[i].dli_fname,

1260-symbol_name,

1261-sign, offset, array[i]);

1288+PUTS(fd, " Binary file \"");

1289+PUTS(fd, info[i].dli_fname);

1290+PUTS(fd, "\", at ");

1291+PUTS(fd, symbol_name);

1292+dump_char(fd, sign);

1293+PUTS(fd, "0x");

1294+dump_hexadecimal(fd, offset, sizeof(offset), 1);

1295+PUTS(fd, " [");

1296+dump_pointer(fd, array[i]);

1297+PUTS(fd, "]\n");

12621298 }

12631299 }

12641300}