Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Add tests for…
… the fallback option
  • Loading branch information
pablogsal committed Oct 12, 2023
commit dab41fb22e2896a58f702ab065387e96f842dc76
5 changes: 2 additions & 3 deletions Include/internal/pycore_traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ extern PyObject* _PyTraceBack_FromFrame(

/* Write the traceback tb to file f. Prefix each line with
indent spaces followed by the margin (if it is not NULL). */
extern int _PyTraceBack_Print_Indented(
PyObject *tb, int indent, const char* margin,
const char *header_margin, const char *header, PyObject *f);
extern int _PyTraceBack_Print(
PyObject *tb, const char *header_margin, const char *header, PyObject *f);
extern int _Py_WriteIndentedMargin(int, const char*, PyObject *);
extern int _Py_WriteIndent(int, PyObject *);

Expand Down
45 changes: 11 additions & 34 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_pythonrun.h" // export _PyRun_InteractiveLoopObject()
#include "pycore_sysmodule.h" // _PySys_Audit()
#include "pycore_traceback.h" // _PyTraceBack_Print_Indented()
#include "pycore_traceback.h" // _PyTraceBack_Print()

#include "errcode.h" // E_EOF
#include "marshal.h" // PyMarshal_ReadLongFromFile()
Expand Down Expand Up @@ -687,20 +687,11 @@ struct exception_print_context
PyObject *seen; // Prevent cycles in recursion
};

static int
write_indented_margin(struct exception_print_context *ctx, PyObject *f)
{
return _Py_WriteIndentedMargin(0, "", f);
}

static int
print_exception_invalid_type(struct exception_print_context *ctx,
PyObject *value)
{
PyObject *f = ctx->file;
if (_Py_WriteIndent(0, f) < 0) {
return -1;
}
const char *const msg = "TypeError: print_exception(): Exception expected "
"for value, ";
if (PyFile_WriteString(msg, f) < 0) {
Expand All @@ -725,8 +716,7 @@ print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
if (tb && tb != Py_None) {
const char *header = EXCEPTION_TB_HEADER;
const char *header_margin = "";
err = _PyTraceBack_Print_Indented(
tb, 0, "", header_margin, header, f);
err = _PyTraceBack_Print(tb, header_margin, header, f);
}
Py_XDECREF(tb);
return err;
Expand Down Expand Up @@ -769,9 +759,6 @@ print_exception_file_and_line(struct exception_print_context *ctx,
if (line == NULL) {
goto error;
}
if (write_indented_margin(ctx, f) < 0) {
goto error;
}
if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
goto error;
}
Expand All @@ -794,9 +781,6 @@ print_exception_message(struct exception_print_context *ctx, PyObject *type,

assert(PyExceptionClass_Check(type));

if (write_indented_margin(ctx, f) < 0) {
return -1;
}
PyObject *modulename = PyObject_GetAttr(type, &_Py_ID(__module__));
if (modulename == NULL || !PyUnicode_Check(modulename)) {
Py_XDECREF(modulename);
Expand Down Expand Up @@ -931,21 +915,12 @@ print_chained(struct exception_print_context* ctx, PyObject *value,
return -1;
}

if (write_indented_margin(ctx, f) < 0) {
return -1;
}
if (PyFile_WriteString("\n", f) < 0) {
return -1;
}
if (write_indented_margin(ctx, f) < 0) {
return -1;
}
if (PyFile_WriteString(message, f) < 0) {
return -1;
}
if (write_indented_margin(ctx, f) < 0) {
return -1;
}
if (PyFile_WriteString("\n", f) < 0) {
return -1;
}
Expand Down Expand Up @@ -1063,35 +1038,37 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
}
}

int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;

if (!value) {
goto fallback;
}

// Try first with the traceback.
int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;
// Try first with the stdlib traceback module
PyObject *traceback_module = PyImport_ImportModule("traceback");

if (traceback_module == NULL) {
goto fallback;
}

PyObject *print_tb_func = PyObject_GetAttrString(traceback_module, "_print_exception_bltin");
PyObject *print_exception_fn = PyObject_GetAttrString(traceback_module, "_print_exception_bltin");

if (print_tb_func == NULL || !PyCallable_Check(print_tb_func)) {
if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
Py_DECREF(traceback_module);
goto fallback;
}

PyObject* result = PyObject_CallOneArg(print_tb_func, value);
PyObject* result = PyObject_CallOneArg(print_exception_fn, value);

Py_DECREF(traceback_module);
Py_XDECREF(print_tb_func);
_PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
Py_XDECREF(print_exception_fn);
if (result) {
Py_DECREF(result);
_PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
return;
}
fallback:
_PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
#ifdef Py_DEBUG
_PyErr_WriteUnraisableMsg("in the internal traceback machinery", NULL);
#endif
Expand Down
61 changes: 11 additions & 50 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,27 +397,9 @@ _Py_WriteIndent(int indent, PyObject *f)
return 0;
}

/* Writes indent spaces, followed by the margin if it is not `\0`.
Returns 0 on success and non-zero on failure.
*/
int
_Py_WriteIndentedMargin(int indent, const char *margin, PyObject *f)
{
if (_Py_WriteIndent(indent, f) < 0) {
return -1;
}
if (margin) {
if (PyFile_WriteString(margin, f) < 0) {
return -1;
}
}
return 0;
}

static int
display_source_line_with_margin(PyObject *f, PyObject *filename, int lineno, int indent,
int margin_indent, const char *margin,
int *truncation, PyObject **line)
display_source_line(PyObject *f, PyObject *filename, int lineno, int indent,
int *truncation, PyObject **line)
{
int fd;
int i;
Expand Down Expand Up @@ -545,10 +527,6 @@ display_source_line_with_margin(PyObject *f, PyObject *filename, int lineno, int
*truncation = i - indent;
}

if (_Py_WriteIndentedMargin(margin_indent, margin, f) < 0) {
goto error;
}

/* Write some spaces before the line */
if (_Py_WriteIndent(indent, f) < 0) {
goto error;
Expand All @@ -574,8 +552,7 @@ int
_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent,
int *truncation, PyObject **line)
{
return display_source_line_with_margin(f, filename, lineno, indent, 0,
NULL, truncation, line);
return display_source_line(f, filename, lineno, indent, truncation, line);
}


Expand All @@ -595,16 +572,12 @@ ignore_source_errors(void) {

static int
tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int lineno,
PyFrameObject *frame, PyObject *name, int margin_indent, const char *margin)
PyFrameObject *frame, PyObject *name)
{
if (filename == NULL || name == NULL) {
return -1;
}

if (_Py_WriteIndentedMargin(margin_indent, margin, f) < 0) {
return -1;
}

PyObject *line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
filename, lineno, name);
if (line == NULL) {
Expand All @@ -621,9 +594,9 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen

int truncation = _TRACEBACK_SOURCE_LINE_INDENT;
PyObject* source_line = NULL;
int rc = display_source_line_with_margin(
int rc = display_source_line(
f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
margin_indent, margin, &truncation, &source_line);
&truncation, &source_line);
if (rc != 0 || !source_line) {
/* ignore errors since we can't report them, can we? */
err = ignore_source_errors();
Expand Down Expand Up @@ -664,11 +637,6 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
}
}

if (_Py_WriteIndentedMargin(margin_indent, margin, f) < 0) {
err = -1;
goto done;
}

done:
Py_XDECREF(source_line);
return err;
Expand All @@ -694,8 +662,7 @@ tb_print_line_repeated(PyObject *f, long cnt)
}

static int
tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
int indent, const char *margin)
tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
{
PyCodeObject *code = NULL;
Py_ssize_t depth = 0;
Expand Down Expand Up @@ -731,7 +698,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
cnt++;
if (cnt <= TB_RECURSIVE_CUTOFF) {
if (tb_displayline(tb, f, code->co_filename, tb->tb_lineno,
tb->tb_frame, code->co_name, indent, margin) < 0) {
tb->tb_frame, code->co_name) < 0) {
goto error;
}

Expand All @@ -756,8 +723,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
#define PyTraceBack_LIMIT 1000

int
_PyTraceBack_Print_Indented(PyObject *v, int indent, const char *margin,
const char *header_margin, const char *header, PyObject *f)
_PyTraceBack_Print(PyObject *v, const char *header_margin, const char *header, PyObject *f)
{
PyObject *limitv;
long limit = PyTraceBack_LIMIT;
Expand All @@ -780,15 +746,12 @@ _PyTraceBack_Print_Indented(PyObject *v, int indent, const char *margin,
return 0;
}
}
if (_Py_WriteIndentedMargin(indent, header_margin, f) < 0) {
return -1;
}

if (PyFile_WriteString(header, f) < 0) {
return -1;
}

if (tb_printinternal((PyTracebackObject *)v, f, limit, indent, margin) < 0) {
if (tb_printinternal((PyTracebackObject *)v, f, limit) < 0) {
return -1;
}

Expand All @@ -798,12 +761,10 @@ _PyTraceBack_Print_Indented(PyObject *v, int indent, const char *margin,
int
PyTraceBack_Print(PyObject *v, PyObject *f)
{
int indent = 0;
const char *margin = NULL;
const char *header_margin = NULL;
const char *header = EXCEPTION_TB_HEADER;

return _PyTraceBack_Print_Indented(v, indent, margin, header_margin, header, f);
return _PyTraceBack_Print(v, header_margin, header, f);
}

/* Format an integer in range [0; 0xffffffff] to decimal and write it
Expand Down