Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,16 @@ export class PythonShell extends EventEmitter {
private parseError(data: string | Buffer) {
let text = '' + data;
let error: PythonShellError;
const tracebackHeader = 'Traceback (most recent call last):';
const tracebackStart = text.indexOf(tracebackHeader);

if (/^Traceback/.test(text)) {
// traceback data is available
let lines = text.trim().split(newline);
if (tracebackStart >= 0) {
// Traceback can be prefixed by stderr logs, so parse from the traceback header.
const tracebackText = text.slice(tracebackStart).trim();
let lines = tracebackText.split(newline);
let exception = lines.pop();
error = new PythonShellError(exception);
error.traceback = data;
error.traceback = tracebackText;
// extend stack trace
error.stack +=
newline + ' ----- Python Traceback -----' + newline + ' ';
Expand Down
10 changes: 10 additions & 0 deletions test/python/error_with_stderr_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys

sys.stderr.write('prefix log before traceback\n')


def fail():
raise Exception('Error sample')


fail()
9 changes: 9 additions & 0 deletions test/test-python-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ describe('PythonShell', function () {
done();
});
});
it('should parse traceback when stderr has prefixed logs', function (done) {
let pyshell = new PythonShell('error_with_stderr_prefix.py');
pyshell.on('pythonError', function (err) {
err.message.should.be.equal('Exception: Error sample');
err.should.have.property('traceback');
err.traceback.should.containEql('Traceback (most recent call last)');
done();
});
});
});

describe('.kill()', function () {
Expand Down