2001-03-15 23:11:01 -05:00
|
|
|
|
PEP: 237
|
|
|
|
|
Title: Unifying Long Integers and Integers
|
|
|
|
|
Version: $Revision$
|
2001-07-29 05:48:51 -04:00
|
|
|
|
Author: pep@zadka.site.co.il (Moshe Zadka), guido@python.org (Guido van Rossum)
|
2001-03-15 23:11:01 -05:00
|
|
|
|
Status: Draft
|
|
|
|
|
Type: Standards Track
|
|
|
|
|
Created: 11-Mar-2001
|
|
|
|
|
Python-Version: 2.2
|
2001-03-16 11:02:24 -05:00
|
|
|
|
Post-History: 16-Mar-2001
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
|
|
|
|
|
Python has both integers (machine word size integral) types, and
|
|
|
|
|
long integers (unbounded integral) types. When integers
|
|
|
|
|
operations overflow the machine registers, they raise an error.
|
|
|
|
|
This PEP proposes to do away with the distinction, and unify the
|
|
|
|
|
types from the perspective of both the Python interpreter and the
|
|
|
|
|
C API.
|
|
|
|
|
|
2001-07-29 05:48:51 -04:00
|
|
|
|
Note from second author: this PEP requires more thought about
|
|
|
|
|
implementation details. I've started to make a list of semantic
|
|
|
|
|
differences but I doubt it's complete.
|
|
|
|
|
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
|
|
|
|
|
Having the machine word size exposed to the language hinders
|
|
|
|
|
portability. For examples Python source files and .pyc's are not
|
|
|
|
|
portable because of this. Many programs find a need to deal with
|
|
|
|
|
larger numbers after the fact, and changing the algorithms later
|
|
|
|
|
is not only bothersome, but hinders performance in the normal
|
|
|
|
|
case.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Literals
|
|
|
|
|
|
|
|
|
|
A trailing 'L' at the end of an integer literal will stop having
|
|
|
|
|
any meaning, and will be eventually phased out. This will be done
|
|
|
|
|
using warnings when encountering such literals. The warning will
|
|
|
|
|
be off by default in Python 2.2, on for 12 months, which will
|
|
|
|
|
probably mean Python 2.3 and 2.4, and then will no longer be
|
|
|
|
|
supported.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Builtin Functions
|
|
|
|
|
|
|
|
|
|
The function long() will call the function int(), issuing a
|
|
|
|
|
warning. The warning will be off in 2.2, and on for two revisions
|
|
|
|
|
before removing the function. A FAQ will be added to explain that
|
|
|
|
|
a solutions for old modules are:
|
|
|
|
|
|
|
|
|
|
long=int
|
|
|
|
|
|
|
|
|
|
at the top of the module, or:
|
|
|
|
|
|
|
|
|
|
import __builtin__
|
|
|
|
|
__builtin__.long=int
|
|
|
|
|
|
|
|
|
|
In site.py.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C API
|
|
|
|
|
|
|
|
|
|
All PyLong_As* will call PyInt_As*. If PyInt_As* does not exist,
|
|
|
|
|
it will be added. Similarly for PyLong_From*. A similar path of
|
|
|
|
|
warnings as for the Python builtins will be followed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Overflows
|
|
|
|
|
|
|
|
|
|
When an arithmetic operation on two numbers whose internal
|
|
|
|
|
representation is as machine-level integers returns something
|
|
|
|
|
whose internal representation is a bignum, a warning which is
|
|
|
|
|
turned off by default will be issued. This is only a debugging
|
|
|
|
|
aid, and has no guaranteed semantics.
|
|
|
|
|
|
|
|
|
|
|
2001-07-29 05:48:51 -04:00
|
|
|
|
Semantic Differences
|
|
|
|
|
|
|
|
|
|
The following operations have (usually subtly) different semantics
|
|
|
|
|
for short and for long integers, and one will have to change
|
|
|
|
|
somehow. This is intended to be an exhaustive list; if you know
|
|
|
|
|
of anything else that might change, please write the author.
|
|
|
|
|
|
|
|
|
|
- Currently, all arithmetic operators on short ints except <<
|
|
|
|
|
raise OverflowError if the result cannot be represented as a
|
|
|
|
|
short int. This will change (of course).
|
|
|
|
|
|
|
|
|
|
- Currently x<<n can lose bits for short ints. No more.
|
|
|
|
|
|
|
|
|
|
- Currently, hex and oct literals for for short ints may specify
|
|
|
|
|
negative values; for example 0xffffffff == -1 on a 32-bint
|
|
|
|
|
machine. No more; this will equal 2**32-1.
|
|
|
|
|
|
|
|
|
|
- Currently, repr() of a long int returns a string ending in 'L'
|
|
|
|
|
while repr() of a short int doesn't. The 'L' will be dropped.
|
|
|
|
|
|
|
|
|
|
- Currently, an operation with long operands will never return a
|
|
|
|
|
short int. This may change.
|
|
|
|
|
|
|
|
|
|
- Currently, type(x) may reveal the difference between short and
|
|
|
|
|
long ints. This may or may not change (see Implementation
|
|
|
|
|
below).
|
|
|
|
|
|
|
|
|
|
|
2001-03-15 23:11:01 -05:00
|
|
|
|
Implementation
|
|
|
|
|
|
2001-07-29 05:48:51 -04:00
|
|
|
|
There are two alternative implementations to choose from.
|
|
|
|
|
|
|
|
|
|
1. The PyInt type's slot for a C long will be turned into a
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
long i;
|
2001-03-16 08:02:23 -05:00
|
|
|
|
struct {
|
2001-03-15 23:11:01 -05:00
|
|
|
|
unsigned long length;
|
|
|
|
|
digit digits[1];
|
2001-03-16 08:02:23 -05:00
|
|
|
|
} bignum;
|
2001-03-15 23:11:01 -05:00
|
|
|
|
};
|
|
|
|
|
|
2001-07-29 05:48:51 -04:00
|
|
|
|
Only the n-1 lower bits of the long have any meaning; the top bit
|
|
|
|
|
is always set. This distinguishes the union. All PyInt functions
|
|
|
|
|
will check this bit before deciding which types of operations to
|
|
|
|
|
use.
|
|
|
|
|
|
|
|
|
|
2. The existing short and long int types remain, but the short int
|
|
|
|
|
returns a long int instead of raising OverflowError.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Jython Issues
|
|
|
|
|
|
|
|
|
|
Jython will have a PyInt interface which is implemented by both
|
|
|
|
|
from PyFixNum and PyBigNum.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Open Issues
|
|
|
|
|
|
|
|
|
|
What to do about sys.maxint?
|
|
|
|
|
|
|
|
|
|
What to do about PyInt_AS_LONG failures?
|
|
|
|
|
|
|
|
|
|
What do do about %u, %o, %x formatting operators?
|
|
|
|
|
|
|
|
|
|
How to warn about << not cutting integers?
|
|
|
|
|
|
|
|
|
|
Should the overflow warning be on a portable maximum size?
|
|
|
|
|
|
2001-03-16 08:02:23 -05:00
|
|
|
|
Will unification of types and classes help with a more straightforward
|
|
|
|
|
implementations?
|
|
|
|
|
|
2001-03-19 14:36:46 -05:00
|
|
|
|
Define an C API that can be used to find out what the representation of an
|
|
|
|
|
int is.
|
|
|
|
|
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|