Skip to content

Commit b2eea70

Browse files
committed
Validate that the argument of excepthook is exception
1 parent 234dbd6 commit b2eea70

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_sys.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_ioencoding_nonascii
2424
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_no_duplicates_in_meta_path
2525
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_refcount
26+
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_executable
2627
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_setrecursionlimit_recursion_depth
2728
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_sys_getwindowsversion_no_instantiation
2829
*graalpython.lib-python.3.test.test_sys.UnraisableHookTest.test_custom_unraisablehook

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,6 @@ private synchronized int forkExec(PList args, PList execList, @SuppressWarnings(
148148
}
149149
}
150150

151-
if (!argStrings.isEmpty()) {
152-
if (argStrings.get(0).equals(context.getOption(PythonOptions.Executable))) {
153-
String[] executableList = PythonOptions.getExecutableList(context);
154-
argStrings.remove(0);
155-
for (int i = executableList.length - 1; i >= 0; i--) {
156-
argStrings.add(0, executableList[i]);
157-
}
158-
}
159-
}
160-
161151
File cwdFile;
162152
try {
163153
if (getContext().getEnv().getPublicTruffleFile(cwd).exists()) {
@@ -211,14 +201,30 @@ private synchronized int forkExec(PList args, PList execList, @SuppressWarnings(
211201
}
212202
byte[] bytes = checkNullBytes(toBytes.execute(null, item));
213203
String path = new String(bytes, StandardCharsets.US_ASCII);
214-
argStrings.set(0, path);
204+
int executableListLen = 0;
205+
if (path.equals(context.getOption(PythonOptions.Executable))) {
206+
// In case someone passed to us sys.executable that happens to be java command
207+
// invocation with additional options like classpath, we split it to the
208+
// individual arguments
209+
String[] executableList = PythonOptions.getExecutableList(context);
210+
argStrings.remove(0);
211+
executableListLen = executableList.length;
212+
for (int j = executableListLen - 1; j >= 0; j--) {
213+
argStrings.add(0, executableList[j]);
214+
}
215+
} else {
216+
argStrings.set(0, path);
217+
}
215218
try {
216219
return exec(argStrings, cwdFile, envMap, p2cwrite, p2cread, c2pwrite, c2pread, errwrite, errpipe_write, resources, errread);
217220
} catch (IOException ex) {
218221
if (firstError == null) {
219222
firstError = ex;
220223
}
221224
}
225+
for (int j = 0; j < executableListLen - 1; j++) {
226+
argStrings.remove(j + 1);
227+
}
222228
}
223229
assert firstError != null;
224230
if (errpipe_write != -1) {

graalpython/lib-graalpython/sys.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39+
from builtins import BaseException
40+
3941

4042
def make_implementation_info():
4143
from _descriptor import SimpleNamespace, make_named_tuple_class
@@ -190,6 +192,10 @@ def simple_print_traceback(e):
190192
print(type(e).__qualname__, file=stderr)
191193

192194
def __print_traceback__(typ, value, tb):
195+
if not isinstance(value, BaseException):
196+
msg = "TypeError: print_exception(): Exception expected for value, {} found\n".format(type(value).__name__)
197+
print(msg, file=stderr, end="")
198+
return
193199
try:
194200
import traceback
195201
lines = traceback.format_exception(typ, value, tb)

0 commit comments

Comments
 (0)