Skip to content

Commit cfd8054

Browse files
committed
simple ctypes that map to Python types (because we can pass these to sulong)
1 parent 199e839 commit cfd8054

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ private static final String[] initializeCoreFiles() {
216216
"_sysconfig",
217217
"_socket",
218218
"_thread",
219+
"ctypes",
219220
"zipimport"));
220221

221222
return coreFiles.toArray(new String[coreFiles.size()]);

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,46 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.util.ArrayList;
4443
import java.util.List;
4544

45+
import com.oracle.graal.python.builtins.Builtin;
4646
import com.oracle.graal.python.builtins.CoreFunctions;
4747
import com.oracle.graal.python.builtins.PythonBuiltins;
48+
import com.oracle.graal.python.builtins.objects.PNone;
49+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
50+
import com.oracle.graal.python.builtins.objects.str.PString;
4851
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
52+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
53+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4954
import com.oracle.truffle.api.dsl.NodeFactory;
55+
import com.oracle.truffle.api.dsl.Specialization;
5056

5157
@CoreFunctions(defineModule = "ctypes")
5258
public class CtypesModuleBuiltins extends PythonBuiltins {
5359
@Override
5460
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
55-
return new ArrayList<>();
61+
return CtypesModuleBuiltinsFactory.getFactories();
62+
}
63+
64+
@Builtin(name = "c_char_p_", fixedNumOfPositionalArgs = 1)
65+
@GenerateNodeFactory
66+
abstract static class CCharP extends PythonUnaryBuiltinNode {
67+
@Child CExtNodes.AsCharPointer asCharPointer = CExtNodes.AsCharPointer.create();
68+
@Child CExtNodes.ToSulongNode toSulongNode = CExtNodes.ToSulongNode.create();
69+
70+
@Specialization
71+
Object defaultValue(@SuppressWarnings("unused") PNone noValue) {
72+
return toSulongNode.execute(PNone.NO_VALUE); // NULL
73+
}
74+
75+
@Specialization
76+
Object withValue(String value) {
77+
return asCharPointer.execute(value);
78+
}
79+
80+
@Specialization
81+
Object withValue(PString value) {
82+
return asCharPointer.execute(value.getValue());
83+
}
5684
}
5785
}

graalpython/lib-graalpython/ctypes.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
class c_bool(bool):
2+
@staticmethod
3+
def size():
4+
return 1
5+
6+
7+
class c_char(int):
8+
@staticmethod
9+
def size():
10+
return 1
11+
12+
13+
class c_byte(int):
14+
@staticmethod
15+
def size():
16+
return 1
17+
18+
19+
class c_ubyte(int):
20+
@staticmethod
21+
def size():
22+
return 1
23+
24+
25+
class c_short(int):
26+
@staticmethod
27+
def size():
28+
return 2
29+
30+
31+
class c_ushort(int):
32+
@staticmethod
33+
def size():
34+
return 2
35+
36+
37+
class c_int(int):
38+
@staticmethod
39+
def size():
40+
return 4
41+
42+
43+
class c_uint(int):
44+
@staticmethod
45+
def size():
46+
return 4
47+
48+
49+
class c_long(int):
50+
@staticmethod
51+
def size():
52+
return 8
53+
54+
55+
class c_ulong(int):
56+
@staticmethod
57+
def size():
58+
return 8
59+
60+
61+
class c_longlong(int):
62+
@staticmethod
63+
def size():
64+
return 16
65+
66+
67+
class c_ulonglong(int):
68+
@staticmethod
69+
def size():
70+
return 16
71+
72+
73+
class c_size_t(int):
74+
@staticmethod
75+
def size():
76+
return 8
77+
78+
79+
class c_ssize_t(int):
80+
@staticmethod
81+
def size():
82+
return 8
83+
84+
85+
class c_float(float):
86+
@staticmethod
87+
def size():
88+
return 4
89+
90+
91+
class c_double(float):
92+
@staticmethod
93+
def size():
94+
return 8
95+
96+
97+
class c_char_p():
98+
def __init__(cls, value=None):
99+
return c_char_p_(value)
100+
101+
@staticmethod
102+
def size():
103+
return 8
104+
105+
106+
class c_void_p(int):
107+
@staticmethod
108+
def size():
109+
return 8
110+
111+
112+
def sizeof(t):
113+
return t.size()

0 commit comments

Comments
 (0)