Skip to content

Commit aef836c

Browse files
committed
SystemExit: non integer code should be written to Python level stderr
1 parent eae40ad commit aef836c

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
import com.oracle.graal.python.builtins.objects.module.PythonModule;
153153
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
154154
import com.oracle.graal.python.builtins.objects.object.PythonObject;
155+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
155156
import com.oracle.graal.python.builtins.objects.posix.DirEntryBuiltins;
156157
import com.oracle.graal.python.builtins.objects.posix.ScandirIteratorBuiltins;
157158
import com.oracle.graal.python.builtins.objects.random.RandomBuiltins;
@@ -568,6 +569,21 @@ public void warn(PythonBuiltinClassType type, String format, Object... args) {
568569
WarningsModuleBuiltins.WarnNode.getUncached().warnFormat(null, null, type, 1, format, args);
569570
}
570571

572+
@Override
573+
public Object getStderr() {
574+
Object sys = lookupBuiltinModule("sys");
575+
try {
576+
return PythonObjectLibrary.getUncached().lookupAttribute(sys, null, "stderr");
577+
} catch (PException e) {
578+
try {
579+
getContext().getEnv().err().write("lost sys.stderr\n".getBytes());
580+
} catch (IOException ioe) {
581+
// nothing more we can do
582+
}
583+
throw e;
584+
}
585+
}
586+
571587
private void publishBuiltinModules() {
572588
PythonModule sysModule = builtinModules.get("sys");
573589
PDict sysModules = (PDict) sysModule.getAttribute("modules");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/WarningsModuleBuiltins.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.io.IOException;
4443
import java.util.IllegalFormatException;
4544
import java.util.List;
4645

@@ -529,18 +528,7 @@ private static void showWarning(Object filename, int lineno, Object text, Object
529528
} else {
530529
name = polib.lookupAttribute(category, null, SpecialAttributeNames.__NAME__);
531530
}
532-
Object sys = PythonLanguage.getCore().lookupBuiltinModule("sys");
533-
Object stderr;
534-
try {
535-
stderr = polib.lookupAttribute(sys, null, "stderr");
536-
} catch (PException e) {
537-
try {
538-
PythonLanguage.getContext().getEnv().err().write("lost sys.stderr\n".getBytes());
539-
} catch (IOException ioe) {
540-
// nothing more we can do
541-
}
542-
throw e;
543-
}
531+
Object stderr = PythonLanguage.getCore().getStderr();
544532

545533
// tfel: I've inlined PyFile_WriteObject, which just calls the "write" method and
546534
// decides if we should use "repr" or "str" - in this case its always "str" for objects
@@ -823,10 +811,12 @@ private static String getSourceLine(PDict globals, int lineno) {
823811
private final Assumption passFrame = Truffle.getRuntime().createAssumption();
824812
private final Assumption passExc = Truffle.getRuntime().createAssumption();
825813

814+
@Override
826815
public Assumption needNotPassFrameAssumption() {
827816
return passFrame;
828817
}
829818

819+
@Override
830820
public Assumption needNotPassExceptionAssumption() {
831821
return passExc;
832822
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,9 @@ private void handleSystemExit(VirtualFrame frame, PBaseException pythonException
250250
}
251251
if (theContext.getOption(PythonOptions.AlwaysRunExcepthook)) {
252252
// If we failed to dig out the exit code we just print and leave
253-
try {
254-
theContext.getEnv().err().write(callStrNode.executeObject(frame, pythonException).toString().getBytes());
255-
theContext.getEnv().err().write('\n');
256-
} catch (IOException e1) {
257-
}
253+
Object stderr = theContext.getCore().getStderr();
254+
Object message = callStrNode.executeObject(frame, pythonException);
255+
PythonObjectLibrary.getUncached().lookupAndCallRegularMethod(stderr, null, "write", message);
258256
throw new PythonExitException(this, 1);
259257
}
260258
PException e = pythonException.getExceptionForReraise(pythonException.getTraceback());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonCore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public interface PythonCore extends ParserErrorCallback {
7272
@Override
7373
void warn(PythonBuiltinClassType type, String format, Object... args);
7474

75+
/**
76+
* Returns the stderr object or signals error when stderr is "lost".
77+
*/
78+
Object getStderr();
79+
7580
// Accessors
7681
@Override
7782
public PythonLanguage getLanguage();

0 commit comments

Comments
 (0)