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-08-14 14:13:50 -04:00
|
|
|
|
Post-History: 16-Mar-2001, 14-Aug-2001
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Python currently distinguishes between two kinds of integers
|
|
|
|
|
(ints): regular or short ints, limited by the size of a C long
|
|
|
|
|
(typically 32 or 64 bits), and long ints, which are limited only
|
|
|
|
|
by available memory. When operations on short ints yield results
|
|
|
|
|
that don't fit in a C long, they raise an error. There are some
|
|
|
|
|
other distinctions too. This PEP proposes to do away with most of
|
|
|
|
|
the differences in semantics, unifying the two types from the
|
|
|
|
|
perspective of the Python user.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Many programs find a need to deal with larger numbers after the
|
|
|
|
|
fact, and changing the algorithms later is bothersome. It can
|
|
|
|
|
hinder performance in the normal case, when all arithmetic is
|
|
|
|
|
performed using long ints whether or not they are needed.
|
|
|
|
|
|
2001-03-15 23:11:01 -05:00
|
|
|
|
Having the machine word size exposed to the language hinders
|
|
|
|
|
portability. For examples Python source files and .pyc's are not
|
2001-08-14 14:12:48 -04:00
|
|
|
|
portable between 32-bit and 64-bit machines because of this.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-01 12:48:28 -04:00
|
|
|
|
There is also the general desire to hide unnecessary details from
|
|
|
|
|
the Python user when they are irrelevant for most applications.
|
2001-08-14 14:12:48 -04:00
|
|
|
|
An example is memory allocation, which explicit in C but automatic
|
|
|
|
|
in Python, giving us the convenience of unlimited sizes on
|
|
|
|
|
strings, lists, etc. It makes sense to extend this convenience to
|
|
|
|
|
numbers.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-01 12:48:28 -04:00
|
|
|
|
It will give new Python programmers (whether they are new to
|
|
|
|
|
programming in general or not) one less thing to learn before they
|
|
|
|
|
can start using the language.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
2001-08-01 12:48:28 -04:00
|
|
|
|
Implementation
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Initially, two alternative implementations were proposed (one by
|
|
|
|
|
each autor):
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
|
|
|
|
1. The PyInt type's slot for a C long will be turned into a
|
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
long i;
|
|
|
|
|
struct {
|
|
|
|
|
unsigned long length;
|
|
|
|
|
digit digits[1];
|
|
|
|
|
} bignum;
|
|
|
|
|
};
|
|
|
|
|
|
2001-08-14 14:12:48 -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.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
|
|
|
|
2. The existing short and long int types remain, but the short int
|
|
|
|
|
returns a long int instead of raising OverflowError when a
|
|
|
|
|
result cannot be represented as a short int. A new type,
|
|
|
|
|
integer, may be introduced that is an abstract base type of
|
|
|
|
|
which both the int and long implementation types are
|
|
|
|
|
subclassed. This is useful so that programs can check
|
|
|
|
|
integer-ness with a single test:
|
|
|
|
|
|
|
|
|
|
if isinstance(i, integer): ...
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
After some consideration, the second implementation plan was
|
|
|
|
|
selected, since it is far easier to implement, is backwards
|
|
|
|
|
compatible at the C API level, and in addition can be implemented
|
|
|
|
|
partially as a transitional measure.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Incompatibilities
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
The following operations have (usually subtly) different semantics
|
|
|
|
|
for short and for long integers, and one or the other will have to
|
|
|
|
|
be changed somehow. This is intended to be an exhaustive list.
|
|
|
|
|
If you know of any other operation that differ in outcome
|
|
|
|
|
depending on whether a short or a long int with the same value is
|
|
|
|
|
passed, please write the second author.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Currently, all arithmetic operators on short ints except <<
|
|
|
|
|
raise OverflowError if the result cannot be represented as a
|
|
|
|
|
short int. This will be changed to return a long int instead.
|
|
|
|
|
The following operators can currently raise OverflowError: x+y,
|
|
|
|
|
x-y, x*y, x**y, divmod(x, y), x/y, x%y, and -x. (The last four
|
|
|
|
|
can only overflow when the value -sys.maxint-1 is involved.)
|
|
|
|
|
|
|
|
|
|
- Currently, x<<n can lose bits for short ints. This will be
|
|
|
|
|
changed to return a long int containing all the shifted-out
|
|
|
|
|
bits, if returning a short int would lose bits.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Currently, hex and oct literals for for short ints may specify
|
|
|
|
|
negative values; for example 0xffffffff == -1 on a 32-bint
|
|
|
|
|
machine. This will be changed to equal 0xffffffffL (2**32-1).
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Currently, the '%u', '%x' and '%o' string formatting operators
|
|
|
|
|
and the hex() and oct() built-in functions behave differently
|
|
|
|
|
for negative numbers: negative short ints are formatted as
|
|
|
|
|
unsigned C long, while negative long ints are formatted with a
|
|
|
|
|
minus sign. This will be changed to use the long int semantics
|
|
|
|
|
in all cases (but without the trailing 'L' that currently
|
|
|
|
|
distinguishes the output of hex() and oct() for long ints).
|
|
|
|
|
Note that this means that '%u' becomes an alias for '%d'. It
|
|
|
|
|
will eventually be removed.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- 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.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Currently, an operation with long operands will never return a
|
|
|
|
|
short int. This *may* change, since it allows some
|
|
|
|
|
optimization.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Literals
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
A trailing 'L' at the end of an integer literal will stop having
|
|
|
|
|
any meaning, and will be eventually become illegal. The compiler
|
|
|
|
|
will choose the appropriate type solely based on the value.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Built-in Functions
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
The function int() will return a short or a long int depending on
|
|
|
|
|
the argument value. The function long() will call the function
|
|
|
|
|
int(). The built-in name 'long' will remain in the language to
|
|
|
|
|
represent the long implementation type, but using the int()
|
|
|
|
|
function is still recommended, since it will automatically return
|
|
|
|
|
a long when needed.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
C API
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
The C API remains unchanged; C code will still need to be aware of
|
|
|
|
|
the difference between short and long ints.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
The PyArg_Parse*() APIs already accept long ints, as long as they
|
|
|
|
|
are within the range representable by C ints or longs, so that
|
|
|
|
|
functions taking C int or long argument won't have to worry about
|
|
|
|
|
dealing with Python longs.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Transition
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
There are two major phases to the transition:
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
A. Short int operations that currently raise OverflowError return
|
|
|
|
|
a long int value instead. This is the only change in this
|
|
|
|
|
phase. Literals will still distinguish between short and long
|
|
|
|
|
ints. The other semantic differences listed above (including
|
|
|
|
|
the behavior of <<) will remain. Because this phase only
|
|
|
|
|
changes situations that currently raise OverflowError, it is
|
|
|
|
|
assumed that this won't break existing code. (Code that
|
|
|
|
|
depends on this exception would have to be too convoluted to be
|
|
|
|
|
concerned about it.) For those concerned about extreme
|
|
|
|
|
backwards compatibility, a command line option will allow a
|
|
|
|
|
warning to be issued at this point, but this is off by default.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B. The remaining semantic differences are addressed. In most
|
|
|
|
|
cases the long int semantics will prevail; however, the
|
|
|
|
|
trailing 'L' from long int representations will be dropped.
|
|
|
|
|
Eventually, support for integer literals with a trailing 'L'
|
|
|
|
|
will be removed. Since this will introduce backwards
|
|
|
|
|
incompatibilities which will break some old code, this phase
|
|
|
|
|
may require a future statement and/or warnings, and a
|
|
|
|
|
prolongued transition phase.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Phase A will be implemented in Python 2.2.
|
2001-07-29 05:48:51 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Phase B will be implemented starting with Python 2.3. Envisioned
|
|
|
|
|
stages of phase B:
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B1. The remaining semantic differenes are addressed. Operations
|
|
|
|
|
that give different results than before will issue a warning
|
|
|
|
|
that is on by default. A warning for the use of long literals
|
|
|
|
|
(with a trailing 'L') may be enabled through a command line
|
|
|
|
|
option, but it is off by default.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B2. The warning for long literals is turned on by default.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B3. The warnings about operations that give different results than
|
|
|
|
|
before are turned off by default.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B4. Long literals are no longer legal. All warnings related to
|
|
|
|
|
this issue are gone.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
We propose the following timeline:
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B1. Python 2.3.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B2. Python 2.4.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B3. The rest of the Python 2.x line.
|
2001-08-01 12:48:28 -04:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
B4. Python 3.0 (at least two years in the future).
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
Open Issues
|
|
|
|
|
|
|
|
|
|
We expect that these issues will be resolved over time, as more
|
|
|
|
|
feedback is received or we gather more experience with the initial
|
|
|
|
|
implementation.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- What to do about sys.maxint? Leave it in, since it is still
|
|
|
|
|
relevant whenever the distinction between short and long ints is
|
|
|
|
|
still relevant (e.g. when inspecting the type of a value).
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Should be remove '%u' completely? Remove it.
|
2001-03-15 23:11:01 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Should we warn about << not truncating integers? Yes.
|
2001-03-16 08:02:23 -05:00
|
|
|
|
|
2001-08-14 14:12:48 -04:00
|
|
|
|
- Should the overflow warning be on a portable maximum size? No.
|
2001-03-19 14:36:46 -05:00
|
|
|
|
|
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:
|