Skip to content

Commit bd31b7c

Browse files
committed
Issue #23848: Expose _Py_DumpHexadecimal()
This function will be reused by faulthandler.
1 parent 5dacbd4 commit bd31b7c

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

Include/traceback.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,20 @@ PyAPI_FUNC(void) _Py_DumpASCII(int fd, PyObject *text);
9494
/* Format an integer as decimal into the file descriptor fd.
9595
9696
This function is signal safe. */
97-
PyAPI_FUNC(void) _Py_DumpDecimal(int fd, unsigned long value);
97+
PyAPI_FUNC(void) _Py_DumpDecimal(
98+
int fd,
99+
unsigned long value);
100+
101+
/* Format an integer as hexadecimal into the file descriptor fd with at least
102+
width digits.
103+
104+
The maximum width is sizeof(unsigned long)*2 digits.
105+
106+
This function is signal safe. */
107+
PyAPI_FUNC(void) _Py_DumpHexadecimal(
108+
int fd,
109+
unsigned long value,
110+
Py_ssize_t width);
98111

99112
#endif /* !Py_LIMITED_API */
100113

Python/traceback.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,15 @@ _Py_DumpDecimal(int fd, unsigned long value)
506506
507507
This function is signal safe. */
508508

509-
static void
510-
dump_hexadecimal(int fd, unsigned long value, Py_ssize_t width)
509+
void
510+
_Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width)
511511
{
512512
char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end;
513513
const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
514514

515515
if (width > size)
516516
width = size;
517+
/* it's ok if width is negative */
517518

518519
end = &buffer[size];
519520
ptr = end;
@@ -582,15 +583,15 @@ _Py_DumpASCII(int fd, PyObject *text)
582583
}
583584
else if (ch <= 0xff) {
584585
PUTS(fd, "\\x");
585-
dump_hexadecimal(fd, ch, 2);
586+
_Py_DumpHexadecimal(fd, ch, 2);
586587
}
587588
else if (ch <= 0xffff) {
588589
PUTS(fd, "\\u");
589-
dump_hexadecimal(fd, ch, 4);
590+
_Py_DumpHexadecimal(fd, ch, 4);
590591
}
591592
else {
592593
PUTS(fd, "\\U");
593-
dump_hexadecimal(fd, ch, 8);
594+
_Py_DumpHexadecimal(fd, ch, 8);
594595
}
595596
}
596597
if (truncated) {
@@ -693,9 +694,9 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
693694
PUTS(fd, "Current thread 0x");
694695
else
695696
PUTS(fd, "Thread 0x");
696-
dump_hexadecimal(fd,
697-
(unsigned long)tstate->thread_id,
698-
sizeof(unsigned long) * 2);
697+
_Py_DumpHexadecimal(fd,
698+
(unsigned long)tstate->thread_id,
699+
sizeof(unsigned long) * 2);
699700
PUTS(fd, " (most recent call first):\n");
700701
}
701702

0 commit comments

Comments
 (0)