Issue #23848: Expose _Py_DumpHexadecimal()

This function will be reused by faulthandler.
This commit is contained in:
Victor Stinner 2016-03-23 10:32:26 +01:00
parent 5dacbd4c42
commit bd31b7c483
2 changed files with 23 additions and 9 deletions

View File

@ -94,7 +94,20 @@ PyAPI_FUNC(void) _Py_DumpASCII(int fd, PyObject *text);
/* Format an integer as decimal into the file descriptor fd. /* Format an integer as decimal into the file descriptor fd.
This function is signal safe. */ This function is signal safe. */
PyAPI_FUNC(void) _Py_DumpDecimal(int fd, unsigned long value); PyAPI_FUNC(void) _Py_DumpDecimal(
int fd,
unsigned long value);
/* Format an integer as hexadecimal into the file descriptor fd with at least
width digits.
The maximum width is sizeof(unsigned long)*2 digits.
This function is signal safe. */
PyAPI_FUNC(void) _Py_DumpHexadecimal(
int fd,
unsigned long value,
Py_ssize_t width);
#endif /* !Py_LIMITED_API */ #endif /* !Py_LIMITED_API */

View File

@ -506,14 +506,15 @@ _Py_DumpDecimal(int fd, unsigned long value)
This function is signal safe. */ This function is signal safe. */
static void void
dump_hexadecimal(int fd, unsigned long value, Py_ssize_t width) _Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width)
{ {
char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end; char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end;
const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1; const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
if (width > size) if (width > size)
width = size; width = size;
/* it's ok if width is negative */
end = &buffer[size]; end = &buffer[size];
ptr = end; ptr = end;
@ -582,15 +583,15 @@ _Py_DumpASCII(int fd, PyObject *text)
} }
else if (ch <= 0xff) { else if (ch <= 0xff) {
PUTS(fd, "\\x"); PUTS(fd, "\\x");
dump_hexadecimal(fd, ch, 2); _Py_DumpHexadecimal(fd, ch, 2);
} }
else if (ch <= 0xffff) { else if (ch <= 0xffff) {
PUTS(fd, "\\u"); PUTS(fd, "\\u");
dump_hexadecimal(fd, ch, 4); _Py_DumpHexadecimal(fd, ch, 4);
} }
else { else {
PUTS(fd, "\\U"); PUTS(fd, "\\U");
dump_hexadecimal(fd, ch, 8); _Py_DumpHexadecimal(fd, ch, 8);
} }
} }
if (truncated) { if (truncated) {
@ -693,7 +694,7 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
PUTS(fd, "Current thread 0x"); PUTS(fd, "Current thread 0x");
else else
PUTS(fd, "Thread 0x"); PUTS(fd, "Thread 0x");
dump_hexadecimal(fd, _Py_DumpHexadecimal(fd,
(unsigned long)tstate->thread_id, (unsigned long)tstate->thread_id,
sizeof(unsigned long) * 2); sizeof(unsigned long) * 2);
PUTS(fd, " (most recent call first):\n"); PUTS(fd, " (most recent call first):\n");