Top Description Inners Fields Constructors Methods
org.python.core

public Class PyLong

extends AbstractPyObject
implements Key
Class Inheritance
All Implemented Interfaces
org.python.core.PyDict.Key
Known Direct Subclasses
org.python.core.PyLong.Derived
Static Imports
java.math.BigInteger.ONE, .BigInteger.ZERO

A Python int object may be represented by a java.lang.Integer or a java.math.BigInteger. An instance of a Python sub-class of int, must be represented by an instance of a Java sub-class of) this class.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
PyLong.Derived

Instances in Python of sub-classes of 'int', are represented in Java by instances of this class.

private static class
PyLong.Formatter

An IntegerFormatter, constructed from a Spec, with validations customised for int.__format__.

Field Summary

Modifier and TypeField and Description
pack-priv static final BigInteger
MAX_INT

The maximum Java int as a BigInteger.

pack-priv static final BigInteger
MAX_LONG

The maximum Java long as a BigInteger.

pack-priv static final BigInteger
MIN_INT

The minimum Java int as a BigInteger.

pack-priv static final BigInteger
MIN_LONG

The minimum Java long as a BigInteger.

private static final String
private static final String
public static final PyType
TYPE

The type int.

pack-priv final BigInteger
value

The value of this Python int (in sub-class instances).

Constructor Summary

AccessConstructor and Description
pack-priv
PyLong(PyType
actual Python sub-class being created
subType
,
BigInteger
of the int
v
)

Constructor for Python sub-class specifying type.

Method Summary

Modifier and TypeMethod and Description
pack-priv static final Object
__format__(Object self, Object formatSpec)

pack-priv static PyTuple
pack-priv static Object
__new__(PyType subType, Object[] args, String[] kwnames)

private static Object

Returns:

an int or sub-class with the right value
__new__impl
(PyType
actual sub-type of int to produce
subType
,
Object
int-like or str-like value or null.
x
,
Object
number base (x must be str-like)
obase
)

Implementation of __new__ with classic arguments unpacked.

private static Object
public Object
pack-priv static Object
pack-priv static BigInteger

Returns:

BigInteger value
asBigInteger
(Object
claimed int
v
)

Present the value as a Java BigInteger when the argument is expected to be a Python int or a sub-class of it.

pack-priv static double

Returns:

nearest double
asDouble
(Object
to convert
v
)

Value as a Java double using the round-half-to-even rule.

pack-priv static int

Returns:

int value
asInt
(Object
claimed int
v
)

Present the value as a Java int when the argument is expected to be a Python int or a sub-class of it.

public long
pack-priv static int

Returns:

int value
asSize
(Object
claimed int
v
)

Present the value as a Java int when the argument is expected to be a Python int or a sub-class of it.

pack-priv static int
pack-priv static Object
pack-priv static BigInteger

Returns:

converted to BigInteger
convertToBigInteger
(Object
claimed int
v
)

Convert a Python int to a Java BigInteger (or throw NoConversion).

pack-priv static double

Returns:

converted to double
convertToDouble
(Object
to convert
v
)

Convert an int to a Java double (or throw NoConversion), using the round-half-to-even rule.

pack-priv static double

Returns:

converted to double
convertToDouble
(BigInteger
to convert
v
)

Convert a BigInteger to a Java double , using the round-half-to-even rule.

pack-priv static int

Returns:

converted to int
convertToInt
(Object
to convert
v
)

Convert a Python int to a Java int (or throw NoConversion).

pack-priv static int

Returns:

converted to int
convertToInt
(BigInteger
to convert
v
)

Convert a BigInteger to a Java int.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Implements org.python.core.PyDict.Key.equals.

Python objects that implement the interface Key, define this method so that calls from Java libraries to Object.equals(Object) are answered with Python semantics.
pack-priv static Object

Returns:

the same value as exactly int
from
(Object
to represent
value
)

Return a Python int from a Python int or subclass.

pack-priv static BigInteger

Returns:

BigInteger equivalent.
fromDouble
(double
to convert
value
)

Create a Python int from a Java double.

pack-priv static Object

Returns:

integer value of argument
fromIndexOrIntOf
(Object
to convert to int
integral
)

Convert the given object to a int using the __index__ or __int__ special methods, if available (the latter is deprecated).

pack-priv static Object

Returns:

integer value of argument
fromIntOf
(Object
to convert to int
integral
)

Convert the given object to a Python int using the op_int slot, if available.

pack-priv static BigInteger

Returns:

converted value
fromUnicode
(Object
string to convert
u
,
int
in which to interpret it
base
)

Convert a sequence of Unicode digits in the string u to a Python integer value.

public static Object
public static Object
public long
getLong(long min, long max)

public long
getLong(long min, long max, String overflowMsg)

public static Object
public static Object
public BigInteger

Returns:

value
getValue
()

The value of this Python int (in sub-class instances).

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements org.python.core.PyDict.Key.hashCode.

Python objects that implement the interface Key, define this method so that calls from Java libraries to Object.hashCode() receive the hash defined by Python.
private static InterpreterError

Returns:

exception to throw
impossible
(Object
actual argument
o
)

We received an argument that should be impossible in a correct interpreter.

private static Object
longSubtypeNew(PyType subType, Object x, Object obase)

Wimpy, slow approach to __new__ calls for sub-types of int, that will temporarily create a regular int from the arguments.

pack-priv static int
pack-priv static OverflowError

Returns:

an OverflowError with that message
tooLarge
(String
description of type to convert from
from
,
String
description of type to convert to
to
)

Create an OverflowError with a message along the lines "X too large to convert to Y", where X is from and Y is to.

public String
toString()

Overrides org.python.core.AbstractPyObject.toString.

Returns a string representation of the object.
Inherited from org.python.core.AbstractPyObject:
getType

Field Detail

MAX_INTback to summary
pack-priv static final BigInteger MAX_INT

The maximum Java int as a BigInteger.

MAX_LONGback to summary
pack-priv static final BigInteger MAX_LONG

The maximum Java long as a BigInteger.

MIN_INTback to summary
pack-priv static final BigInteger MIN_INT

The minimum Java int as a BigInteger.

MIN_LONGback to summary
pack-priv static final BigInteger MIN_LONG

The minimum Java long as a BigInteger.

NON_STR_EXPLICIT_BASEback to summary
private static final String NON_STR_EXPLICIT_BASE
TOO_LARGEback to summary
private static final String TOO_LARGE
TYPEback to summary
public static final PyType TYPE

The type int.

valueback to summary
pack-priv final BigInteger value

The value of this Python int (in sub-class instances).

Constructor Detail

PyLongback to summary
pack-priv PyLong(PyType subType, BigInteger v)

Constructor for Python sub-class specifying type.

Parameters
subType:PyType

actual Python sub-class being created

v:BigInteger

of the int

Method Detail

__format__back to summary
pack-priv static final Object __format__(Object self, Object formatSpec)
__getnewargs__back to summary
pack-priv static PyTuple __getnewargs__(Object self)
__new__back to summary
pack-priv static Object __new__(PyType subType, Object[] args, String[] kwnames) throws Throwable
Annotations
@SuppressWarnings:fallthrough
__new__implback to summary
private static Object __new__impl(PyType subType, Object x, Object obase) throws Throwable

Implementation of __new__ with classic arguments unpacked.

Parameters
subType:PyType

actual sub-type of int to produce

x:Object

int-like or str-like value or null.

obase:Object

number base (x must be str-like)

Returns:Object

an int or sub-class with the right value

Exceptions
Throwable:
on argument type or other errors

__repr__back to summary
private static Object __repr__(Object self)
Annotations
@SuppressWarnings:unused
__tojava__back to summary
public Object __tojava__(Class<?> c)
__trunc__back to summary
pack-priv static Object __trunc__(Object self)
asBigIntegerback to summary
pack-priv static BigInteger asBigInteger(Object v) throws TypeError

Present the value as a Java BigInteger when the argument is expected to be a Python int or a sub-class of it.

Parameters
v:Object

claimed int

Returns:BigInteger

BigInteger value

Exceptions
TypeError:
if v is not a Python int

asDoubleback to summary
pack-priv static double asDouble(Object v)

Value as a Java double using the round-half-to-even rule.

Parameters
v:Object

to convert

Returns:double

nearest double

Exceptions
OverflowError:
if out of double range

asIntback to summary
pack-priv static int asInt(Object v) throws TypeError, OverflowError

Present the value as a Java int when the argument is expected to be a Python int or a sub-class of it.

Parameters
v:Object

claimed int

Returns:int

int value

Exceptions
TypeError:
if v is not a Python int
OverflowError:
if v is out of Java range

asLongback to summary
public long asLong()
asSizeback to summary
pack-priv static int asSize(Object v) throws TypeError, OverflowError

Present the value as a Java int when the argument is expected to be a Python int or a sub-class of it.

Parameters
v:Object

claimed int

Returns:int

int value

Exceptions
TypeError:
if v is not a Python int
OverflowError:
if v is out of Java range

bit_lengthback to summary
pack-priv static int bit_length(Object self)
conjugateback to summary
pack-priv static Object conjugate(Object self)
convertToBigIntegerback to summary
pack-priv static BigInteger convertToBigInteger(Object v) throws NoConversion

Convert a Python int to a Java BigInteger (or throw NoConversion). Conversion may raise an exception that is propagated to the caller. If the Java type of the int is declared, generally there is a better option than this method. We only use it for Object arguments.

If the method throws the special exception NoConversion, the caller must deal with it by throwing an appropriate Python exception or taking an alternative course of action.

Parameters
v:Object

claimed int

Returns:BigInteger

converted to BigInteger

Exceptions
NoConversion:
if v is not a Python int

convertToDoubleback to summary
pack-priv static double convertToDouble(Object v) throws NoConversion, OverflowError

Convert an int to a Java double (or throw NoConversion), using the round-half-to-even rule. Conversion to a double may overflow, raising an exception that is propagated to the caller.

If the method throws the special exception NoConversion, the caller must deal with it by throwing an appropriate Python exception or taking an alternative course of action. Binary operations will normally return Py#NotImplemented in response.

Parameters
v:Object

to convert

Returns:double

converted to double

Exceptions
NoConversion:
v is not an int
OverflowError:
v is too large to be a float

convertToDoubleback to summary
pack-priv static double convertToDouble(BigInteger v) throws OverflowError

Convert a BigInteger to a Java double , using the round-half-to-even rule. Conversion to a double may overflow, raising an exception that is propagated to the caller.

Parameters
v:BigInteger

to convert

Returns:double

converted to double

Exceptions
OverflowError:
if too large to be a float

convertToIntback to summary
pack-priv static int convertToInt(Object v) throws NoConversion, OverflowError

Convert a Python int to a Java int (or throw NoConversion). Conversion to an int may overflow, raising an exception that is propagated to the caller.

If the method throws the special exception NoConversion, the caller must deal with it by throwing an appropriate Python exception or taking an alternative course of action.

Parameters
v:Object

to convert

Returns:int

converted to int

Exceptions
NoConversion:
v is not an int
OverflowError:
v is too large to be a Java int

convertToIntback to summary
pack-priv static int convertToInt(BigInteger v) throws OverflowError

Convert a BigInteger to a Java int. Conversion to an int may overflow, raising an exception that is propagated to the caller.

Parameters
v:BigInteger

to convert

Returns:int

converted to int

Exceptions
OverflowError:
if too large to be a Java int

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements org.python.core.PyDict.Key.equals.

Doc from org.python.core.PyDict.Key.equals.

Python objects that implement the interface Key, define this method so that calls from Java libraries to Object.equals(Object) are answered with Python semantics. A sufficient implementation is:

@Override
public int equals(Object obj) throws PyException {
    return PyDict.pythonEquals(this, obj);
}
Objects that provide their own specialised implementation of equals, receiving a Key a object as the other argument, must dereference it with get() and work on those contents. An idiom like this may be used:
@Override
public boolean equals(Object other) {
    if (other instanceof PyDict.Key)
        other = ((PyDict.Key) other).get();
    // ... rest of implementation
}
 
Parameters
obj:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override

fromback to summary
pack-priv static Object from(Object value) throws TypeError

Return a Python int from a Python int or subclass. If the value has exactly Python type int return it, otherwise construct a new instance of exactly int type.

Parameters
value:Object

to represent

Returns:Object

the same value as exactly int

Exceptions
TypeError:
if not a Python int or sub-class

fromDoubleback to summary
pack-priv static BigInteger fromDouble(double value)

Create a Python int from a Java double.

Parameters
value:double

to convert

Returns:BigInteger

BigInteger equivalent.

Exceptions
OverflowError:
when value is a floating infinity
ValueError:
when value is a floating NaN

fromIndexOrIntOfback to summary
pack-priv static Object fromIndexOrIntOf(Object integral) throws TypeError, Throwable

Convert the given object to a int using the __index__ or __int__ special methods, if available (the latter is deprecated).

The return is not always exactly an int. integral.__index__ or integral.__int__, which this method wraps, may return any type: Python sub-classes of int are tolerated, but with a deprecation warning. Returns not even a sub-class type int raise TypeError. This method should be replaced with PyNumber#index(Object) after the end of the deprecation period.

Parameters
integral:Object

to convert to int

Returns:Object

integer value of argument

Exceptions
TypeError:
if integral seems not to be
Throwable:
from the supporting implementation

fromIntOfback to summary
pack-priv static Object fromIntOf(Object integral) throws TypeError, Throwable

Convert the given object to a Python int using the op_int slot, if available. Raise TypeError if either the op_int slot is not available or the result of the call to op_int returns something not of type int.

The return is not always exactly an int. integral.__int__, which this method wraps, may return any type: Python sub-classes of int are tolerated, but with a deprecation warning. Returns not even a sub-class type int raise TypeError.

Parameters
integral:Object

to convert to int

Returns:Object

integer value of argument

Exceptions
TypeError:
if integral seems not to be
Throwable:
from the supporting implementation

fromUnicodeback to summary
pack-priv static BigInteger fromUnicode(Object u, int base) throws ValueError, TypeError

Convert a sequence of Unicode digits in the string u to a Python integer value.

Parameters
u:Object

string to convert

base:int

in which to interpret it

Returns:BigInteger

converted value

Exceptions
ValueError:
if u is an invalid literal
TypeError:
if u is not a Python str

getDenominatorback to summary
public static Object getDenominator(Object self)
getImagback to summary
public static Object getImag(Object self)
getLongback to summary
public long getLong(long min, long max) throws OverflowError
getLongback to summary
public long getLong(long min, long max, String overflowMsg) throws OverflowError
getNumeratorback to summary
public static Object getNumerator(Object self)
getRealback to summary
public static Object getReal(Object self)
getValueback to summary
public BigInteger getValue()

The value of this Python int (in sub-class instances).

Returns:BigInteger

value

hashCodeback to summary
public int hashCode() throws PyException

Overrides java.lang.Object.hashCode.

Implements org.python.core.PyDict.Key.hashCode.

Doc from org.python.core.PyDict.Key.hashCode.

Python objects that implement the interface Key, define this method so that calls from Java libraries to Object.hashCode() receive the hash defined by Python. A sufficient implementation is:

@Override
public int hashCode() throws PyException {
    return PyDict.pythonHash(this);
}
Where it is known __hash__ cannot have been redefined, object implementations may have a shorter option.
Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
Exceptions
PyException:
from __hash__ implementations

impossibleback to summary
private static InterpreterError impossible(Object o)

We received an argument that should be impossible in a correct interpreter. We use this when conversion of an Object self argument may theoretically fail, but we know that we should only reach that point by paths that guarantee self` to be some kind on int.

Parameters
o:Object

actual argument

Returns:InterpreterError

exception to throw

longSubtypeNewback to summary
private static Object longSubtypeNew(PyType subType, Object x, Object obase) throws Throwable

Wimpy, slow approach to __new__ calls for sub-types of int, that will temporarily create a regular int from the arguments.

Exceptions
Throwable:
on argument type or other errors
signumback to summary
pack-priv static int signum(Object v) throws TypeError
tooLargeback to summary
pack-priv static OverflowError tooLarge(String from, String to)

Create an OverflowError with a message along the lines "X too large to convert to Y", where X is from and Y is to.

Parameters
from:String

description of type to convert from

to:String

description of type to convert to

Returns:OverflowError

an OverflowError with that message

toStringback to summary
public String toString()

Overrides org.python.core.AbstractPyObject.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override

org.python.core back to summary

pack-priv Class PyLong.Derived

extends PyLong
implements DictPyObject
Class Inheritance
All Implemented Interfaces
org.python.core.DictPyObject, org.python.core.CraftedPyObject

Instances in Python of sub-classes of 'int', are represented in Java by instances of this class.

Field Summary

Inherited from org.python.core.PyLong:
MAX_INTMAX_LONGMIN_INTMIN_LONGTYPEvalue

Constructor Summary

AccessConstructor and Description
protected
Derived(PyType subType, BigInteger value)

Method Summary

Modifier and TypeMethod and Description
public Map<Object, Object>
getDict()

Implements org.python.core.DictPyObject.getDict.

The dictionary of the instance, (not necessarily a Python dict or writable.
Inherited from org.python.core.PyLong:
__format____getnewargs____new____tojava____trunc__asBigIntegerasDoubleasIntasLongasSizebit_lengthconjugateconvertToBigIntegerconvertToDoubleconvertToDoubleconvertToIntconvertToIntequalsfromfromDoublefromIndexOrIntOffromIntOffromUnicodegetDenominatorgetImaggetLonggetLonggetNumeratorgetRealgetValuehashCodesignumtooLargetoString

Constructor Detail

Derivedback to summary
protected Derived(PyType subType, BigInteger value)

Method Detail

getDictback to summary
public Map<Object, Object> getDict()

Implements org.python.core.DictPyObject.getDict.

Doc from org.python.core.DictPyObject.getDict.

The dictionary of the instance, (not necessarily a Python dict or writable. If the returned Map is not writable, it should throw a Java UnsupportedOperationException on attempts to modify it.

Returns:Map<Object, Object>

a mapping to treat like a dictionary (not null).

Annotations
@Override

org.python.core back to summary

private Class PyLong.Formatter

extends IntegerFormatter
Class Inheritance

An IntegerFormatter, constructed from a Spec, with validations customised for int.__format__.

Constructor Summary

AccessConstructor and Description
pack-priv
Formatter(InternalFormat.Spec
a parsed PEP-3101 format specification.
spec
)

Prepare an IntegerFormatter in support of int.

Method Summary

Modifier and TypeMethod and Description
public IntegerFormatter
format(Object
to format
o
)

Implements abstract org.python.core.stringlib.InternalFormat.AbstractFormatter.format.

Format an object into the buffer according to the specification with which this instance has been initialised.
private static InternalFormat.Spec

Returns:

validated spec with defaults filled
validated
(InternalFormat.Spec
to validate
spec
)

Validations and defaults specific to int.__format__.

Inherited from org.python.core.stringlib.IntegerFormatter:
appendappendappendformat

Constructor Detail

Formatterback to summary
pack-priv Formatter(InternalFormat.Spec spec) throws FormatError

Prepare an IntegerFormatter in support of int.__format__.

Parameters
spec:InternalFormat.Spec

a parsed PEP-3101 format specification.

Exceptions
FormatError:
if an unsupported format character is encountered
FormatOverflow:
if a value is out of range (including the precision)

Method Detail

formatback to summary
public IntegerFormatter format(Object o) throws NoConversion, FormatError

Implements abstract org.python.core.stringlib.InternalFormat.AbstractFormatter.format.

Doc from org.python.core.stringlib.InternalFormat.AbstractFormatter.format.

Format an object into the buffer according to the specification with which this instance has been initialised. The specific behaviour, and the acceptable types, are for a sub-class to define. Also, it is usually desirable for the return type in an implementing class to be a more specific.

Parameters
o:Object

to format

Returns:IntegerFormatter

this formatter

Annotations
@Override
Exceptions
NoConversion:
if o is not convertible
FormatError:
from the process of formatting

validatedback to summary
private static InternalFormat.Spec validated(InternalFormat.Spec spec) throws FormatError

Validations and defaults specific to int.__format__. (Note that int.__mod__ has slightly different rules.)

Parameters
spec:InternalFormat.Spec

to validate

Returns:InternalFormat.Spec

validated spec with defaults filled

Exceptions
FormatError:
on failure to validate