Skip to content

Commit 3a27343

Browse files
committed
replace __int/long/float__ with asInt/Long/Double where appropriate
1 parent 0805bba commit 3a27343

5 files changed

Lines changed: 27 additions & 84 deletions

File tree

src/org/python/core/PyString.java

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,35 +2714,12 @@ private String formatInteger(long v, int radix, boolean unsigned) {
27142714
return s;
27152715
}
27162716

2717-
private String formatFloatDecimal(PyObject arg, boolean truncate) {
2718-
PyFloat argAsFloat;
2719-
if (arg instanceof PyFloat) {
2720-
// Fast path
2721-
argAsFloat = (PyFloat)arg;
2722-
} else {
2723-
// Use __float__
2724-
if (arg instanceof PyInteger || arg instanceof PyLong) {
2725-
// Typical cases, safe to call __float__:
2726-
argAsFloat = arg.__float__();
2727-
} else {
2728-
try {
2729-
// We can't simply call arg.__float__() because PyString implements
2730-
// it without exposing it to python (i.e, str instances has no
2731-
// __float__ attribute). So, we would support strings as arguments
2732-
// for %g format, which is forbidden by CPython tests (on
2733-
// test_format.py).
2734-
argAsFloat = (PyFloat)arg.__getattr__("__float__").__call__();
2735-
} catch (PyException e) {
2736-
// XXX: Swallow customs AttributeError throws from __float__ methods
2737-
// No better alternative for the moment
2738-
if (e.match(Py.AttributeError)) {
2739-
throw Py.TypeError("float argument required");
2740-
}
2741-
throw e;
2742-
}
2743-
}
2717+
private double asDouble(PyObject obj) {
2718+
try {
2719+
return obj.asDouble();
2720+
} catch (PyException pye) {
2721+
throw !pye.match(Py.TypeError) ? pye : Py.TypeError("float argument required");
27442722
}
2745-
return formatFloatDecimal(argAsFloat.getValue(), truncate);
27462723
}
27472724

27482725
private String formatFloatDecimal(double v, boolean truncate) {
@@ -2772,7 +2749,7 @@ private String formatFloatExponential(PyObject arg, char e,
27722749
boolean truncate)
27732750
{
27742751
StringBuilder buf = new StringBuilder();
2775-
double v = arg.__float__().getValue();
2752+
double v = asDouble(arg);
27762753
boolean isNegative = false;
27772754
if (v < 0) {
27782755
v = -v;
@@ -2971,10 +2948,8 @@ else if (arg instanceof PyInteger || arg instanceof PyFloat) {
29712948
string = formatFloatExponential(arg, c, false);
29722949
break;
29732950
case 'f':
2974-
case 'F':
2975-
string = formatFloatDecimal(arg, false);
2976-
// if (altFlag && string.indexOf('.') == -1)
2977-
// string += '.';
2951+
case 'F':
2952+
string = formatFloatDecimal(asDouble(arg), false);
29782953
break;
29792954
case 'g':
29802955
case 'G':
@@ -2983,15 +2958,15 @@ else if (arg instanceof PyInteger || arg instanceof PyFloat) {
29832958
precision = 6;
29842959
}
29852960

2986-
double v = arg.__float__().getValue();
2961+
double v = asDouble(arg);
29872962
int exponent = (int)ExtraMath.closeFloor(Math.log10(Math.abs(v == 0 ? 1 : v)));
29882963
if (v == Double.POSITIVE_INFINITY) {
29892964
string = "inf";
29902965
} else if (v == Double.NEGATIVE_INFINITY) {
29912966
string = "-inf";
29922967
} else if (exponent >= -4 && exponent < precision) {
29932968
precision -= exponent + 1;
2994-
string = formatFloatDecimal(arg, !altFlag);
2969+
string = formatFloatDecimal(v, !altFlag);
29952970

29962971
// XXX: this block may be unnecessary now
29972972
if (altFlag && string.indexOf('.') == -1) {

src/org/python/modules/_sre.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,34 @@
1212
* CNRI. Hewlett-Packard provided funding for 1.6 integration and
1313
* other compatibility work.
1414
*/
15-
1615
package org.python.modules;
1716

18-
import java.util.Iterator;
19-
import org.python.core.Py;
20-
import org.python.core.PyInteger;
21-
import org.python.core.PyList;
22-
import org.python.core.PyLong;
2317
import org.python.core.PyObject;
2418
import org.python.core.PyString;
2519
import org.python.modules.sre.PatternObject;
2620
import org.python.modules.sre.SRE_STATE;
2721

28-
2922
public class _sre {
3023
public static int MAGIC = SRE_STATE.SRE_MAGIC;
3124

3225
// workaround the fact that H, I types are unsigned, but we are not really using them as such
33-
//XXX: May not be the right size, but I suspect it is -- see sre_compile.py
26+
// XXX: May not be the right size, but I suspect it is -- see sre_compile.py
3427
public static int CODESIZE = 4;
3528

36-
public static PatternObject compile(PyString pattern, int flags,
37-
PyObject code, int groups,
38-
PyObject groupindex,
39-
PyObject indexgroup) {
40-
int[] ccode = null;
41-
if (code instanceof PyList) {
42-
int n = code.__len__();
43-
ccode = new int[n];
44-
int i = 0;
45-
for (PyObject item : code.asIterable()) {
46-
ccode[i] = (int)((PyLong)item.__long__()).getValue().longValue();
47-
i++;
48-
}
49-
} else {
50-
throw Py.TypeError("Expected list");
29+
public static PatternObject compile(PyString pattern, int flags, PyObject code, int groups,
30+
PyObject groupindex, PyObject indexgroup) {
31+
int[] ccode = new int[code.__len__()];
32+
int i = 0;
33+
for (PyObject item : code.asIterable()) {
34+
ccode[i++] = (int)item.asLong();
5135
}
52-
53-
PatternObject po = new PatternObject(pattern,
54-
flags,
55-
ccode,
56-
groups,
57-
groupindex,
58-
indexgroup);
59-
return po;
36+
return new PatternObject(pattern, flags, ccode, groups, groupindex, indexgroup);
6037
}
6138

62-
63-
6439
public static int getcodesize() {
6540
return CODESIZE;
6641
}
6742

68-
6943
public static int getlower(int ch, int flags) {
7044
return SRE_STATE.getlower(ch, flags);
7145
}

src/org/python/modules/jffi/Util.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ public static final int uint32Value(PyObject value) {
8888
}
8989

9090
public static final long int64Value(PyObject value) {
91-
if (value instanceof PyLong) {
92-
return ((PyLong) value).getLong(Long.MIN_VALUE, Long.MAX_VALUE);
93-
} else if (value instanceof PyInteger) {
94-
return value.asInt();
95-
} else {
96-
return ((PyLong) value.__long__()).getLong(Long.MIN_VALUE, Long.MAX_VALUE);
97-
}
91+
return value.asLong();
9892
}
9993

10094
public static final long uint64Value(PyObject value) {

src/org/python/modules/math.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static double exp(double v) {
5353
}
5454

5555
public static double floor(PyObject v) {
56-
return floor(v.__float__().getValue());
56+
return floor(v.asDouble());
5757
}
5858

5959
public static double floor(double v) {
@@ -69,7 +69,7 @@ public static double log(PyObject v, PyObject base) {
6969
if (v instanceof PyLong) {
7070
doubleValue = calculateLongLog((PyLong)v);
7171
} else {
72-
doubleValue = log(v.__float__().getValue());
72+
doubleValue = log(v.asDouble());
7373
}
7474
if (base != null) {
7575
return check(applyLoggedBase(doubleValue, base));
@@ -89,7 +89,7 @@ private static double applyLoggedBase(double loggedValue, PyObject base) {
8989
if (base instanceof PyLong) {
9090
loggedBase = calculateLongLog((PyLong)base);
9191
} else {
92-
loggedBase = log(base.__float__().getValue());
92+
loggedBase = log(base.asDouble());
9393
}
9494
return check(loggedValue / loggedBase);
9595
}
@@ -103,15 +103,15 @@ public static double pow(double v, double w) {
103103
}
104104

105105
public static double sin(PyObject v) {
106-
return sin(v.__float__().getValue());
106+
return sin(v.asDouble());
107107
}
108108

109109
public static double sin(double v) {
110110
return check(Math.sin(v));
111111
}
112112

113113
public static double sqrt(PyObject v) {
114-
return sqrt(v.__float__().getValue());
114+
return sqrt(v.asDouble());
115115
}
116116

117117
public static double sqrt(double v) {
@@ -129,7 +129,7 @@ public static double log10(PyObject v) {
129129
if (x <= 0.0) throw Py.ValueError("math domain error");
130130
return log10(x) + (e[0]*8.0)*log10(2.0);
131131
}
132-
return log10(v.__float__().getValue());
132+
return log10(v.asDouble());
133133
}
134134

135135
private static double log10(double v) {

src/org/python/modules/struct.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ BigInteger get_ulong(PyObject value) {
351351
}
352352

353353
double get_float(PyObject value) {
354-
return value.__float__().getValue();
354+
return value.asDouble();
355355
}
356356

357357

@@ -1000,7 +1000,7 @@ static void pack_into(String format, FormatDef[] f, int size, int argstart, PyOb
10001000
throw Py.TypeError("pack_into takes an array arg"); // as well as a buffer, what else?
10011001
}
10021002
PyArray buffer = (PyArray)args[argstart];
1003-
int offset = args[argstart + 1].__int__().asInt();
1003+
int offset = args[argstart + 1].asInt();
10041004

10051005
ByteStream res = pack(format, f, size, argstart + 2, args);
10061006
if (res.pos > buffer.__len__()) {

0 commit comments

Comments
 (0)