Skip to content

Commit bcca9fe

Browse files
committed
Merge remote-tracking branch 'origin/master' into tim/GR-9957/dynamic-cast
2 parents c280136 + 045d78b commit bcca9fe

File tree

13 files changed

+230
-134
lines changed

13 files changed

+230
-134
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@
3838
*/
3939
#include "capi.h"
4040

41-
#define FORCE_TO_NATIVE(__obj__) (polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", (__obj__), truffle_is_handle_to_managed((__obj__)) ? (__obj__) : truffle_deref_handle_for_managed(__obj__)))
41+
42+
MUST_INLINE static void force_to_native(void* obj) {
43+
if (polyglot_is_value(obj)) {
44+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", obj, truffle_deref_handle_for_managed(obj));
45+
}
46+
}
4247

4348
static void initialize_type_structure(PyTypeObject* structure, const char* typname, void* typeid) {
4449
PyTypeObject* ptype = (PyTypeObject*)UPCALL_CEXT_O("PyTruffle_Type", polyglot_from_string(typname, SRC_CS));
4550

4651
// We eagerly create a native pointer for all builtin types. This is necessary for pointer comparisons to work correctly.
4752
// TODO Remove this as soon as this is properly supported.
48-
FORCE_TO_NATIVE(ptype);
53+
force_to_native(ptype);
4954

5055
// Store the Sulong struct type id to be used for instances of this class
5156
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_SulongType", ptype, typeid);
@@ -106,37 +111,37 @@ initialize_type(PyEllipsis_Type, ellipsis, _object);
106111
static void initialize_globals() {
107112
// None
108113
PyObject* jnone = UPCALL_CEXT_O("Py_None");
109-
FORCE_TO_NATIVE(jnone);
114+
force_to_native(jnone);
110115
truffle_assign_managed(&_Py_NoneStruct, jnone);
111116

112117
// NotImplemented
113118
void *jnotimpl = UPCALL_CEXT_O("Py_NotImplemented");
114-
FORCE_TO_NATIVE(jnotimpl);
119+
force_to_native(jnotimpl);
115120
truffle_assign_managed(&_Py_NotImplementedStruct, jnotimpl);
116121

117122
// Ellipsis
118123
void *jellipsis = UPCALL_CEXT_O("Py_Ellipsis");
119-
FORCE_TO_NATIVE(jellipsis);
124+
force_to_native(jellipsis);
120125
truffle_assign_managed(&_Py_EllipsisObject, jellipsis);
121126

122127
// True, False
123128
void *jtrue = UPCALL_CEXT_O("Py_True");
124-
FORCE_TO_NATIVE(jtrue);
129+
force_to_native(jtrue);
125130
truffle_assign_managed(&_Py_TrueStruct, jtrue);
126131
void *jfalse = UPCALL_CEXT_O("Py_False");
127-
FORCE_TO_NATIVE(jfalse);
132+
force_to_native(jfalse);
128133
truffle_assign_managed(&_Py_FalseStruct, jfalse);
129134

130135
// error marker
131136
void *jerrormarker = UPCALL_CEXT_O("Py_ErrorHandler");
132-
FORCE_TO_NATIVE(jerrormarker);
137+
force_to_native(jerrormarker);
133138
truffle_assign_managed(&marker_struct, jerrormarker);
134139
}
135140

136141
static void initialize_bufferprocs() {
137-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyBytes_Type), (getbufferproc)bytes_buffer_getbuffer, (releasebufferproc)NULL);
138-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyByteArray_Type), (getbufferproc)NULL, (releasebufferproc)NULL);
139-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyBuffer_Type), (getbufferproc)bufferdecorator_getbuffer, (releasebufferproc)NULL);
142+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyBytes_Type), (getbufferproc)bytes_buffer_getbuffer, (releasebufferproc)NULL);
143+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyByteArray_Type), (getbufferproc)NULL, (releasebufferproc)NULL);
144+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyBuffer_Type), (getbufferproc)bufferdecorator_getbuffer, (releasebufferproc)NULL);
140145
}
141146

142147
__attribute__((constructor))

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/PythonModuleTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class PythonModuleTests {
4646
@Test
4747
public void pythonModuleTest() {
4848
final PythonContext context = PythonTests.getContext();
49-
PythonModule module = context.getCore().factory().createPythonModule("testModule", null);
49+
PythonModule module = context.getCore().factory().createPythonModule("testModule");
5050

5151
assertEquals("testModule", module.getAttribute(__NAME__).toString());
5252
assertEquals("None", module.getAttribute(__DOC__).toString());
@@ -74,7 +74,7 @@ public void builtinsIntTest() {
7474
@Test
7575
public void mainModuleTest() {
7676
final PythonContext context = PythonTests.getContext();
77-
PythonModule main = context.createMainModule(null);
77+
PythonModule main = context.getMainModule();
7878
PythonModule builtins = (PythonModule) main.getAttribute(__BUILTINS__);
7979
PBuiltinFunction abs = (PBuiltinFunction) builtins.getAttribute(BuiltinNames.ABS);
8080
Object returned = InvokeNode.create(abs).invoke(createWithUserArguments(-42));

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

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,103 @@
2727
a = 5
2828

2929
LOG = []
30+
LOG1 = []
31+
LOG2 = []
32+
LOG3 = []
33+
34+
35+
class Context:
36+
37+
def __init__(self, log, suppress_exception, raise_exception):
38+
self._log = log
39+
self._suppress = suppress_exception
40+
self._raise = raise_exception
3041

31-
class Sample:
3242
def __enter__(self):
33-
LOG.append("__enter__")
43+
self._log.append("__enter__")
3444
return self
3545

3646
def __exit__(self, type, value, trace):
37-
LOG.append("type: %s" % type)
38-
LOG.append("value: %s" % value)
47+
self._log.append("type: %s" % type)
48+
self._log.append("value: %s" % value)
3949
# LOG.append("trace: %s" % trace) # trace back is not supported yet
40-
return False
50+
return self._suppress
4151

4252
def do_something(self):
43-
bar = 1/0
53+
self._log.append("do_something")
54+
bar = 1
55+
if self._raise:
56+
bar = bar / 0
4457
return bar + 10
4558

46-
def test_with():
59+
60+
def payload(log, suppress_exception, raise_exception, do_return):
61+
a = 5
4762
try:
48-
with Sample() as sample:
49-
a = 5
50-
sample.do_something()
63+
with Context(log, suppress_exception, raise_exception) as sample:
64+
if do_return:
65+
a = sample.do_something()
66+
return a
67+
else:
68+
a = sample.do_something()
5169
except ZeroDivisionError:
52-
LOG.append("Exception has been thrown correctly")
70+
log.append("Exception has been thrown correctly")
5371

5472
else:
55-
LOG.append("This is not correct!!")
73+
log.append("no exception or exception suppressed")
5674

5775
finally:
58-
LOG.append("a = %s" % a)
76+
log.append("a = %s" % a)
77+
78+
return a
79+
80+
81+
def test_with_dont_suppress():
82+
payload(LOG, False, True, False)
83+
assert LOG == [
84+
"__enter__" ,
85+
"do_something" ,
86+
"type: <class 'ZeroDivisionError'>" ,
87+
"value: division by zero" ,
88+
"Exception has been thrown correctly" ,
89+
"a = 5"
90+
], "was: " + str(LOG)
91+
92+
93+
def test_with_suppress():
94+
payload(LOG1, True, True, False)
95+
assert LOG1 == [ "__enter__" ,
96+
"do_something" ,
97+
"type: <class 'ZeroDivisionError'>" ,
98+
"value: division by zero" ,
99+
"no exception or exception suppressed" ,
100+
"a = 5"
101+
], "was: " + str(LOG1)
102+
103+
104+
def with_return(ctx):
105+
with ctx as sample:
106+
return ctx.do_something()
107+
return None
108+
109+
110+
def test_with_return():
111+
result = payload(LOG2, False, False, True)
112+
assert result == 11
113+
assert LOG2 == [ "__enter__",
114+
"do_something",
115+
"type: None",
116+
"value: None",
117+
"a = 11",
118+
], "was: " + str(LOG2)
119+
59120

60-
assert LOG[0] == "__enter__"
61-
assert LOG[1] == "type: <class 'ZeroDivisionError'>"
62-
assert LOG[2] == "value: division by zero"
63-
assert LOG[3] == "Exception has been thrown correctly"
64-
assert LOG[4] == "a = 5"
121+
def test_with_return_and_exception():
122+
result = payload(LOG3, True, False, True)
123+
assert result == 11
124+
assert LOG3 == [ "__enter__",
125+
"do_something",
126+
"type: None",
127+
"value: None",
128+
"a = 11",
129+
], "was: " + str(LOG3)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ protected CallTarget parse(ParsingRequest request) throws Exception {
247247
if (!pythonCore.isInitialized()) {
248248
pythonCore.initialize();
249249
}
250-
context.getOrCreateMainModule(request.getSource().getPath());
250+
context.initializeMainModule(request.getSource().getPath());
251251

252252
// if we are running the interpreter, module 'site' is automatically imported
253253
if (request.getSource().isInteractive()) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public Object duplicate(Map<Object, Object> replacements, Object value) {
391391
return value;
392392
} else if (value instanceof PythonModule) {
393393
PythonModule module = (PythonModule) value;
394-
PythonModule newModule = factory().createPythonModule(module.getModuleName(), module.getModulePath());
394+
PythonModule newModule = factory().createPythonModule(module.getModuleName());
395395
replacements.put(module, newModule);
396396
for (String attr : module.getAttributeNames()) {
397397
newModule.setAttribute(attr, duplicate(replacements, module.getAttribute(attr)));
@@ -676,7 +676,7 @@ private PythonModule createModule(String name, String pkg, PythonBuiltins... bui
676676
}
677677

678678
public PythonModule createModule(String name, boolean add, PythonBuiltins... builtins) {
679-
PythonModule mod = factory().createPythonModule(name, null);
679+
PythonModule mod = factory().createPythonModule(name);
680680
for (PythonBuiltins builtin : builtins) {
681681
addBuiltinsToModule(mod, builtin);
682682
}
@@ -779,7 +779,7 @@ private void loadFile(String s, String prefix) {
779779
PythonModule mod = lookupBuiltinModule(s);
780780
if (mod == null) {
781781
// use an anonymous module for the side-effects
782-
mod = factory().createPythonModule("__anonymous__", "<bootstrap>");
782+
mod = factory().createPythonModule("__anonymous__");
783783
}
784784
CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedModule.getRootNode());
785785
callTarget.call(PArguments.withGlobals(mod));

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static com.oracle.graal.python.nodes.BuiltinNames.TUPLE;
4545
import static com.oracle.graal.python.nodes.BuiltinNames.TYPE;
4646
import static com.oracle.graal.python.nodes.BuiltinNames.ZIP;
47+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
4748
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
4849
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
4950
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REVERSED__;
@@ -125,6 +126,7 @@
125126
import com.oracle.graal.python.nodes.PGuards;
126127
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
127128
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
129+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
128130
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
129131
import com.oracle.graal.python.nodes.builtins.TupleNodes;
130132
import com.oracle.graal.python.nodes.call.CallDispatchNode;
@@ -137,6 +139,7 @@
137139
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
138140
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
139141
import com.oracle.graal.python.nodes.object.GetClassNode;
142+
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
140143
import com.oracle.graal.python.runtime.JavaTypeConversions;
141144
import com.oracle.graal.python.runtime.PythonParseResult;
142145
import com.oracle.graal.python.runtime.exception.PException;
@@ -150,6 +153,7 @@
150153
import com.oracle.truffle.api.dsl.Fallback;
151154
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
152155
import com.oracle.truffle.api.dsl.Specialization;
156+
import com.oracle.truffle.api.dsl.TypeSystemReference;
153157
import com.oracle.truffle.api.nodes.UnexpectedResultException;
154158
import com.oracle.truffle.api.profiles.ConditionProfile;
155159

@@ -1212,26 +1216,21 @@ public Object typeGeneric(Object cls, Object name, Object bases, Object dict, PK
12121216

12131217
@Builtin(name = MODULE, minNumOfArguments = 2, maxNumOfArguments = 3, constructsClass = {PythonModule.class}, isPublic = false)
12141218
@GenerateNodeFactory
1219+
@TypeSystemReference(PythonArithmeticTypes.class)
12151220
@SuppressWarnings("unused")
12161221
public abstract static class ModuleNode extends PythonBuiltinNode {
1222+
@Child WriteAttributeToObjectNode writeFile = WriteAttributeToObjectNode.create();
1223+
12171224
@Specialization
12181225
public PythonModule module(Object cls, String name, PNone path) {
1219-
return factory().createPythonModule(name, null);
1226+
return factory().createPythonModule(name);
12201227
}
12211228

12221229
@Specialization
12231230
public PythonModule module(Object cls, String name, String path) {
1224-
return factory().createPythonModule(name, path);
1225-
}
1226-
1227-
@Specialization
1228-
public PythonModule module(Object cls, PString name, PNone path) {
1229-
return factory().createPythonModule(name.getValue(), null);
1230-
}
1231-
1232-
@Specialization
1233-
public PythonModule module(Object cls, PString name, String path) {
1234-
return factory().createPythonModule(name.getValue(), path);
1231+
PythonModule module = factory().createPythonModule(name);
1232+
writeFile.execute(module, __FILE__, path);
1233+
return module;
12351234
}
12361235
}
12371236

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,18 @@ private GetClassNode getClassNode() {
11851185
abstract static class PyTruffle_Set_Ptr extends NativeBuiltin {
11861186

11871187
@Specialization
1188-
PythonObjectNativeWrapper doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1189-
nativeWrapper.setNativePointer(ptr);
1190-
return nativeWrapper;
1188+
int doPythonObject(PythonAbstractObject nativeWrapper, TruffleObject ptr) {
1189+
return doNativeWrapper(nativeWrapper.getNativeWrapper(), ptr);
1190+
}
1191+
1192+
@Specialization
1193+
int doNativeWrapper(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1194+
if (nativeWrapper.isNative()) {
1195+
PythonContext.getSingleNativeContextAssumption().invalidate();
1196+
} else {
1197+
nativeWrapper.setNativePointer(ptr);
1198+
}
1199+
return 0;
11911200
}
11921201
}
11931202

@@ -1207,12 +1216,16 @@ Object doPythonObject(PythonClassNativeWrapper klass, Object ptr) {
12071216
abstract static class PyTruffle_SetBufferProcs extends NativeBuiltin {
12081217

12091218
@Specialization
1210-
Object doPythonObject(PythonClass obj, Object getBufferProc, Object releaseBufferProc) {
1211-
PythonClassNativeWrapper nativeWrapper = obj.getNativeWrapper();
1219+
Object doNativeWrapper(PythonClassNativeWrapper nativeWrapper, Object getBufferProc, Object releaseBufferProc) {
12121220
nativeWrapper.setGetBufferProc(getBufferProc);
12131221
nativeWrapper.setReleaseBufferProc(releaseBufferProc);
12141222
return PNone.NO_VALUE;
12151223
}
1224+
1225+
@Specialization
1226+
Object doPythonObject(PythonClass obj, Object getBufferProc, Object releaseBufferProc) {
1227+
return doNativeWrapper(obj.getNativeWrapper(), getBufferProc, releaseBufferProc);
1228+
}
12161229
}
12171230

12181231
@Builtin(name = "PyTruffle_ThreadState_GetDict", fixedNumOfArguments = 0)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeWrappers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Object getNativePointer() {
6262

6363
public void setNativePointer(Object nativePointer) {
6464
// we should set the pointer just once
65-
assert this.nativePointer == null || this.nativePointer.equals(nativePointer);
65+
assert this.nativePointer == null || this.nativePointer.equals(nativePointer) || nativePointer == null;
6666
this.nativePointer = nativePointer;
6767
}
6868

0 commit comments

Comments
 (0)