Skip to content

Commit 8f3669f

Browse files
committed
[GR-34916] Intrinsify python_cext - PyLong/Float/Complex/Number_XXX.
PullRequest: graalpython/2041
2 parents 247fe97 + e97ab5d commit 8f3669f

File tree

11 files changed

+600
-129
lines changed

11 files changed

+600
-129
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ static PyObject* null_error(void) {
5858

5959
UPCALL_ID(PyNumber_Check);
6060
int PyNumber_Check(PyObject *o) {
61-
PyObject *result = UPCALL_CEXT_O(_jls_PyNumber_Check, native_to_java(o));
62-
if(result == Py_True) {
63-
return 1;
64-
}
65-
return 0;
61+
return UPCALL_CEXT_I(_jls_PyNumber_Check, native_to_java(o));
6662
}
6763

6864
typedef PyObject *(*unaryop_fun_t)(PyObject *, int32_t);

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_abstract.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,26 @@ def _safe_check(v, type_check):
5151

5252
def _reference_checknumber(args):
5353
v = args[0]
54+
if isinstance(v, str):
55+
return False
5456
return _safe_check(v, lambda x: isinstance(int(x), int)) or _safe_check(v, lambda x: isinstance(float(x), float))
5557

58+
def _reference_tobase(args):
59+
n = args[0]
60+
base = args[1]
61+
if base not in (2, 8, 10, 16):
62+
raise SystemError("PyNumber_ToBase: base must be 2, 8, 10 or 16")
63+
if not hasattr(n, "__index__"):
64+
raise TypeError
65+
b_index = n.__index__()
66+
if base == 2:
67+
return bin(b_index)
68+
elif base == 8:
69+
return oct(b_index)
70+
elif base == 10:
71+
return str(b_index)
72+
elif base == 16:
73+
return hex(b_index)
5674

5775
def _reference_index(args):
5876
v = args[0]
@@ -205,6 +223,7 @@ def _default_unarop_args():
205223
(False,),
206224
(True,),
207225
("hello",),
226+
("1",),
208227
((1, 2, 3),),
209228
(0x7fffffff,),
210229
(0xffffffffffffffffffffffffffffffff,),
@@ -259,6 +278,55 @@ def compile_module(self, name):
259278
cmpfunc=unhandled_error_compare
260279
)
261280

281+
test_PyNumber_ToBase = CPyExtFunction(
282+
_reference_tobase,
283+
lambda: (
284+
(2, 0),
285+
("hello", 0),
286+
(1.1, 0),
287+
(NoNumber(), 0),
288+
(2, 0),
289+
(2, 0),
290+
(2, 2),
291+
(2, 8),
292+
(2, 10),
293+
(2, 16),
294+
("1", 2),
295+
("1", 8),
296+
("1", 10),
297+
("1", 16),
298+
(1.1, 2),
299+
(1.1, 8),
300+
(1.1, 10),
301+
(1.1, 16),
302+
(False, 2),
303+
(False, 8),
304+
(False, 10),
305+
(False, 16),
306+
(True, 16),
307+
("hello, ", 2),
308+
("hello, ", 8),
309+
("hello, ", 10),
310+
("hello, ", 16),
311+
(DummyIntable(), 2),
312+
(DummyIntable(), 8),
313+
(DummyIntable(), 10),
314+
(DummyIntable(), 16),
315+
(DummyIntSubclass(), 2),
316+
(DummyIntSubclass(), 8),
317+
(DummyIntSubclass(), 10),
318+
(DummyIntSubclass(), 16),
319+
(NoNumber(), 2),
320+
(NoNumber(), 8),
321+
(NoNumber(), 10),
322+
(NoNumber(), 16),
323+
),
324+
resultspec="O",
325+
argspec='Oi',
326+
arguments=["PyObject* n", "int base"],
327+
cmpfunc=unhandled_error_compare
328+
)
329+
262330
test_PyNumber_Add = CPyExtFunction(
263331
lambda args: args[0] + args[1],
264332
lambda: (
@@ -482,6 +550,7 @@ def compile_module(self, name):
482550
(1,),
483551
(-1,),
484552
(1.0,),
553+
("1",),
485554
(0x7FFFFFFF,),
486555
(0x7FFFFFFFFFFFFFFF,),
487556
(DummyIntable(),),
@@ -522,6 +591,7 @@ def compile_module(self, name):
522591
(1,),
523592
(-1,),
524593
(1.0,),
594+
("1",),
525595
(0x7FFFFFFF,),
526596
(0x7FFFFFFFFFFFFFFF,),
527597
(DummyIntable(),),

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_complex.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ def _reference_realasdouble(args):
6565
raise SystemError
6666
else:
6767
return -1.0
68-
68+
69+
def _reference_fromdoubles(args):
70+
if isinstance(args[0], float) and isinstance(args[1], float):
71+
return complex(args[0], args[1])
72+
raise SystemError
6973

7074
class DummyNonComplex():
7175
pass
@@ -167,3 +171,15 @@ def compile_module(self, name):
167171
arguments=["PyObject* obj"],
168172
cmpfunc=unhandled_error_compare
169173
)
174+
175+
test_PyComplex_FromDoubles = CPyExtFunction(
176+
_reference_fromdoubles,
177+
lambda: (
178+
(float(0.0), float(2.0), ),
179+
(1.0, 2.0, ),
180+
),
181+
resultspec="O",
182+
argspec='dd',
183+
arguments=["double r", "double i"],
184+
cmpfunc=unhandled_error_compare
185+
)

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
4242
__dir__ = __file__.rpartition("/")[0]
4343

44-
44+
def raise_Py6_SystemError():
45+
if sys.version_info.minor >= 6:
46+
raise SystemError
47+
else:
48+
return -1
49+
4550
def _reference_aslong(args):
4651
# We cannot be sure if we are on 32-bit or 64-bit architecture. So, assume the smaller one.
4752
n = int(args[0])
@@ -88,6 +93,14 @@ def _reference_fromlong(args):
8893
n = args[0]
8994
return n
9095

96+
def _reference_sign(args):
97+
n = args[0]
98+
if n==0:
99+
return 0
100+
elif n < 0:
101+
return -1
102+
else:
103+
return 1
91104

92105
class DummyNonInt():
93106
pass
@@ -395,3 +408,19 @@ def compile_module(self, name):
395408
argspec="OniiO",
396409
arguments=["PyObject* object", "Py_ssize_t n", "int little_endian", "int is_signed", "PyObject* unused"],
397410
)
411+
412+
test__PyLong_Sign = CPyExtFunction(
413+
_reference_sign,
414+
lambda: (
415+
(0,),
416+
(-1,),
417+
(0xffffffff,),
418+
(0xfffffffffffffffffffffff,),
419+
(True,),
420+
(False,),
421+
),
422+
resultspec="i",
423+
argspec='O',
424+
arguments=["PyObject* o"],
425+
cmpfunc=unhandled_error_compare
426+
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public PByteArray setEmpty(Object cls, @SuppressWarnings("unused") Object arg) {
356356
"Create a complex number from a real part and an optional imaginary part.\n" +
357357
"This is equivalent to (real + imag*1j) where imag defaults to 0.")
358358
@GenerateNodeFactory
359-
public abstract static class ComplexNode extends PythonBuiltinNode {
359+
public abstract static class ComplexNode extends PythonTernaryBuiltinNode {
360360

361361
@Child private IsBuiltinClassProfile isPrimitiveProfile = IsBuiltinClassProfile.create();
362362
@Child private IsBuiltinClassProfile isComplexTypeProfile;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
import com.oracle.graal.python.builtins.PythonBuiltins;
9292
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory;
9393
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GlobalsNodeFactory;
94+
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.HexNodeFactory;
95+
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.OctNodeFactory;
9496
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
9597
import com.oracle.graal.python.builtins.modules.io.IOModuleBuiltins;
9698
import com.oracle.graal.python.builtins.modules.io.IONodes;
@@ -611,6 +613,10 @@ protected String longToString(long x) {
611613
protected String prefix() {
612614
return "0o";
613615
}
616+
617+
public static OctNode create() {
618+
return OctNodeFactory.create();
619+
}
614620
}
615621

616622
// hex(object)
@@ -634,6 +640,10 @@ protected String longToString(long x) {
634640
protected String prefix() {
635641
return "0x";
636642
}
643+
644+
public static HexNode create() {
645+
return HexNodeFactory.create();
646+
}
637647
}
638648

639649
// callable(object)

0 commit comments

Comments
 (0)