Skip to content

Commit fcc1417

Browse files
committed
[GR-10614] Missing implementation of special functions from Math module.
PullRequest: graalpython/99
2 parents 6d07363 + 9fb519b commit fcc1417

File tree

7 files changed

+1405
-97
lines changed

7 files changed

+1405
-97
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
# Copyright (C) 1996-2017 Python Software Foundation
3+
#
4+
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
6+
import unittest
7+
8+
class MyIndexable(object):
9+
def __init__(self, value):
10+
self.value = value
11+
def __index__(self):
12+
return self.value
13+
14+
class BuiltinTest(unittest.TestCase):
15+
def test_bin(self):
16+
self.assertEqual(bin(0), '0b0')
17+
self.assertEqual(bin(1), '0b1')
18+
self.assertEqual(bin(-1), '-0b1')
19+
self.assertEqual(bin(2**65), '0b1' + '0' * 65)
20+
self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
21+
self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
22+
self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
23+
24+
# test of specializations
25+
self.assertEqual(bin(MyIndexable(False)), '0b0')
26+
self.assertEqual(bin(MyIndexable(True)), '0b1')
27+
self.assertEqual(bin(MyIndexable(-(2**65))), '-0b1' + '0' * 65)

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

Lines changed: 426 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE;
2929
import static com.oracle.graal.python.builtins.objects.PNotImplemented.NOT_IMPLEMENTED;
3030
import static com.oracle.graal.python.nodes.BuiltinNames.ABS;
31+
import static com.oracle.graal.python.nodes.BuiltinNames.BIN;
3132
import static com.oracle.graal.python.nodes.BuiltinNames.CALLABLE;
3233
import static com.oracle.graal.python.nodes.BuiltinNames.CHR;
3334
import static com.oracle.graal.python.nodes.BuiltinNames.COMPILE;
@@ -120,6 +121,7 @@
120121
import com.oracle.graal.python.nodes.object.GetClassNode;
121122
import com.oracle.graal.python.nodes.subscript.GetItemNode;
122123
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
124+
import com.oracle.graal.python.nodes.util.CastToIntNode;
123125
import com.oracle.graal.python.runtime.PythonContext;
124126
import com.oracle.graal.python.runtime.PythonCore;
125127
import com.oracle.graal.python.runtime.PythonOptions;
@@ -198,6 +200,54 @@ public Object absObject(Object object,
198200
}
199201
}
200202

203+
// bin(object)
204+
@Builtin(name = BIN, fixedNumOfArguments = 1)
205+
@TypeSystemReference(PythonArithmeticTypes.class)
206+
@GenerateNodeFactory
207+
public abstract static class BinNode extends PythonUnaryBuiltinNode {
208+
209+
public abstract String executeObject(Object x);
210+
211+
private static String buildString(boolean isNegative, String number) {
212+
StringBuilder sb = new StringBuilder();
213+
if (isNegative) {
214+
sb.append('-');
215+
}
216+
sb.append("0b");
217+
sb.append(number);
218+
return sb.toString();
219+
}
220+
221+
@Specialization
222+
public String doL(long x) {
223+
return buildString(x < 0, Long.toBinaryString(Math.abs(x)));
224+
}
225+
226+
@Specialization
227+
public String doD(double x) {
228+
throw raise(TypeError, "'%p' object cannot be interpreted as an integer", x);
229+
}
230+
231+
@Specialization
232+
@TruffleBoundary
233+
public String doPI(PInt x) {
234+
BigInteger value = x.getValue();
235+
return buildString(value.compareTo(BigInteger.ZERO) == -1, value.abs().toString(2));
236+
}
237+
238+
@Specialization
239+
public String doO(Object x,
240+
@Cached("create()") CastToIntNode toIntNode,
241+
@Cached("create()") BinNode recursiveNode) {
242+
Object value = toIntNode.execute(x);
243+
return recursiveNode.executeObject(value);
244+
}
245+
246+
protected BinNode create() {
247+
return BuiltinFunctionsFactory.BinNodeFactory.create();
248+
}
249+
}
250+
201251
// callable(object)
202252
@Builtin(name = CALLABLE, fixedNumOfArguments = 1)
203253
@GenerateNodeFactory

0 commit comments

Comments
 (0)