Skip to content

Commit 9b1f66b

Browse files
committed
[GR-68273] Fix two emulated posix shortcomings reported on Github.
PullRequest: graalpython/3928
2 parents d5304a2 + a25fae2 commit 9b1f66b

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_posix.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ def test_pipe(self):
173173
os.close(fd1)
174174
os.close(fd2)
175175

176+
@unittest.skipIf(sys.platform != 'linux', 'mkfifo is a linux command')
177+
def test_seek_pipe(self):
178+
new_file_path = './myscript.sh'
179+
with io.open(new_file_path, 'w') as script:
180+
script.write("""#!/bin/sh
181+
mkfifo testpipe
182+
echo "4" > testpipe &
183+
""")
184+
try:
185+
st = os.stat(new_file_path)
186+
os.chmod(new_file_path, st.st_mode | stat.S_IEXEC)
187+
os.system(new_file_path)
188+
with io.open("testpipe", "rb") as r:
189+
out = r.read(1)
190+
assert out == b"4", out
191+
finally:
192+
try:
193+
os.remove(new_file_path)
194+
except:
195+
pass
196+
try:
197+
os.remove("testpipe")
198+
except:
199+
pass
200+
176201
def test_mkdir_rmdir(self):
177202
os.mkdir(TEST_FULL_PATH1)
178203
try:

graalpython/com.oracle.graal.python.test/src/tests/test_subprocess.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ def test_java_asserts(self):
162162
result = subprocess.run([sys.executable, "-c", "import __graalpython__; not __graalpython__.java_assert()"])
163163
assert result.returncode == 0
164164

165+
def test_subprocess_inherits_environ(self):
166+
import os
167+
import subprocess
168+
prev = os.environ.get("FOOBAR")
169+
try:
170+
expected_value = f"42{prev}".strip()
171+
os.environ["FOOBAR"] = expected_value
172+
out = subprocess.check_output([sys.executable, '-c', "import os; print(os.environ['FOOBAR'])"]).decode().strip()
173+
assert out == expected_value, f"{out!r} != {expected_value!r}"
174+
finally:
175+
if prev:
176+
os.environ["FOOBAR"] = prev
177+
else:
178+
del os.environ["FOOBAR"]
179+
165180
@unittest.skipUnless(sys.implementation.name == 'graalpy', "GraalPy-specific test")
166181
@unittest.skipIf(sys.platform == 'win32', "TODO the cmd replacement breaks the test")
167182
def test_graal_python_args(self):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/OSErrorEnum.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -312,7 +312,7 @@ public static ErrorAndMessagePair fromException(Exception e, TruffleString.Equal
312312
return new ErrorAndMessagePair(oserror, oserror.getMessage());
313313
}
314314
} else { // Generic IOException
315-
OSErrorEnum oserror = tryFindErrnoFromMessage(e);
315+
OSErrorEnum oserror = tryFindErrnoFromMessage(e, eqNode);
316316
if (oserror == null) {
317317
return new ErrorAndMessagePair(OSErrorEnum.EIO, getMessage(e));
318318
} else {
@@ -360,15 +360,13 @@ private static TruffleString getReason(FileSystemException e) {
360360
}
361361

362362
@TruffleBoundary
363-
private static OSErrorEnum tryFindErrnoFromMessage(Exception e) {
364-
if (e.getMessage().contains("Broken pipe")) {
365-
return OSErrorEnum.EPIPE;
366-
}
367-
Matcher m = ERRNO_PATTERN.matcher(e.getMessage());
363+
private static OSErrorEnum tryFindErrnoFromMessage(Exception e, TruffleString.EqualNode eqNode) {
364+
String message = e.getMessage();
365+
Matcher m = ERRNO_PATTERN.matcher(message);
368366
if (m.find()) {
369367
return fromNumber(Integer.parseInt(m.group(1)));
370368
}
371-
return null;
369+
return OSErrorEnum.fromMessage(toTruffleStringUncached(message), eqNode);
372370
}
373371

374372
@ValueType

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,15 @@ public Buffer read(int fd, long length,
527527
}
528528

529529
@TruffleBoundary
530-
private static Buffer readBytesFromChannel(ReadableByteChannel channel, long size) throws IOException {
530+
private static Buffer readBytesFromChannel(ReadableByteChannel channel, long sizeIn) throws IOException {
531+
long size = sizeIn;
531532
if (channel instanceof SeekableByteChannel seekableByteChannel) {
532-
long availableSize = seekableByteChannel.size() - seekableByteChannel.position();
533-
size = Math.min(size, availableSize);
533+
try {
534+
long availableSize = seekableByteChannel.size() - seekableByteChannel.position();
535+
size = Math.min(size, availableSize);
536+
} catch (IOException e) {
537+
// pass and read what we can
538+
}
534539
}
535540
size = Math.min(size, MAX_READ);
536541
ByteBuffer dst = ByteBuffer.allocate((int) size);
@@ -2304,6 +2309,8 @@ public int forkExec(Object[] executables, Object[] args, Object cwd, Object[] en
23042309
throw createUnsupportedFeature("Only key=value environment variables are supported in fork_exec");
23052310
}
23062311
}
2312+
} else {
2313+
envMap = new HashMap<>(environ);
23072314
}
23082315

23092316
String[] argStrings;

0 commit comments

Comments
 (0)