Skip to content

Commit 4434c2d

Browse files
committed
Implement os.openpty
1 parent 6619e6b commit 4434c2d

File tree

9 files changed

+68
-1
lines changed

9 files changed

+68
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def parse_unittest_output(output):
203203
# The whole reason for this function's complexity is that we want to consume arbitrary
204204
# warnings after the '...' part without accidentally consuming the next test result
205205
import re
206-
re_test_result = re.compile(r"""\b(test\S+) \((\S+)\)(?:\n.*?)?? \.\.\. """, re.MULTILINE | re.DOTALL)
206+
re_test_result = re.compile(r"""\b(test\S*) \((\S+)\)(?:\n.*?)?? \.\.\. """, re.MULTILINE | re.DOTALL)
207207
re_test_status = re.compile(r"""\b(ok|skipped (?:'[^']*'|"[^"]*")|FAIL|ERROR)$""", re.MULTILINE | re.DOTALL)
208208
pos = 0
209209
current_result = None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*graalpython.lib-python.3.test.test_openpty.OpenptyTest.test

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import com.oracle.graal.python.runtime.PosixConstants.IntConstant;
116116
import com.oracle.graal.python.runtime.PosixSupportLibrary;
117117
import com.oracle.graal.python.runtime.PosixSupportLibrary.Buffer;
118+
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
118119
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
119120
import com.oracle.graal.python.runtime.PosixSupportLibrary.Timeval;
120121
import com.oracle.graal.python.runtime.PythonContext;
@@ -689,6 +690,24 @@ Object setsid(VirtualFrame frame,
689690
}
690691
}
691692

693+
@Builtin(name = "openpty")
694+
@GenerateNodeFactory
695+
public abstract static class OpenPtyNode extends PythonBuiltinNode {
696+
697+
@Specialization
698+
Object openpty(VirtualFrame frame,
699+
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
700+
try {
701+
OpenPtyResult result = posixLib.openpty(getPosixSupport());
702+
posixLib.setInheritable(getPosixSupport(), result.masterFd(), false);
703+
posixLib.setInheritable(getPosixSupport(), result.slaveFd(), false);
704+
return factory().createTuple(new int[]{result.masterFd(), result.slaveFd()});
705+
} catch (PosixException e) {
706+
throw raiseOSErrorFromPosixException(frame, e);
707+
}
708+
}
709+
}
710+
692711
@Builtin(name = "open", minNumOfPositionalArgs = 2, parameterNames = {"path", "flags", "mode"}, keywordOnlyNames = {"dir_fd"})
693712
@ArgumentClinic(name = "path", conversionClass = PathConversionNode.class, args = {"false", "false"})
694713
@ArgumentClinic(name = "flags", conversion = ClinicConversion.Int)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
230230
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
231231
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
232+
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
232233
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
233234
import com.oracle.graal.python.runtime.PosixSupportLibrary.PwdResult;
234235
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
@@ -2003,6 +2004,12 @@ public long setsid() {
20032004
throw new UnsupportedPosixFeatureException("Emulated getsid not supported");
20042005
}
20052006

2007+
@ExportMessage
2008+
@SuppressWarnings("static-method")
2009+
public OpenPtyResult openpty() {
2010+
throw new UnsupportedPosixFeatureException("Emulated openpty not supported");
2011+
}
2012+
20062013
@ExportMessage
20072014
@SuppressWarnings("static-method")
20082015
public TruffleString ctermid() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.runtime.PosixSupportLibrary.FamilySpecificSockAddr;
5454
import com.oracle.graal.python.runtime.PosixSupportLibrary.GetAddrInfoException;
5555
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
56+
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
5657
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
5758
import com.oracle.graal.python.runtime.PosixSupportLibrary.PwdResult;
5859
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
@@ -681,6 +682,12 @@ final long setsid(
681682
return nativeLib.setsid(nativePosixSupport);
682683
}
683684

685+
@ExportMessage
686+
final OpenPtyResult openpty(@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
687+
checkNotInImageBuildtime();
688+
return nativeLib.openpty(nativePosixSupport);
689+
}
690+
684691
@ExportMessage
685692
final TruffleString ctermid(@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
686693
checkNotInImageBuildtime();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.runtime.PosixSupportLibrary.FamilySpecificSockAddr;
5353
import com.oracle.graal.python.runtime.PosixSupportLibrary.GetAddrInfoException;
5454
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
55+
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
5556
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
5657
import com.oracle.graal.python.runtime.PosixSupportLibrary.PwdResult;
5758
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
@@ -888,6 +889,16 @@ public int mmapReadBytes(Object mmap, long index, byte[] bytes, int length,
888889
}
889890
}
890891

892+
@ExportMessage
893+
final OpenPtyResult openpty(@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
894+
logEnter("openpty", "");
895+
try {
896+
return logExit("openpty", "%s", lib.openpty(delegate));
897+
} catch (PosixException e) {
898+
throw logException("openpty", e);
899+
}
900+
}
901+
891902
@ExportMessage
892903
final TruffleString ctermid(@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
893904
logEnter("ctermid", "");

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
8686
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
8787
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
88+
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
8889
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
8990
import com.oracle.graal.python.runtime.PosixSupportLibrary.PwdResult;
9091
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
@@ -216,6 +217,7 @@ private enum PosixNativeFunction {
216217
call_getpgrp("():sint64"),
217218
call_getsid("(sint64):sint64"),
218219
call_setsid("():sint64"),
220+
call_openpty("([sint32]):sint32"),
219221
call_ctermid("([sint8]):sint32"),
220222
call_setenv("([sint8], [sint8], sint32):sint32"),
221223
call_unsetenv("([sint8]):sint32"),
@@ -1167,6 +1169,16 @@ public long setsid(
11671169
return res;
11681170
}
11691171

1172+
@ExportMessage
1173+
public OpenPtyResult openpty(@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
1174+
int[] outvars = new int[2];
1175+
int res = invokeNode.callInt(this, PosixNativeFunction.call_openpty, wrap(outvars));
1176+
if (res == -1) {
1177+
throw getErrnoAndThrowPosixException(invokeNode);
1178+
}
1179+
return new OpenPtyResult(outvars[0], outvars[1]);
1180+
}
1181+
11701182
@ExportMessage
11711183
public TruffleString ctermid(@Shared("invoke") @Cached InvokeNativeFunction invokeNode,
11721184
@Shared("tsFromBytes") @Cached TruffleString.FromByteArrayNode fromByteArrayNode,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ public abstract class PosixSupportLibrary extends Library {
288288

289289
public abstract long setsid(Object receiver) throws PosixException;
290290

291+
public record OpenPtyResult(int masterFd, int slaveFd) {
292+
}
293+
294+
public abstract OpenPtyResult openpty(Object receiver) throws PosixException;
295+
291296
public abstract TruffleString ctermid(Object receiver) throws PosixException;
292297

293298
// note: this leaks memory in nfi backend and is not synchronized

graalpython/python-libposix/src/posix.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include <sys/mman.h>
7676
#include <unistd.h>
7777
#include <pwd.h>
78+
#include <pty.h>
7879

7980

8081
int64_t call_getpid() {
@@ -581,6 +582,10 @@ int64_t call_setsid() {
581582
return setsid();
582583
}
583584

585+
int32_t call_openpty(int32_t *outvars) {
586+
return openpty(outvars, outvars + 1, NULL, NULL, NULL);
587+
}
588+
584589
int32_t call_ctermid(char *buf) {
585590
return ctermid(buf) == NULL ? -1 : 0;
586591
}

0 commit comments

Comments
 (0)