Skip to content

GH-37164: [Python] Attach Python stacktrace to errors in ConvertPyError#39380

Merged
pitrou merged 6 commits intoapache:mainfrom
wjones127:37164-rbr-traceback
Jan 11, 2024
Merged

GH-37164: [Python] Attach Python stacktrace to errors in ConvertPyError#39380
pitrou merged 6 commits intoapache:mainfrom
wjones127:37164-rbr-traceback

Conversation

@wjones127
Copy link
Copy Markdown
Member

@wjones127 wjones127 commented Dec 27, 2023

Rationale for this change

Users might define Python generators that are used in RecordBatchReaders and then exported through the C Data Interface. However, if an error occurs in their generator, the stacktrace and message are currently swallowed in the current ConvertPyError implementation, which only provides the type of error. This makes debugging code that passed RBRs difficult.

What changes are included in this PR?

Changes ConvertPyError to provide the fully formatted traceback in the error message.

Are these changes tested?

Yes, added one test to validate the errors messages are propagated.

Are there any user-facing changes?

This is a minor change in the error reporting behavior, which will provide more information.

@wjones127 wjones127 marked this pull request as ready for review December 27, 2023 02:34
@wjones127 wjones127 requested a review from AlenkaF December 27, 2023 02:35
@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Jan 9, 2024
&fmt_exception));

OwnedRef formatted;
formatted.reset(PyObject_CallFunctionObjArgs(fmt_exception.obj(), exc_type_.obj(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check for errors here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've added error checks to each of the calls into the Python API.


std::stringstream ss;
ss << "Python exception: ";
Py_ssize_t num_lines = PyList_GET_SIZE(formatted.obj());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably check that we do have a list here. Or you can simply use PySequence_Length and PySequence_GetItem (this code is not performance-critical).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API docs claim this should return a list, but it's perhaps possible that they might change to some other sequence. I changed to be generic sequences.

I assume the function calls will do the type checking internally, correct?

Copy link
Copy Markdown
Member

@pitrou pitrou Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the function calls will do the type checking internally, correct?

If you're using the concrete PyList APIs, they will generally assume that it is a list object (there is no hard rule unfortunately). Also, fast access macros such as PyList_GET_ITEM don't do any error checking.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. I see the distinction now between the error checking and non-error-checking variants. 👍

@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Jan 11, 2024
@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Jan 11, 2024

std::stringstream ss;
ss << "Python exception: ";
Py_ssize_t num_lines = PySequence_Length(formatted.obj());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check for failure here (-1 is returned on error).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I should have seen that 🤦

@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Jan 11, 2024
@pitrou pitrou merged commit 6fe7480 into apache:main Jan 11, 2024
@pitrou pitrou removed the awaiting changes Awaiting changes label Jan 11, 2024
@github-actions github-actions bot added the awaiting committer review Awaiting committer review label Jan 11, 2024
@conbench-apache-arrow
Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 6fe7480.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 1 possible false positive for unstable benchmarks that are known to sometimes produce them.

dgreiss pushed a commit to dgreiss/arrow that referenced this pull request Feb 19, 2024
…rtPyError` (apache#39380)

### Rationale for this change

Users might define Python generators that are used in RecordBatchReaders and then exported through the C Data Interface. However, if an error occurs in their generator, the stacktrace and message are currently swallowed in the current `ConvertPyError` implementation, which only provides the type of error. This makes debugging code that passed RBRs difficult.

### What changes are included in this PR?

Changes `ConvertPyError` to provide the fully formatted traceback in the error message.

### Are these changes tested?

Yes, added one test to validate the errors messages are propagated.

### Are there any user-facing changes?

This is a minor change in the error reporting behavior, which will provide more information.
* Closes: apache#37164

Authored-by: Will Jones <willjones127@gmail.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Attach Python stacktrace to errors in ConvertPyError

3 participants