Skip to content

Commit 03a719e

Browse files
committed
Support memoryview in os.write()
1 parent dac35b7 commit 03a719e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
import com.oracle.truffle.api.dsl.Specialization;
162162
import com.oracle.truffle.api.dsl.TypeSystemReference;
163163
import com.oracle.truffle.api.frame.VirtualFrame;
164+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
164165
import com.oracle.truffle.api.library.CachedLibrary;
165166
import com.oracle.truffle.api.profiles.BranchProfile;
166167
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -1202,6 +1203,21 @@ Object write(VirtualFrame frame, int fd, PBytesLike data,
12021203
return write(frame, fd, getByteArray(data.getSequenceStorage()), channelClassProfile);
12031204
}
12041205

1206+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
1207+
Object write(VirtualFrame frame, int fd, Object data,
1208+
@CachedLibrary("data") PythonObjectLibrary lib,
1209+
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
1210+
if (lib.isBuffer(data)) {
1211+
try {
1212+
byte[] b = lib.getBufferBytes(data);
1213+
return write(frame, fd, b, channelClassProfile);
1214+
} catch (UnsupportedMessageException e) {
1215+
throw CompilerDirectives.shouldNotReachHere("Buffer-like object does not support getBufferBytes()");
1216+
}
1217+
}
1218+
throw raise(TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, data);
1219+
}
1220+
12051221
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
12061222
static Object writePInt(VirtualFrame frame, Object fd, Object data,
12071223
@CachedLibrary("fd") PythonObjectLibrary lib,

0 commit comments

Comments
 (0)