Skip to content

Commit a8c0da7

Browse files
committed
#130 fixing stderr causing python-shell to emit error
1 parent e001d41 commit a8c0da7

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class PythonShell extends EventEmitter{
145145
if(!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return;
146146

147147
let err:PythonShellError;
148-
if (errorData || (self.exitCode && self.exitCode !== 0)) {
148+
if (self.exitCode && self.exitCode !== 0) {
149149
if (errorData) {
150150
err = self.parseError(errorData);
151151
} else {

test/python/stderrLogging.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# logging example taken from https://docs.python.org/3/howto/logging-cookbook.html
2+
# Note that logging logs to stderr by default
3+
4+
import logging
5+
6+
# set up logging to file - see previous section for more details
7+
logging.basicConfig(level=logging.DEBUG)
8+
# define a Handler which writes INFO messages or higher to the sys.stderr
9+
console = logging.StreamHandler()
10+
console.setLevel(logging.INFO)
11+
# add the handler to the root logger
12+
logging.getLogger('').addHandler(console)
13+
14+
# Now, we can log to the root logger, or any other logger. First the root...
15+
logging.info('Jackdaws love my big sphinx of quartz.')
16+
17+
# Now, define a couple of other loggers which might represent areas in your
18+
# application:
19+
20+
logger1 = logging.getLogger('myapp.area1')
21+
logger2 = logging.getLogger('myapp.area2')
22+
23+
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
24+
logger1.info('How quickly daft jumping zebras vex.')
25+
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
26+
logger2.error('The five boxing wizards jump quickly.')

test/test-python-shell.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ describe('PythonShell', function () {
296296
done();
297297
});
298298
});
299-
it('should emit error when error is written to stderr', function (done) {
299+
it('should emit error when the program exits because of an unhandled exception', function (done) {
300300
let pyshell = new PythonShell('error.py');
301301
pyshell.on('error', function (err) {
302302
err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero');
@@ -305,6 +305,15 @@ describe('PythonShell', function () {
305305
done();
306306
});
307307
});
308+
it('should NOT emit error when logging is written to stderr', function (done) {
309+
let pyshell = new PythonShell('stderrLogging.py');
310+
pyshell.on('error', function (err) {
311+
done(new Error("an error should not have been raised"));
312+
});
313+
pyshell.on('close', function(){
314+
done();
315+
})
316+
});
308317
});
309318

310319
describe('.parseError(data)', function () {

0 commit comments

Comments
 (0)