Skip to content

Commit e54499b

Browse files
committed
fix java.instanceof
- fix (c), checkstyle
1 parent eca328d commit e54499b

File tree

4 files changed

+179
-87
lines changed

4 files changed

+179
-87
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advance/CustomBuiltinsTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
141
package com.oracle.graal.python.test.advance;
242

343
import org.junit.Test;

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advance/CustomModule.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
141
package com.oracle.graal.python.test.advance;
242

343
import java.util.ArrayList;
@@ -22,4 +62,4 @@ public void initialize(PythonCore core) {
2262
super.initialize(core);
2363
this.builtinConstants.put("success", "success");
2464
}
25-
}
65+
}

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

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -192,84 +192,85 @@ public final class Python3Core implements PythonCore {
192192
private final PythonBuiltins[] builtins;
193193

194194
private static final PythonBuiltins[] initializeBuiltins() {
195-
List<PythonBuiltins> builtins = new ArrayList<>(Arrays.asList(new BuiltinConstructors(),
196-
new BuiltinFunctions(),
197-
new InteropModuleBuiltins(),
198-
new ObjectBuiltins(),
199-
new CellBuiltins(),
200-
new BoolBuiltins(),
201-
new FloatBuiltins(),
202-
new BytesBuiltins(),
203-
new ComplexBuiltins(),
204-
new ByteArrayBuiltins(),
205-
new TypeBuiltins(),
206-
new IntBuiltins(),
207-
new TruffleObjectBuiltins(),
208-
new ListBuiltins(),
209-
new DictBuiltins(),
210-
new DictViewBuiltins(),
211-
new DictValuesBuiltins(),
212-
new DictKeysIteratorBuiltins(),
213-
new DictValuesIteratorBuiltins(),
214-
new DictItemsIteratorBuiltins(),
215-
new RangeBuiltins(),
216-
new SliceBuiltins(),
217-
new TupleBuiltins(),
218-
new StringBuiltins(),
219-
new SetBuiltins(),
220-
new FrozenSetBuiltins(),
221-
new IteratorBuiltins(),
222-
new ReversedBuiltins(),
223-
new PZipBuiltins(),
224-
new EnumerateBuiltins(),
225-
new SentinelIteratorBuiltins(),
226-
new ForeignIteratorBuiltins(),
227-
new GeneratorBuiltins(),
228-
new AbstractFunctionBuiltins(),
229-
new FunctionBuiltins(),
230-
new BuiltinFunctionBuiltins(),
231-
new AbstractMethodBuiltins(),
232-
new MethodBuiltins(),
233-
new BuiltinMethodBuiltins(),
234-
new CodeBuiltins(),
235-
new FrameBuiltins(),
236-
new MappingproxyBuiltins(),
237-
new GetSetDescriptorTypeBuiltins(),
238-
new BaseExceptionBuiltins(),
239-
new PosixModuleBuiltins(),
240-
new ImpModuleBuiltins(),
241-
new ArrayModuleBuiltins(),
242-
new ArrayBuiltins(),
243-
new TimeModuleBuiltins(),
244-
new MathModuleBuiltins(),
245-
new MarshalModuleBuiltins(),
246-
new RandomModuleBuiltins(),
247-
new RandomBuiltins(),
248-
new TruffleCextBuiltins(),
249-
new WeakRefModuleBuiltins(),
250-
new ReferenceTypeBuiltins(),
251-
new IOModuleBuiltins(),
252-
new StringModuleBuiltins(),
253-
new ItertoolsModuleBuiltins(),
254-
new FunctoolsModuleBuiltins(),
255-
new ErrnoModuleBuiltins(),
256-
new CodecsModuleBuiltins(),
257-
new CollectionsModuleBuiltins(),
258-
new JavaModuleBuiltins(),
259-
new SREModuleBuiltins(),
260-
new AstModuleBuiltins(),
261-
new SelectModuleBuiltins(),
262-
new SignalModuleBuiltins(),
263-
new TracebackBuiltins(),
264-
new GcModuleBuiltins(),
265-
new AtexitModuleBuiltins(),
266-
new FaulthandlerModuleBuiltins(),
267-
new UnicodeDataModuleBuiltins(),
268-
new LocaleModuleBuiltins(),
269-
new SysModuleBuiltins(),
270-
new BufferBuiltins(),
271-
new MemoryviewBuiltins(),
272-
new SuperBuiltins()));
195+
List<PythonBuiltins> builtins = new ArrayList<>(Arrays.asList(
196+
new BuiltinConstructors(),
197+
new BuiltinFunctions(),
198+
new InteropModuleBuiltins(),
199+
new ObjectBuiltins(),
200+
new CellBuiltins(),
201+
new BoolBuiltins(),
202+
new FloatBuiltins(),
203+
new BytesBuiltins(),
204+
new ComplexBuiltins(),
205+
new ByteArrayBuiltins(),
206+
new TypeBuiltins(),
207+
new IntBuiltins(),
208+
new TruffleObjectBuiltins(),
209+
new ListBuiltins(),
210+
new DictBuiltins(),
211+
new DictViewBuiltins(),
212+
new DictValuesBuiltins(),
213+
new DictKeysIteratorBuiltins(),
214+
new DictValuesIteratorBuiltins(),
215+
new DictItemsIteratorBuiltins(),
216+
new RangeBuiltins(),
217+
new SliceBuiltins(),
218+
new TupleBuiltins(),
219+
new StringBuiltins(),
220+
new SetBuiltins(),
221+
new FrozenSetBuiltins(),
222+
new IteratorBuiltins(),
223+
new ReversedBuiltins(),
224+
new PZipBuiltins(),
225+
new EnumerateBuiltins(),
226+
new SentinelIteratorBuiltins(),
227+
new ForeignIteratorBuiltins(),
228+
new GeneratorBuiltins(),
229+
new AbstractFunctionBuiltins(),
230+
new FunctionBuiltins(),
231+
new BuiltinFunctionBuiltins(),
232+
new AbstractMethodBuiltins(),
233+
new MethodBuiltins(),
234+
new BuiltinMethodBuiltins(),
235+
new CodeBuiltins(),
236+
new FrameBuiltins(),
237+
new MappingproxyBuiltins(),
238+
new GetSetDescriptorTypeBuiltins(),
239+
new BaseExceptionBuiltins(),
240+
new PosixModuleBuiltins(),
241+
new ImpModuleBuiltins(),
242+
new ArrayModuleBuiltins(),
243+
new ArrayBuiltins(),
244+
new TimeModuleBuiltins(),
245+
new MathModuleBuiltins(),
246+
new MarshalModuleBuiltins(),
247+
new RandomModuleBuiltins(),
248+
new RandomBuiltins(),
249+
new TruffleCextBuiltins(),
250+
new WeakRefModuleBuiltins(),
251+
new ReferenceTypeBuiltins(),
252+
new IOModuleBuiltins(),
253+
new StringModuleBuiltins(),
254+
new ItertoolsModuleBuiltins(),
255+
new FunctoolsModuleBuiltins(),
256+
new ErrnoModuleBuiltins(),
257+
new CodecsModuleBuiltins(),
258+
new CollectionsModuleBuiltins(),
259+
new JavaModuleBuiltins(),
260+
new SREModuleBuiltins(),
261+
new AstModuleBuiltins(),
262+
new SelectModuleBuiltins(),
263+
new SignalModuleBuiltins(),
264+
new TracebackBuiltins(),
265+
new GcModuleBuiltins(),
266+
new AtexitModuleBuiltins(),
267+
new FaulthandlerModuleBuiltins(),
268+
new UnicodeDataModuleBuiltins(),
269+
new LocaleModuleBuiltins(),
270+
new SysModuleBuiltins(),
271+
new BufferBuiltins(),
272+
new MemoryviewBuiltins(),
273+
new SuperBuiltins()));
273274
if (!TruffleOptions.AOT) {
274275
ServiceLoader<PythonBuiltins> providers = ServiceLoader.load(PythonBuiltins.class);
275276
for (PythonBuiltins builtin : providers) {

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
import com.oracle.truffle.api.dsl.Specialization;
5858
import com.oracle.truffle.api.interop.TruffleObject;
5959

60+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
61+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
62+
6063
@CoreFunctions(defineModule = "java")
6164
public class JavaModuleBuiltins extends PythonBuiltins {
6265
@Override
@@ -132,27 +135,35 @@ abstract static class InstanceOfNode extends PythonBinaryBuiltinNode {
132135
@Specialization(guards = {"!isForeignObject(object)", "isForeignObject(klass)"})
133136
boolean check(Object object, TruffleObject klass) {
134137
Env env = getContext().getEnv();
135-
Object hostKlass = env.asHostObject(klass);
136-
if (hostKlass instanceof Class<?>) {
137-
return ((Class<?>) hostKlass).isInstance(object);
138+
try {
139+
Object hostKlass = env.asHostObject(klass);
140+
if (hostKlass instanceof Class<?>) {
141+
return ((Class<?>) hostKlass).isInstance(object);
142+
}
143+
} catch (ClassCastException cce) {
144+
throw raise(ValueError, "klass argument '%p' is not a host object", klass);
138145
}
139146
return false;
140147
}
141148

142149
@Specialization(guards = {"isForeignObject(object)", "isForeignObject(klass)"})
143150
boolean checkForeign(Object object, TruffleObject klass) {
144151
Env env = getContext().getEnv();
145-
Object hostObject = env.asHostObject(object);
146-
Object hostKlass = env.asHostObject(klass);
147-
if (hostKlass instanceof Class<?>) {
148-
return ((Class<?>) hostKlass).isInstance(hostObject);
152+
try {
153+
Object hostObject = env.asHostObject(object);
154+
Object hostKlass = env.asHostObject(klass);
155+
if (hostKlass instanceof Class<?>) {
156+
return ((Class<?>) hostKlass).isInstance(hostObject);
157+
}
158+
} catch (ClassCastException cce) {
159+
throw raise(ValueError, "the object '%p' or klass '%p' arguments is not a host object", object, klass);
149160
}
150161
return false;
151162
}
152163

153164
@Fallback
154165
boolean fallback(Object object, Object klass) {
155-
throw raise(PythonErrorType.TypeError, "unsupported instanceof(%p, %p)", object, klass);
166+
throw raise(TypeError, "unsupported instanceof(%p, %p)", object, klass);
156167
}
157168
}
158169
}

0 commit comments

Comments
 (0)