Skip to content

Commit fdd2812

Browse files
committed
Add templates for int, float, str method classes
Mostly unchanged from VSJ4. [skip-ci]
1 parent af35475 commit fdd2812

File tree

5 files changed

+756
-0
lines changed

5 files changed

+756
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c)2025 Jython Developers.
2+
// Licensed to PSF under a contributor agreement.
3+
package uk.co.farowl.vsj4.runtime;
4+
5+
import java.math.BigInteger;
6+
import static uk.co.farowl.vsj4.runtime.PyLong.convertToDouble;
7+
import static uk.co.farowl.vsj4.runtime.PyFloat.nonzero;
8+
9+
// $OBJECT_GENERATOR$ PyFloatGenerator
10+
11+
/**
12+
* This class contains static methods implementing operations on the
13+
* Python {@code float} object, supplementary to those defined in
14+
* {@link PyFloat} and {@link PyFloatMethods}. These exist in order to
15+
* support {@code CallSite} creation.
16+
* <p>
17+
* Implementations are not allowed to return {@link Py#NotImplemented}.
18+
* If a binary operation is not defined here, for the pair of Java
19+
* classes that type the arguments, the operation is not defined for the
20+
* pair of their Python types.
21+
* <p>
22+
* When matching, we allow a signature here to match if it matches a
23+
* type assignable in Java to the class of the argument in question.
24+
* Types providing this compatibility must not be too broad,
25+
* {@code Object} for example, as this would make it necessary to return
26+
* {@link Py#NotImplemented}.
27+
* <p>
28+
* It follows that if a signature {@code f(A, B)} appears, where
29+
* {@code A} is an accepted implementation of Python type {@code P}, and
30+
* {@code B} is an accepted implementation of Python type {@code Q},
31+
* {@code f(a, b)} must be present, or a signature with compatible
32+
* types, for every accepted implementation {@code a} of {@code P} and
33+
* {@code b} of {@code Q}. It is the responsibility of the script
34+
* generating this class to ensure this condition is satisfied.
35+
*/
36+
class PyFloatBinops {
37+
38+
private PyFloatBinops() {} // no instances
39+
40+
// $SPECIAL_BINOPS$ --------------------------------------------
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c)2025 Jython Developers.
2+
// Licensed to PSF under a contributor agreement.
3+
package uk.co.farowl.vsj4.runtime;
4+
5+
// $OBJECT_GENERATOR$ PyFloatGenerator
6+
7+
import uk.co.farowl.vsj4.runtime.PyUtil.NoConversion;
8+
import static uk.co.farowl.vsj4.runtime.PyFloat.nonzero;
9+
import static uk.co.farowl.vsj4.runtime.PyFloat.floordiv;
10+
import static uk.co.farowl.vsj4.runtime.PyFloat.mod;
11+
import static uk.co.farowl.vsj4.runtime.PyFloat.divmod;
12+
13+
/**
14+
* This class contains static methods implementing operations on the
15+
* Python {@code float} object, supplementary to those defined in
16+
* {@link PyFloat}.
17+
* <p>
18+
* These methods may cause creation of descriptors in the dictionary of
19+
* the type. Those with reserved names in the data model will also fill
20+
* slots in the {@code Operations} object for the type.
21+
* <p>
22+
* Implementations of binary operations defined here will have
23+
* {@code Object} as their second argument, and should return
24+
* {@link Py#NotImplemented} when the type in that position is not
25+
* supported.
26+
*/
27+
class PyFloatMethods {
28+
29+
PyFloatMethods() {} // no instances
30+
31+
// $SPECIAL_METHODS$ ---------------------------------------------
32+
33+
// plumbing ------------------------------------------------------
34+
35+
/**
36+
* Convert an object to a Java double. Conversion to a double may
37+
* raise an exception that is propagated to the caller. If the
38+
* method throws the special exception {@link NoConversion}, the
39+
* caller must catch it, and will normally return
40+
* {@link Py#NotImplemented}.
41+
*
42+
* @param v to convert
43+
* @return converted to {@code double}
44+
* @throws NoConversion v is not a {@code float} or {@code int}
45+
* @throws PyBaseException (OverflowError) v is an {@code int} too
46+
* large to be a {@code float}
47+
*/
48+
static double toDouble(Object v)
49+
throws NoConversion, PyBaseException {
50+
// Check against supported types, most likely first
51+
if (v instanceof Double)
52+
return ((Double)v).doubleValue();
53+
else if (v instanceof PyFloat)
54+
return ((PyFloat)v).value;
55+
else
56+
// BigInteger, PyLong, Boolean, etc.
57+
// or throw PyObjectUtil.NO_CONVERSION;
58+
return PyLong.convertToDouble(v);
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c)2025 Jython Developers.
2+
// Licensed to PSF under a contributor agreement.
3+
package uk.co.farowl.vsj4.runtime;
4+
5+
// $OBJECT_GENERATOR$ PyLongGenerator
6+
7+
import java.math.BigInteger;
8+
import static java.math.BigInteger.ZERO;
9+
import static java.math.BigInteger.ONE;
10+
import static uk.co.farowl.vsj4.runtime.PyLongMethods.toInt;
11+
import static uk.co.farowl.vsj4.runtime.PyLongMethods.divide;
12+
import static uk.co.farowl.vsj4.runtime.PyLongMethods.modulo;
13+
import static uk.co.farowl.vsj4.runtime.PyLongMethods.divmod;
14+
import static uk.co.farowl.vsj4.runtime.PyLongMethods.trueDivide;
15+
16+
17+
/**
18+
* This class contains static methods implementing operations on the
19+
* Python {@code long} object, supplementary to those defined in
20+
* {@link PyLong} and {@link PyLongMethods}. These exist in order to
21+
* support {@code CallSite} creation.
22+
* <p>
23+
* Implementations are not allowed to return {@link Py#NotImplemented}.
24+
* If a binary operation is not defined here, for the pair of Java
25+
* classes that type the arguments, the operation is not defined for the
26+
* pair of their Python types.
27+
* <p>
28+
* When matching, we allow a signature here to match if it matches a
29+
* type assignable in Java to the class of the argument in question.
30+
* Types providing this compatibility must not be too broad,
31+
* {@code Object} for example, as this would make it necessary to return
32+
* {@link Py#NotImplemented}.
33+
* <p>
34+
* It follows that if a signature {@code f(A, B)} appears, where
35+
* {@code A} is an accepted implementation of Python type {@code P}, and
36+
* {@code B} is an accepted implementation of Python type {@code Q},
37+
* {@code f(a, b)} must be present, or a signature with compatible
38+
* types, for every accepted implementation {@code a} of {@code P} and
39+
* {@code b} of {@code Q}. It is the responsibility of the script
40+
* generating this class to ensure this condition is satisfied.
41+
*/
42+
class PyLongBinops {
43+
44+
private PyLongBinops() {} // no instances
45+
46+
// $SPECIAL_BINOPS$ --------------------------------------------
47+
}

0 commit comments

Comments
 (0)