Top Description Fields Constructors Methods
org.python.core

public Class PyNumber

extends Abstract
Class Inheritance
Imports
java.lang.invoke.MethodHandle, java.util.function.Function, org.python.core.Slot.EmptyException

Abstract API for operations on numeric types, corresponding to CPython methods defined in abstract.h and with names like: PyNumber_*.

Field Summary

Modifier and TypeField and Description
private static final MethodHandle
private static final String
private static final String
private static final String
Inherited from org.python.core.Abstract:
NOT_ITERABLENOT_MAPPING

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Object

Returns:

-v
absolute
(Object
operand
v
)

abs(v): absolute value with Python semantics.

public static Object

Returns:

v + w
add
(Object
left operand
v
,
Object
right operand
w
)

v + w with Python semantics.

pack-priv static final Object

Returns:

v & w
and
(Object
left operand
v
,
Object
right operand
w
)

v & w with Python semantics.

pack-priv static Object

Returns:

int(o)
asLong
(Object
operand
o
)

Returns the o converted to an integer object.

pack-priv static int

Returns:

int value of o
asSize
(Object
the object to convert to an int
o
,
Function<String, PyException>
null or function of String returning the exception to use for overflow.
exc
)

Returns o converted to a Java int if o can be interpreted as an integer.

private static Object

Returns:

result of operation
binary_op
(Object
left operand
v
,
Object
right operand
w
,
Slot
operation to apply
binop
)

Helper for implementing a binary operation that has one, slot-based interpretation.

private static Object

Returns:

result or Py.NotImplemented
binary_op1
(Object
left operand
v
,
Object
right operand
w
,
Slot
operation to apply
binop
)

Helper for implementing binary operation.

pack-priv static Object

Returns:

o coerced to a Python int
index
(Object
operand
o
)

Return a Python int (or subclass) from the object o.

pack-priv static boolean

Returns:

whether obj has non-empty Slot#op_index
indexCheck
(Object
to test
obj
)

True iff the object has a slot for conversion to the index type.

public static Object

Returns:

~v
invert
(Object
operand
v
)

~v: unary negative with Python semantics.

public static Object

Returns:

v * w
multiply
(Object
left operand
v
,
Object
right operand
w
)

v * w with Python semantics.

public static Object

Returns:

-v
negative
(Object
operand
v
)

-v: unary negative with Python semantics.

pack-priv static PyException

Returns:

exception to throw
operandError
(Slot
operation to report
op
,
Object
actual operand (only PyType.of(v) is used)
v
)

Create a TypeError for a named unary operation, along the lines "bad operand type for OP: 'T'".

pack-priv static PyException

Returns:

exception to throw
operandError
(Slot
operation to report
op
,
Object
left operand (only PyType.of(v) is used)
v
,
Object
right operand (only PyType.of(w) is used)
w
)

Throw a TypeError for the named binary operation, along the lines "unsupported operand type(s) for OP: 'V' and 'W'".

pack-priv static final Object

Returns:

v | w
or
(Object
left operand
v
,
Object
right operand
w
)

v | w with Python semantics.

pack-priv static int

Returns:

normalised value as a Java int
sliceIndex
(Object
to convert
v
,
int
to return when v==Py.None
defaultValue
)

Extract a slice index from a Python int or an object defining __index__, and return it as a Java int.

public static Object

Returns:

v - w
subtract
(Object
left operand
v
,
Object
right operand
w
)

v - w with Python semantics.

pack-priv static final Object

Returns:

v ^ w
xor
(Object
left operand
v
,
Object
right operand
w
)

v ^ w with Python semantics.

Inherited from org.python.core.Abstract:
argumentTypeErrorargumentTypeErrorattributeNameTypeErrorattrMustBeattrMustBeStringbadInternalCallcantSetAttributeErrordelAttrdelAttrgetAttrgetAttrgetIteratorgetIteratorhashimpossibleArgumentErrorindexOutOfRangeindexTypeErrorindexTypeErrorisTrueiterableCheckiteratorChecklookupAttrlookupAttrmandatoryAttributeErrormandatoryAttributeOnTypenextnoAttributeErrornoAttributeOnTypereadonlyAttributeErrorreadonlyAttributeOnTyperecursiveIsSubclassreprrequiredTypeErrorreturnDeprecationreturnTypeErrorrichComparerichCompareBoolrichCompareBoolsetAttrsetAttrstrtojavatypeError

Field Detail

BINARY_EMPTYback to summary
private static final MethodHandle BINARY_EMPTY
CANNOT_FITback to summary
private static final String CANNOT_FIT
CANNOT_INTERPRET_AS_INTback to summary
private static final String CANNOT_INTERPRET_AS_INT
UNSUPPORTED_TYPESback to summary
private static final String UNSUPPORTED_TYPES

Constructor Detail

PyNumberback to summary
private PyNumber()

Method Detail

absoluteback to summary
public static Object absolute(Object v) throws Throwable

abs(v): absolute value with Python semantics.

Parameters
v:Object

operand

Returns:Object

-v

Exceptions
Throwable:
from invoked implementations
addback to summary
public static Object add(Object v, Object w) throws Throwable

v + w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v + w

Exceptions
Throwable:
from invoked implementations
andback to summary
pack-priv static final Object and(Object v, Object w) throws Throwable

v & w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v & w

Exceptions
Throwable:
from invoked implementations
asLongback to summary
pack-priv static Object asLong(Object o) throws TypeError, Throwable

Returns the o converted to an integer object. This is the equivalent of the Python expression int(o). It will refer to the __int__, __index_ and __trunc__ special methods of o, in that order, an then (if o is string or bytes-like) attempt a conversion from text assuming decimal base.

Parameters
o:Object

operand

Returns:Object

int(o)

Exceptions
TypeError:
if o cannot be converted to a Python int
Throwable:
on other errors
asSizeback to summary
pack-priv static int asSize(Object o, Function<String, PyException> exc) throws TypeError, Throwable

Returns o converted to a Java int if o can be interpreted as an integer. If the call fails, an exception is raised, which may be a TypeError or anything thrown by o's implementation of __index__. In the special case of OverflowError, a replacement may be made where the message is formulated by this method and the type of exception by the caller. (Arcane, but it's what CPython does.) A recommended idiom for this is

     int k = Number.asSize(key, IndexError::new);
Parameters
o:Object

the object to convert to an int

exc:Function<String, PyException>

null or function of String returning the exception to use for overflow.

Returns:int

int value of o

Exceptions
TypeError:
if o cannot be converted to a Python int
Throwable:
on other errors
binary_opback to summary
private static Object binary_op(Object v, Object w, Slot binop) throws TypeError, Throwable

Helper for implementing a binary operation that has one, slot-based interpretation.

Parameters
v:Object

left operand

w:Object

right operand

binop:Slot

operation to apply

Returns:Object

result of operation

Exceptions
TypeError:
if neither operand implements the operation
Throwable:
from the implementation of the operation
binary_op1back to summary
private static Object binary_op1(Object v, Object w, Slot binop) throws EmptyException, Throwable

Helper for implementing binary operation. If neither the left type nor the right type implements the operation, it will either return Py#NotImplemented or throw EmptyException. Both mean the same thing.

Parameters
v:Object

left operand

w:Object

right operand

binop:Slot

operation to apply

Returns:Object

result or Py.NotImplemented

Exceptions
EmptyException:
when an empty slot is invoked
Throwable:
from the implementation of the operation
indexback to summary
pack-priv static Object index(Object o) throws TypeError, Throwable

Return a Python int (or subclass) from the object o. Raise TypeError if the result is not a Python int subclass, or if the object o cannot be interpreted as an index (it does not fill Slot#op_index). This method makes no guarantee about the range of the result.

Parameters
o:Object

operand

Returns:Object

o coerced to a Python int

Exceptions
TypeError:
if o cannot be interpreted as an int
Throwable:
otherwise from invoked implementations
indexCheckback to summary
pack-priv static boolean indexCheck(Object obj)

True iff the object has a slot for conversion to the index type.

Parameters
obj:Object

to test

Returns:boolean

whether obj has non-empty Slot#op_index

invertback to summary
public static Object invert(Object v) throws Throwable

~v: unary negative with Python semantics.

Parameters
v:Object

operand

Returns:Object

~v

Exceptions
Throwable:
from invoked implementations
multiplyback to summary
public static Object multiply(Object v, Object w) throws Throwable

v * w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v * w

Exceptions
Throwable:
from invoked implementations
negativeback to summary
public static Object negative(Object v) throws Throwable

-v: unary negative with Python semantics.

Parameters
v:Object

operand

Returns:Object

-v

Exceptions
Throwable:
from invoked implementations
operandErrorback to summary
pack-priv static PyException operandError(Slot op, Object v)

Create a TypeError for a named unary operation, along the lines "bad operand type for OP: 'T'".

Parameters
op:Slot

operation to report

v:Object

actual operand (only PyType.of(v) is used)

Returns:PyException

exception to throw

operandErrorback to summary
pack-priv static PyException operandError(Slot op, Object v, Object w)

Throw a TypeError for the named binary operation, along the lines "unsupported operand type(s) for OP: 'V' and 'W'".

Parameters
op:Slot

operation to report

v:Object

left operand (only PyType.of(v) is used)

w:Object

right operand (only PyType.of(w) is used)

Returns:PyException

exception to throw

orback to summary
pack-priv static final Object or(Object v, Object w) throws Throwable

v | w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v | w

Exceptions
Throwable:
from invoked implementations
sliceIndexback to summary
pack-priv static int sliceIndex(Object v, int defaultValue) throws TypeError, Throwable

Extract a slice index from a Python int or an object defining __index__, and return it as a Java int. So that the call need not be guarded by v!=Py.None, which is a common occurrence in the contexts where it is used, we special-case None to return a supplied default value. We silently reduce values larger than Integer#MAX_VALUE to Integer.MAX_VALUE, and silently boost values less than Integer#MIN_VALUE to Integer.MIN_VALUE.

Parameters
v:Object

to convert

defaultValue:int

to return when v==Py.None

Returns:int

normalised value as a Java int

Exceptions
TypeError:
if v!=None has no __index__
Throwable:
from the implementation of __index__
subtractback to summary
public static Object subtract(Object v, Object w) throws Throwable

v - w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v - w

Exceptions
Throwable:
from invoked implementations
xorback to summary
pack-priv static final Object xor(Object v, Object w) throws Throwable

v ^ w with Python semantics.

Parameters
v:Object

left operand

w:Object

right operand

Returns:Object

v ^ w

Exceptions
Throwable:
from invoked implementations