Skip to content

Commit 5e4b219

Browse files
committed
Properly wrap and convert 'True' and 'False'.
1 parent fe5fdab commit 5e4b219

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ static void initialize_type_structure(PyTypeObject* structure, const char* typna
4747
}
4848

4949
static void initialize_globals() {
50+
// None
5051
void *jnone = polyglot_as__object(to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT, "Py_None")));
5152
truffle_assign_managed(&_Py_NoneStruct, jnone);
53+
54+
// NotImplemented
5255
void *jnotimpl = polyglot_as__object(polyglot_get_member(PY_BUILTIN, "NotImplemented"));
5356
truffle_assign_managed(&_Py_NotImplementedStruct, jnotimpl);
57+
58+
// True, False
5459
void *jtrue = polyglot_invoke(PY_TRUFFLE_CEXT, "Py_True");
55-
truffle_assign_managed(&_Py_TrueStruct, to_sulong(jtrue));
60+
truffle_assign_managed(&_Py_TrueStruct, polyglot_as__longobject(to_sulong(jtrue)));
5661
void *jfalse = polyglot_invoke(PY_TRUFFLE_CEXT, "Py_False");
57-
truffle_assign_managed(&_Py_FalseStruct, to_sulong(jfalse));
62+
truffle_assign_managed(&_Py_FalseStruct, polyglot_as__longobject(to_sulong(jfalse)));
5863
}
5964

6065
__attribute__((constructor))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ POLYGLOT_DECLARE_TYPE(PyListObject);
6363
POLYGLOT_DECLARE_TYPE(PyDictObject);
6464
POLYGLOT_DECLARE_TYPE(PyUnicodeObject);
6565
POLYGLOT_DECLARE_TYPE(PyBytesObject);
66+
POLYGLOT_DECLARE_STRUCT(_longobject);
6667

6768

6869
extern void* to_java(PyObject* obj);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the
16+
# Software is contributed by such licensors),
17+
#
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
#
26+
# The above copyright notice and either this complete permission notice or at a
27+
# minimum a reference to the UPL must be included in all copies or substantial
28+
# portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
import sys
39+
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
40+
__dir__ = __file__.rpartition("/")[0]
41+
42+
43+
class DummyNonInt():
44+
pass
45+
46+
47+
class DummyIntable():
48+
def __int__(self):
49+
return 0xCAFE
50+
51+
52+
class DummyIntSubclass(float):
53+
def __int__(self):
54+
return 0xBABE
55+
56+
57+
class TestPyBool(CPyExtTestCase):
58+
def compile_module(self, name):
59+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
60+
super(TestPyBool, self).compile_module(name)
61+
62+
test_PyBool_Check = CPyExtFunction(
63+
lambda args: isinstance(args[0], bool),
64+
lambda: (
65+
(0,),
66+
(1,),
67+
(True,),
68+
(False,),
69+
(-1,),
70+
(0xffffffff,),
71+
(0xfffffffffffffffffffffff,),
72+
("hello",),
73+
(DummyNonInt(),),
74+
(DummyIntable(),),
75+
(DummyIntSubclass(),),
76+
),
77+
resultspec="i",
78+
argspec='O',
79+
arguments=["PyObject* o"],
80+
cmpfunc=unhandled_error_compare
81+
)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bool/BoolBuiltins.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4444
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4545
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
46+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4647
import com.oracle.truffle.api.dsl.Fallback;
4748
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4849
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -134,11 +135,16 @@ Object doGeneric(Object left, Object right) {
134135

135136
@Builtin(name = __INT__, fixedNumOfArguments = 1)
136137
@GenerateNodeFactory
137-
abstract static class IntNode extends PythonBinaryBuiltinNode {
138+
abstract static class IntNode extends PythonUnaryBuiltinNode {
138139
@Specialization
139140
int op(boolean self) {
140141
return self ? 1 : 0;
141142
}
143+
144+
@Fallback
145+
Object doGeneric(@SuppressWarnings("unused") Object self) {
146+
return PNotImplemented.NOT_IMPLEMENTED;
147+
}
142148
}
143149

144150
@Builtin(name = __INDEX__, fixedNumOfArguments = 1)
@@ -148,10 +154,15 @@ abstract static class IndexNode extends IntNode {
148154

149155
@Builtin(name = __FLOAT__, fixedNumOfArguments = 1)
150156
@GenerateNodeFactory
151-
abstract static class FloatNode extends PythonBinaryBuiltinNode {
157+
abstract static class FloatNode extends PythonUnaryBuiltinNode {
152158
@Specialization
153159
double op(boolean self) {
154160
return self ? 1.0 : 0.0;
155161
}
162+
163+
@Fallback
164+
Object doGeneric(@SuppressWarnings("unused") Object self) {
165+
return PNotImplemented.NOT_IMPLEMENTED;
166+
}
156167
}
157168
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public PythonNativeObject createNativeObjectWrapper(Object obj) {
203203
*/
204204

205205
public PInt createInt(boolean value) {
206-
return trace(new PInt(lookupClass(PythonBuiltinClassType.PInt), value ? BigInteger.ONE : BigInteger.ZERO));
206+
return trace(new PInt(lookupClass(PythonBuiltinClassType.Boolean), value ? BigInteger.ONE : BigInteger.ZERO));
207207
}
208208

209209
public PInt createInt(int value) {

0 commit comments

Comments
 (0)