1991-02-19 20:39:46 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Integer object implementation */
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
1999-10-13 03:54:53 +08:00
|
|
|
#include <ctype.h>
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1993-12-24 18:22:45 +08:00
|
|
|
long
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_GetMax(void)
|
1993-12-24 18:22:45 +08:00
|
|
|
{
|
|
|
|
return LONG_MAX; /* To initialize sys.maxint */
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Standard Booleans */
|
1990-12-20 23:06:42 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyIntObject _Py_ZeroStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyInt_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
0
|
|
|
|
};
|
1990-12-20 23:06:42 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyIntObject _Py_TrueStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyInt_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
1
|
|
|
|
};
|
|
|
|
|
2001-08-23 10:59:04 +08:00
|
|
|
/* Return 1 if exception raised, 0 if caller should retry using longs */
|
|
|
|
static int
|
2000-07-09 23:16:51 +08:00
|
|
|
err_ovf(char *msg)
|
1990-10-15 04:02:26 +08:00
|
|
|
{
|
2001-08-23 10:59:04 +08:00
|
|
|
if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
|
2001-08-24 05:32:40 +08:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
|
|
|
|
PyErr_SetString(PyExc_OverflowError, msg);
|
2001-08-23 10:59:04 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
1990-10-15 04:02:26 +08:00
|
|
|
}
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
/* Integers are quite normal objects, to make object handling uniform.
|
|
|
|
(Using odd pointers to represent integers would save much space
|
|
|
|
but require extra checks for this special case throughout the code.)
|
|
|
|
Since, a typical Python program spends much of its time allocating
|
|
|
|
and deallocating integers, these operations should be very fast.
|
|
|
|
Therefore we use a dedicated allocation scheme with a much lower
|
|
|
|
overhead (in space and time) than straight malloc(): a simple
|
|
|
|
dedicated free list, filled when necessary with memory from malloc().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
|
1999-03-13 03:43:17 +08:00
|
|
|
#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
|
|
|
|
#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
|
1999-03-11 06:55:24 +08:00
|
|
|
|
1999-03-13 03:43:17 +08:00
|
|
|
struct _intblock {
|
|
|
|
struct _intblock *next;
|
|
|
|
PyIntObject objects[N_INTOBJECTS];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _intblock PyIntBlock;
|
|
|
|
|
|
|
|
static PyIntBlock *block_list = NULL;
|
|
|
|
static PyIntObject *free_list = NULL;
|
1990-12-20 23:06:42 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyIntObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
fill_free_list(void)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyIntObject *p, *q;
|
2000-05-04 07:44:39 +08:00
|
|
|
/* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
|
|
|
|
p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
|
1990-12-20 23:06:42 +08:00
|
|
|
if (p == NULL)
|
2000-05-04 07:44:39 +08:00
|
|
|
return (PyIntObject *) PyErr_NoMemory();
|
1999-03-13 03:43:17 +08:00
|
|
|
((PyIntBlock *)p)->next = block_list;
|
|
|
|
block_list = (PyIntBlock *)p;
|
|
|
|
p = &((PyIntBlock *)p)->objects[0];
|
1990-12-20 23:06:42 +08:00
|
|
|
q = p + N_INTOBJECTS;
|
|
|
|
while (--q > p)
|
1999-03-11 06:55:24 +08:00
|
|
|
q->ob_type = (struct _typeobject *)(q-1);
|
|
|
|
q->ob_type = NULL;
|
1990-12-20 23:06:42 +08:00
|
|
|
return p + N_INTOBJECTS - 1;
|
|
|
|
}
|
|
|
|
|
1993-10-16 00:18:48 +08:00
|
|
|
#ifndef NSMALLPOSINTS
|
|
|
|
#define NSMALLPOSINTS 100
|
|
|
|
#endif
|
|
|
|
#ifndef NSMALLNEGINTS
|
|
|
|
#define NSMALLNEGINTS 1
|
|
|
|
#endif
|
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
/* References to small integers are saved in this array so that they
|
|
|
|
can be shared.
|
|
|
|
The integers that are saved are those in the range
|
|
|
|
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
|
|
|
|
*/
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
|
1993-10-16 00:18:48 +08:00
|
|
|
#endif
|
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
int quick_int_allocs, quick_neg_int_allocs;
|
|
|
|
#endif
|
1990-12-20 23:06:42 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_FromLong(long ival)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
register PyIntObject *v;
|
1993-10-16 00:18:48 +08:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
|
|
|
|
(v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(v);
|
1993-10-16 00:18:48 +08:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
if (ival >= 0)
|
|
|
|
quick_int_allocs++;
|
|
|
|
else
|
|
|
|
quick_neg_int_allocs++;
|
|
|
|
#endif
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) v;
|
1993-10-16 00:18:48 +08:00
|
|
|
}
|
|
|
|
#endif
|
1990-12-20 23:06:42 +08:00
|
|
|
if (free_list == NULL) {
|
|
|
|
if ((free_list = fill_free_list()) == NULL)
|
|
|
|
return NULL;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
2000-05-04 07:44:39 +08:00
|
|
|
/* PyObject_New is inlined */
|
1990-12-20 23:06:42 +08:00
|
|
|
v = free_list;
|
1999-03-11 06:55:24 +08:00
|
|
|
free_list = (PyIntObject *)v->ob_type;
|
2000-05-04 07:44:39 +08:00
|
|
|
PyObject_INIT(v, &PyInt_Type);
|
1990-12-20 23:06:42 +08:00
|
|
|
v->ob_ival = ival;
|
1993-10-16 00:18:48 +08:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
|
|
|
|
/* save this one for a following allocation */
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(v);
|
1993-10-16 00:18:48 +08:00
|
|
|
small_ints[ival + NSMALLNEGINTS] = v;
|
|
|
|
}
|
|
|
|
#endif
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) v;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-09 23:16:51 +08:00
|
|
|
int_dealloc(PyIntObject *v)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
2001-09-12 00:13:52 +08:00
|
|
|
if (PyInt_CheckExact(v)) {
|
2001-08-29 23:47:46 +08:00
|
|
|
v->ob_type = (struct _typeobject *)free_list;
|
|
|
|
free_list = v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
v->ob_type->tp_free((PyObject *)v);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_AsLong(register PyObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyNumberMethods *nb;
|
|
|
|
PyIntObject *io;
|
1994-08-29 20:48:32 +08:00
|
|
|
long val;
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
if (op && PyInt_Check(op))
|
|
|
|
return PyInt_AS_LONG((PyIntObject*) op);
|
1994-08-29 20:48:32 +08:00
|
|
|
|
|
|
|
if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_int == NULL) {
|
2000-05-09 22:27:48 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "an integer is required");
|
1990-10-14 20:07:46 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1994-08-29 20:48:32 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
io = (PyIntObject*) (*nb->nb_int) (op);
|
1994-08-29 20:48:32 +08:00
|
|
|
if (io == NULL)
|
|
|
|
return -1;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyInt_Check(io)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"nb_int should return int object");
|
1994-08-29 20:48:32 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
val = PyInt_AS_LONG(io);
|
|
|
|
Py_DECREF(io);
|
1994-08-29 20:48:32 +08:00
|
|
|
|
|
|
|
return val;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1999-10-13 03:54:53 +08:00
|
|
|
PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_FromString(char *s, char **pend, int base)
|
1999-10-13 03:54:53 +08:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
long x;
|
|
|
|
char buffer[256]; /* For errors */
|
|
|
|
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
2000-10-25 03:57:45 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
|
1999-10-13 03:54:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
errno = 0;
|
|
|
|
if (base == 0 && s[0] == '0')
|
|
|
|
x = (long) PyOS_strtoul(s, &end, base);
|
|
|
|
else
|
|
|
|
x = PyOS_strtol(s, &end, base);
|
2001-03-06 20:12:02 +08:00
|
|
|
if (end == s || !isalnum(Py_CHARMASK(end[-1])))
|
1999-10-13 03:54:53 +08:00
|
|
|
goto bad;
|
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
|
|
|
if (*end != '\0') {
|
|
|
|
bad:
|
|
|
|
sprintf(buffer, "invalid literal for int(): %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (errno != 0) {
|
|
|
|
sprintf(buffer, "int() literal too large: %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pend)
|
|
|
|
*pend = end;
|
|
|
|
return PyInt_FromLong(x);
|
|
|
|
}
|
|
|
|
|
2001-08-18 02:39:25 +08:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-04-06 04:11:21 +08:00
|
|
|
PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
|
2000-04-06 04:11:21 +08:00
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
if (length >= sizeof(buffer)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"int() literal too large to convert");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromString(buffer, NULL, base);
|
|
|
|
}
|
2001-08-18 02:39:25 +08:00
|
|
|
#endif
|
2000-04-06 04:11:21 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Methods */
|
|
|
|
|
2001-01-04 09:45:33 +08:00
|
|
|
/* Integers are seen as the "smallest" of all numeric types and thus
|
|
|
|
don't have any knowledge about conversion of other types to
|
|
|
|
integers. */
|
|
|
|
|
|
|
|
#define CONVERT_TO_LONG(obj, lng) \
|
|
|
|
if (PyInt_Check(obj)) { \
|
|
|
|
lng = PyInt_AS_LONG(obj); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
Py_INCREF(Py_NotImplemented); \
|
|
|
|
return Py_NotImplemented; \
|
|
|
|
}
|
|
|
|
|
1992-03-28 01:31:02 +08:00
|
|
|
/* ARGSUSED */
|
1991-06-08 01:10:43 +09:00
|
|
|
static int
|
2000-07-09 23:16:51 +08:00
|
|
|
int_print(PyIntObject *v, FILE *fp, int flags)
|
|
|
|
/* flags -- not used but required by interface */
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
fprintf(fp, "%ld", v->ob_ival);
|
1991-06-08 01:10:43 +09:00
|
|
|
return 0;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_repr(PyIntObject *v)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
sprintf(buf, "%ld", v->ob_ival);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString(buf);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-09 23:16:51 +08:00
|
|
|
int_compare(PyIntObject *v, PyIntObject *w)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
register long i = v->ob_ival;
|
|
|
|
register long j = w->ob_ival;
|
|
|
|
return (i < j) ? -1 : (i > j) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
1993-03-29 18:43:31 +08:00
|
|
|
static long
|
2000-07-09 23:16:51 +08:00
|
|
|
int_hash(PyIntObject *v)
|
1993-03-29 18:43:31 +08:00
|
|
|
{
|
1997-01-07 06:53:20 +08:00
|
|
|
/* XXX If this is changed, you also need to change the way
|
|
|
|
Python's long, float and complex types are hashed. */
|
1993-03-29 18:43:31 +08:00
|
|
|
long x = v -> ob_ival;
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_add(PyIntObject *v, PyIntObject *w)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
register long a, b, x;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1990-10-14 20:07:46 +08:00
|
|
|
x = a + b;
|
2001-08-23 10:59:04 +08:00
|
|
|
if ((x^a) >= 0 || (x^b) >= 0)
|
|
|
|
return PyInt_FromLong(x);
|
|
|
|
if (err_ovf("integer addition"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_sub(PyIntObject *v, PyIntObject *w)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
register long a, b, x;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1990-10-14 20:07:46 +08:00
|
|
|
x = a - b;
|
2001-08-23 10:59:04 +08:00
|
|
|
if ((x^a) >= 0 || (x^~b) >= 0)
|
|
|
|
return PyInt_FromLong(x);
|
|
|
|
if (err_ovf("integer subtraction"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
|
|
|
|
(PyObject *)w);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1994-08-29 20:48:32 +08:00
|
|
|
/*
|
|
|
|
Integer overflow checking used to be done using a double, but on 64
|
|
|
|
bit machines (where both long and double are 64 bit) this fails
|
2000-07-16 20:04:32 +08:00
|
|
|
because the double doesn't have enough precision. John Tromp suggests
|
1994-08-29 20:48:32 +08:00
|
|
|
the following algorithm:
|
|
|
|
|
|
|
|
Suppose again we normalize a and b to be nonnegative.
|
|
|
|
Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
|
|
|
|
Now we test ah and bh against zero and get essentially 3 possible outcomes.
|
|
|
|
|
|
|
|
1) both ah and bh > 0 : then report overflow
|
|
|
|
|
|
|
|
2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
|
|
|
|
negative
|
|
|
|
|
|
|
|
3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
|
|
|
|
compute al*bl and report overflow if it's negative
|
|
|
|
add (ah*bl)<<32 to al*bl and report overflow if
|
|
|
|
it's negative
|
|
|
|
|
|
|
|
In case of no overflow the result is then negated if necessary.
|
|
|
|
|
|
|
|
The majority of cases will be 2), in which case this method is the same as
|
|
|
|
what I suggested before. If multiplication is expensive enough, then the
|
|
|
|
other method is faster on case 3), but also more work to program, so I
|
|
|
|
guess the above is the preferred solution.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2001-01-04 09:45:33 +08:00
|
|
|
int_mul(PyObject *v, PyObject *w)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1994-08-29 20:48:32 +08:00
|
|
|
long a, b, ah, bh, x, y;
|
|
|
|
int s = 1;
|
|
|
|
|
2001-09-15 11:14:32 +08:00
|
|
|
if (!PyInt_Check(v) &&
|
|
|
|
v->ob_type->tp_as_sequence &&
|
|
|
|
v->ob_type->tp_as_sequence->sq_repeat) {
|
2001-01-04 09:45:33 +08:00
|
|
|
/* sequence * int */
|
|
|
|
a = PyInt_AsLong(w);
|
|
|
|
return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
|
|
|
|
}
|
2001-09-15 11:14:32 +08:00
|
|
|
if (!PyInt_Check(w) &&
|
|
|
|
w->ob_type->tp_as_sequence &&
|
|
|
|
w->ob_type->tp_as_sequence->sq_repeat) {
|
2001-01-04 09:45:33 +08:00
|
|
|
/* int * sequence */
|
|
|
|
a = PyInt_AsLong(v);
|
|
|
|
return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1994-08-29 20:48:32 +08:00
|
|
|
ah = a >> (LONG_BIT/2);
|
|
|
|
bh = b >> (LONG_BIT/2);
|
|
|
|
|
|
|
|
/* Quick test for common case: two small positive ints */
|
|
|
|
|
|
|
|
if (ah == 0 && bh == 0) {
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(x);
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Arrange that a >= b >= 0 */
|
|
|
|
|
|
|
|
if (a < 0) {
|
|
|
|
a = -a;
|
|
|
|
if (a < 0) {
|
|
|
|
/* Largest negative */
|
|
|
|
if (b == 0 || b == 1) {
|
|
|
|
x = a*b;
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
s = -s;
|
|
|
|
ah = a >> (LONG_BIT/2);
|
|
|
|
}
|
|
|
|
if (b < 0) {
|
|
|
|
b = -b;
|
|
|
|
if (b < 0) {
|
|
|
|
/* Largest negative */
|
1996-12-07 04:14:43 +08:00
|
|
|
if (a == 0 || (a == 1 && s == 1)) {
|
1994-08-29 20:48:32 +08:00
|
|
|
x = a*b;
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
s = -s;
|
|
|
|
bh = b >> (LONG_BIT/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1) both ah and bh > 0 : then report overflow */
|
|
|
|
|
|
|
|
if (ah != 0 && bh != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/* 2) both ah and bh = 0 : then compute a*b and report
|
|
|
|
overflow if it comes out negative */
|
|
|
|
|
|
|
|
if (ah == 0 && bh == 0) {
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(x*s);
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a < b) {
|
|
|
|
/* Swap */
|
|
|
|
x = a;
|
|
|
|
a = b;
|
|
|
|
b = x;
|
|
|
|
ah = bh;
|
|
|
|
/* bh not used beyond this point */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
|
|
|
|
it's >= 2^31
|
|
|
|
compute al*bl and report overflow if it's negative
|
|
|
|
add (ah*bl)<<32 to al*bl and report overflow if
|
|
|
|
it's negative
|
|
|
|
(NB b == bl in this case, and we make a = al) */
|
|
|
|
|
|
|
|
y = ah*b;
|
1997-01-07 06:53:20 +08:00
|
|
|
if (y >= (1L << (LONG_BIT/2 - 1)))
|
1994-08-29 20:48:32 +08:00
|
|
|
goto bad;
|
|
|
|
a &= (1L << (LONG_BIT/2)) - 1;
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-01-07 06:53:20 +08:00
|
|
|
x += y << (LONG_BIT/2);
|
1994-08-29 20:48:32 +08:00
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
|
|
|
ok:
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(x * s);
|
1994-08-29 20:48:32 +08:00
|
|
|
|
|
|
|
bad:
|
2001-08-23 10:59:04 +08:00
|
|
|
if (err_ovf("integer multiplication"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_Type.tp_as_number->nb_multiply(v, w);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 10:59:04 +08:00
|
|
|
/* Return type of i_divmod */
|
|
|
|
enum divmod_result {
|
|
|
|
DIVMOD_OK, /* Correct result */
|
|
|
|
DIVMOD_OVERFLOW, /* Overflow, try again using longs */
|
|
|
|
DIVMOD_ERROR /* Exception raised */
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum divmod_result
|
2001-06-19 03:21:11 +08:00
|
|
|
i_divmod(register long x, register long y,
|
2000-07-09 23:16:51 +08:00
|
|
|
long *p_xdivy, long *p_xmody)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1992-01-20 00:28:51 +08:00
|
|
|
long xdivy, xmody;
|
|
|
|
|
2001-06-19 03:21:11 +08:00
|
|
|
if (y == 0) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_ZeroDivisionError,
|
2000-10-25 03:57:45 +08:00
|
|
|
"integer division or modulo by zero");
|
2001-08-23 10:59:04 +08:00
|
|
|
return DIVMOD_ERROR;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
2001-06-19 03:21:11 +08:00
|
|
|
/* (-sys.maxint-1)/-1 is the only overflow case. */
|
|
|
|
if (y == -1 && x < 0 && x == -x) {
|
2001-08-23 10:59:04 +08:00
|
|
|
if (err_ovf("integer division"))
|
|
|
|
return DIVMOD_ERROR;
|
|
|
|
return DIVMOD_OVERFLOW;
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
2001-06-19 03:21:11 +08:00
|
|
|
xdivy = x / y;
|
|
|
|
xmody = x - xdivy * y;
|
|
|
|
/* If the signs of x and y differ, and the remainder is non-0,
|
|
|
|
* C89 doesn't define whether xdivy is now the floor or the
|
|
|
|
* ceiling of the infinitely precise quotient. We want the floor,
|
|
|
|
* and we have it iff the remainder's sign matches y's.
|
|
|
|
*/
|
|
|
|
if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
|
|
|
|
xmody += y;
|
|
|
|
--xdivy;
|
|
|
|
assert(xmody && ((y ^ xmody) >= 0));
|
1992-01-20 00:28:51 +08:00
|
|
|
}
|
|
|
|
*p_xdivy = xdivy;
|
|
|
|
*p_xmody = xmody;
|
2001-08-23 10:59:04 +08:00
|
|
|
return DIVMOD_OK;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_div(PyIntObject *x, PyIntObject *y)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2001-01-04 09:45:33 +08:00
|
|
|
long xi, yi;
|
1992-01-20 00:28:51 +08:00
|
|
|
long d, m;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(x, xi);
|
|
|
|
CONVERT_TO_LONG(y, yi);
|
2001-08-23 10:59:04 +08:00
|
|
|
switch (i_divmod(xi, yi, &d, &m)) {
|
|
|
|
case DIVMOD_OK:
|
|
|
|
return PyInt_FromLong(d);
|
|
|
|
case DIVMOD_OVERFLOW:
|
|
|
|
return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
|
|
|
|
(PyObject *)y);
|
|
|
|
default:
|
1990-10-14 20:07:46 +08:00
|
|
|
return NULL;
|
2001-08-23 10:59:04 +08:00
|
|
|
}
|
1992-01-20 00:28:51 +08:00
|
|
|
}
|
|
|
|
|
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
2001-09-01 01:40:15 +08:00
|
|
|
static PyObject *
|
|
|
|
int_classic_div(PyIntObject *x, PyIntObject *y)
|
|
|
|
{
|
|
|
|
long xi, yi;
|
|
|
|
long d, m;
|
|
|
|
CONVERT_TO_LONG(x, xi);
|
|
|
|
CONVERT_TO_LONG(y, yi);
|
|
|
|
if (Py_DivisionWarningFlag &&
|
|
|
|
PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
|
|
|
|
return NULL;
|
|
|
|
switch (i_divmod(xi, yi, &d, &m)) {
|
|
|
|
case DIVMOD_OK:
|
|
|
|
return PyInt_FromLong(d);
|
|
|
|
case DIVMOD_OVERFLOW:
|
|
|
|
return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
|
|
|
|
(PyObject *)y);
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-04 13:52:47 +08:00
|
|
|
static PyObject *
|
|
|
|
int_true_divide(PyObject *v, PyObject *w)
|
|
|
|
{
|
2001-09-04 14:17:36 +08:00
|
|
|
/* If they aren't both ints, give someone else a chance. In
|
|
|
|
particular, this lets int/long get handled by longs, which
|
|
|
|
underflows to 0 gracefully if the long is too big to convert
|
|
|
|
to float. */
|
|
|
|
if (PyInt_Check(v) && PyInt_Check(w))
|
|
|
|
return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
|
|
|
|
Py_INCREF(Py_NotImplemented);
|
|
|
|
return Py_NotImplemented;
|
2001-09-04 13:52:47 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_mod(PyIntObject *x, PyIntObject *y)
|
1992-01-20 00:28:51 +08:00
|
|
|
{
|
2001-01-04 09:45:33 +08:00
|
|
|
long xi, yi;
|
1992-01-20 00:28:51 +08:00
|
|
|
long d, m;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(x, xi);
|
|
|
|
CONVERT_TO_LONG(y, yi);
|
2001-08-23 10:59:04 +08:00
|
|
|
switch (i_divmod(xi, yi, &d, &m)) {
|
|
|
|
case DIVMOD_OK:
|
|
|
|
return PyInt_FromLong(m);
|
|
|
|
case DIVMOD_OVERFLOW:
|
|
|
|
return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
|
|
|
|
(PyObject *)y);
|
|
|
|
default:
|
1992-01-20 00:28:51 +08:00
|
|
|
return NULL;
|
2001-08-23 10:59:04 +08:00
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_divmod(PyIntObject *x, PyIntObject *y)
|
1991-05-06 05:08:27 +09:00
|
|
|
{
|
2001-01-04 09:45:33 +08:00
|
|
|
long xi, yi;
|
1992-01-20 00:28:51 +08:00
|
|
|
long d, m;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(x, xi);
|
|
|
|
CONVERT_TO_LONG(y, yi);
|
2001-08-23 10:59:04 +08:00
|
|
|
switch (i_divmod(xi, yi, &d, &m)) {
|
|
|
|
case DIVMOD_OK:
|
|
|
|
return Py_BuildValue("(ll)", d, m);
|
|
|
|
case DIVMOD_OVERFLOW:
|
|
|
|
return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
|
|
|
|
(PyObject *)y);
|
|
|
|
default:
|
1991-05-06 05:08:27 +09:00
|
|
|
return NULL;
|
2001-08-23 10:59:04 +08:00
|
|
|
}
|
1991-05-06 05:08:27 +09:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1996-12-07 04:14:43 +08:00
|
|
|
register long iv, iw, iz=0, ix, temp, prev;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, iv);
|
|
|
|
CONVERT_TO_LONG(w, iw);
|
1994-08-29 20:48:32 +08:00
|
|
|
if (iw < 0) {
|
2001-09-03 16:35:41 +08:00
|
|
|
if ((PyObject *)z != Py_None) {
|
2001-09-05 14:24:58 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
|
|
|
|
"cannot be negative when 3rd argument specified");
|
2001-09-03 16:35:41 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-07-12 19:19:45 +08:00
|
|
|
/* Return a float. This works because we know that
|
|
|
|
this calls float_pow() which converts its
|
|
|
|
arguments to double. */
|
|
|
|
return PyFloat_Type.tp_as_number->nb_power(
|
|
|
|
(PyObject *)v, (PyObject *)w, (PyObject *)z);
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
if ((PyObject *)z != Py_None) {
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(z, iz);
|
1996-12-07 04:14:43 +08:00
|
|
|
if (iz == 0) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2001-09-05 14:24:58 +08:00
|
|
|
"pow() 3rd argument cannot be 0");
|
1996-12-07 04:14:43 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX: The original exponentiation code stopped looping
|
|
|
|
* when temp hit zero; this code will continue onwards
|
|
|
|
* unnecessarily, but at least it won't cause any errors.
|
|
|
|
* Hopefully the speed improvement from the fast exponentiation
|
|
|
|
* will compensate for the slight inefficiency.
|
|
|
|
* XXX: Better handling of overflows is desperately needed.
|
|
|
|
*/
|
|
|
|
temp = iv;
|
|
|
|
ix = 1;
|
|
|
|
while (iw > 0) {
|
|
|
|
prev = ix; /* Save value for overflow check */
|
|
|
|
if (iw & 1) {
|
|
|
|
ix = ix*temp;
|
|
|
|
if (temp == 0)
|
|
|
|
break; /* Avoid ix / 0 */
|
2001-08-23 10:59:04 +08:00
|
|
|
if (ix / temp != prev) {
|
|
|
|
if (err_ovf("integer exponentiation"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_Type.tp_as_number->nb_power(
|
|
|
|
(PyObject *)v,
|
|
|
|
(PyObject *)w,
|
2001-08-24 05:28:33 +08:00
|
|
|
(PyObject *)z);
|
2001-08-23 10:59:04 +08:00
|
|
|
}
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
|
|
|
iw >>= 1; /* Shift exponent down by 1 bit */
|
|
|
|
if (iw==0) break;
|
|
|
|
prev = temp;
|
|
|
|
temp *= temp; /* Square the value of temp */
|
2001-08-23 10:59:04 +08:00
|
|
|
if (prev!=0 && temp/prev!=prev) {
|
|
|
|
if (err_ovf("integer exponentiation"))
|
|
|
|
return NULL;
|
|
|
|
return PyLong_Type.tp_as_number->nb_power(
|
|
|
|
(PyObject *)v, (PyObject *)w, (PyObject *)z);
|
|
|
|
}
|
1996-12-07 04:14:43 +08:00
|
|
|
if (iz) {
|
1994-08-29 20:48:32 +08:00
|
|
|
/* If we did a multiplication, perform a modulo */
|
|
|
|
ix = ix % iz;
|
|
|
|
temp = temp % iz;
|
|
|
|
}
|
|
|
|
}
|
1996-12-07 04:14:43 +08:00
|
|
|
if (iz) {
|
2001-01-04 09:45:33 +08:00
|
|
|
long div, mod;
|
2001-08-23 10:59:04 +08:00
|
|
|
switch (i_divmod(ix, iz, &div, &mod)) {
|
|
|
|
case DIVMOD_OK:
|
|
|
|
ix = mod;
|
|
|
|
break;
|
|
|
|
case DIVMOD_OVERFLOW:
|
|
|
|
return PyLong_Type.tp_as_number->nb_power(
|
|
|
|
(PyObject *)v, (PyObject *)w, (PyObject *)z);
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(ix);
|
1994-08-29 20:48:32 +08:00
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_neg(PyIntObject *v)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
|
|
|
register long a, x;
|
|
|
|
a = v->ob_ival;
|
|
|
|
x = -a;
|
2001-08-23 10:59:04 +08:00
|
|
|
if (a < 0 && x < 0) {
|
|
|
|
if (err_ovf("integer negation"))
|
|
|
|
return NULL;
|
|
|
|
return PyNumber_Negative(PyLong_FromLong(a));
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(x);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_pos(PyIntObject *v)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2001-09-12 05:44:14 +08:00
|
|
|
if (PyInt_CheckExact(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *)v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return PyInt_FromLong(v->ob_ival);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_abs(PyIntObject *v)
|
1991-05-06 05:08:27 +09:00
|
|
|
{
|
|
|
|
if (v->ob_ival >= 0)
|
|
|
|
return int_pos(v);
|
|
|
|
else
|
|
|
|
return int_neg(v);
|
|
|
|
}
|
|
|
|
|
1991-05-14 21:05:32 +09:00
|
|
|
static int
|
2000-07-09 23:16:51 +08:00
|
|
|
int_nonzero(PyIntObject *v)
|
1991-05-14 21:05:32 +09:00
|
|
|
{
|
|
|
|
return v->ob_ival != 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_invert(PyIntObject *v)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(~v->ob_ival);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_lshift(PyIntObject *v, PyIntObject *w)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
|
|
|
register long a, b;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1992-01-15 02:33:22 +08:00
|
|
|
if (b < 0) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
1992-01-15 02:33:22 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-12 05:44:14 +08:00
|
|
|
if (a == 0 || b == 0)
|
|
|
|
return int_pos(v);
|
1993-10-26 23:21:51 +08:00
|
|
|
if (b >= LONG_BIT) {
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(0L);
|
1992-01-15 02:33:22 +08:00
|
|
|
}
|
2001-07-20 05:49:38 +08:00
|
|
|
a = (long)((unsigned long)a << b);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(a);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_rshift(PyIntObject *v, PyIntObject *w)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
|
|
|
register long a, b;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1992-01-15 02:33:22 +08:00
|
|
|
if (b < 0) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
1992-01-15 02:33:22 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-12 05:44:14 +08:00
|
|
|
if (a == 0 || b == 0)
|
|
|
|
return int_pos(v);
|
1993-10-26 23:21:51 +08:00
|
|
|
if (b >= LONG_BIT) {
|
1992-01-15 02:33:22 +08:00
|
|
|
if (a < 0)
|
|
|
|
a = -1;
|
|
|
|
else
|
|
|
|
a = 0;
|
|
|
|
}
|
|
|
|
else {
|
2000-07-08 12:17:21 +08:00
|
|
|
a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
|
1992-01-15 02:33:22 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(a);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_and(PyIntObject *v, PyIntObject *w)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
|
|
|
register long a, b;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(a & b);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_xor(PyIntObject *v, PyIntObject *w)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
|
|
|
register long a, b;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(a ^ b);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_or(PyIntObject *v, PyIntObject *w)
|
1991-10-24 22:59:31 +08:00
|
|
|
{
|
|
|
|
register long a, b;
|
2001-01-04 09:45:33 +08:00
|
|
|
CONVERT_TO_LONG(v, a);
|
|
|
|
CONVERT_TO_LONG(w, b);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(a | b);
|
1991-10-24 22:59:31 +08:00
|
|
|
}
|
|
|
|
|
2001-09-19 09:25:16 +08:00
|
|
|
static int
|
|
|
|
int_coerce(PyObject **pv, PyObject **pw)
|
|
|
|
{
|
|
|
|
if (PyInt_Check(*pw)) {
|
|
|
|
Py_INCREF(*pv);
|
|
|
|
Py_INCREF(*pw);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1; /* Can't do it */
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_int(PyIntObject *v)
|
1992-09-12 19:09:23 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *)v;
|
1992-09-12 19:09:23 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_long(PyIntObject *v)
|
1992-09-12 19:09:23 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyLong_FromLong((v -> ob_ival));
|
1992-09-12 19:09:23 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_float(PyIntObject *v)
|
1992-09-12 19:09:23 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyFloat_FromDouble((double)(v -> ob_ival));
|
1992-09-12 19:09:23 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_oct(PyIntObject *v)
|
1992-09-12 19:09:23 +08:00
|
|
|
{
|
1997-01-14 23:43:41 +08:00
|
|
|
char buf[100];
|
1993-03-29 18:43:31 +08:00
|
|
|
long x = v -> ob_ival;
|
1992-09-12 19:09:23 +08:00
|
|
|
if (x == 0)
|
|
|
|
strcpy(buf, "0");
|
|
|
|
else
|
1997-01-13 03:48:03 +08:00
|
|
|
sprintf(buf, "0%lo", x);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString(buf);
|
1992-09-12 19:09:23 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:16:51 +08:00
|
|
|
int_hex(PyIntObject *v)
|
1992-09-12 19:09:23 +08:00
|
|
|
{
|
1997-01-14 23:43:41 +08:00
|
|
|
char buf[100];
|
1993-03-29 18:43:31 +08:00
|
|
|
long x = v -> ob_ival;
|
1997-01-13 03:48:03 +08:00
|
|
|
sprintf(buf, "0x%lx", x);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString(buf);
|
1992-09-12 19:09:23 +08:00
|
|
|
}
|
|
|
|
|
2001-08-29 23:47:46 +08:00
|
|
|
staticforward PyObject *
|
|
|
|
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static PyObject *
|
|
|
|
int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyObject *x = NULL;
|
|
|
|
int base = -909;
|
|
|
|
static char *kwlist[] = {"x", "base", 0};
|
|
|
|
|
2001-08-29 23:47:46 +08:00
|
|
|
if (type != &PyInt_Type)
|
|
|
|
return int_subtype_new(type, args, kwds); /* Wimp out */
|
2001-08-02 12:15:00 +08:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
|
|
|
|
&x, &base))
|
|
|
|
return NULL;
|
|
|
|
if (x == NULL)
|
|
|
|
return PyInt_FromLong(0L);
|
|
|
|
if (base == -909)
|
|
|
|
return PyNumber_Int(x);
|
|
|
|
if (PyString_Check(x))
|
|
|
|
return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
|
2001-08-18 02:39:25 +08:00
|
|
|
#ifdef Py_USING_UNICODE
|
2001-08-02 12:15:00 +08:00
|
|
|
if (PyUnicode_Check(x))
|
|
|
|
return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
|
|
|
|
PyUnicode_GET_SIZE(x),
|
|
|
|
base);
|
2001-08-18 02:39:25 +08:00
|
|
|
#endif
|
2001-08-02 12:15:00 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"int() can't convert non-string with explicit base");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-29 23:47:46 +08:00
|
|
|
/* Wimpy, slow approach to tp_new calls for subtypes of int:
|
|
|
|
first create a regular int from whatever arguments we got,
|
|
|
|
then allocate a subtype instance and initialize its ob_ival
|
|
|
|
from the regular int. The regular int is then thrown away.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyObject *tmp, *new;
|
|
|
|
|
|
|
|
assert(PyType_IsSubtype(type, &PyInt_Type));
|
|
|
|
tmp = int_new(&PyInt_Type, args, kwds);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
assert(PyInt_Check(tmp));
|
2001-08-30 11:09:31 +08:00
|
|
|
new = type->tp_alloc(type, 0);
|
2001-08-29 23:47:46 +08:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
|
|
|
((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2001-08-02 12:15:00 +08:00
|
|
|
static char int_doc[] =
|
|
|
|
"int(x[, base]) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Convert a string or number to an integer, if possible. A floating point\n\
|
|
|
|
argument will be truncated towards zero (this does not include a string\n\
|
|
|
|
representation of a floating point number!) When converting a string, use\n\
|
|
|
|
the optional base. It is an error to supply a base when converting a\n\
|
|
|
|
non-string.";
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyNumberMethods int_as_number = {
|
2001-01-04 09:45:33 +08:00
|
|
|
(binaryfunc)int_add, /*nb_add*/
|
|
|
|
(binaryfunc)int_sub, /*nb_subtract*/
|
|
|
|
(binaryfunc)int_mul, /*nb_multiply*/
|
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
2001-09-01 01:40:15 +08:00
|
|
|
(binaryfunc)int_classic_div, /*nb_divide*/
|
2001-01-04 09:45:33 +08:00
|
|
|
(binaryfunc)int_mod, /*nb_remainder*/
|
|
|
|
(binaryfunc)int_divmod, /*nb_divmod*/
|
|
|
|
(ternaryfunc)int_pow, /*nb_power*/
|
|
|
|
(unaryfunc)int_neg, /*nb_negative*/
|
|
|
|
(unaryfunc)int_pos, /*nb_positive*/
|
|
|
|
(unaryfunc)int_abs, /*nb_absolute*/
|
|
|
|
(inquiry)int_nonzero, /*nb_nonzero*/
|
|
|
|
(unaryfunc)int_invert, /*nb_invert*/
|
|
|
|
(binaryfunc)int_lshift, /*nb_lshift*/
|
|
|
|
(binaryfunc)int_rshift, /*nb_rshift*/
|
|
|
|
(binaryfunc)int_and, /*nb_and*/
|
|
|
|
(binaryfunc)int_xor, /*nb_xor*/
|
|
|
|
(binaryfunc)int_or, /*nb_or*/
|
2001-09-19 09:25:16 +08:00
|
|
|
int_coerce, /*nb_coerce*/
|
2001-01-04 09:45:33 +08:00
|
|
|
(unaryfunc)int_int, /*nb_int*/
|
|
|
|
(unaryfunc)int_long, /*nb_long*/
|
|
|
|
(unaryfunc)int_float, /*nb_float*/
|
|
|
|
(unaryfunc)int_oct, /*nb_oct*/
|
|
|
|
(unaryfunc)int_hex, /*nb_hex*/
|
|
|
|
0, /*nb_inplace_add*/
|
|
|
|
0, /*nb_inplace_subtract*/
|
|
|
|
0, /*nb_inplace_multiply*/
|
|
|
|
0, /*nb_inplace_divide*/
|
|
|
|
0, /*nb_inplace_remainder*/
|
|
|
|
0, /*nb_inplace_power*/
|
|
|
|
0, /*nb_inplace_lshift*/
|
|
|
|
0, /*nb_inplace_rshift*/
|
|
|
|
0, /*nb_inplace_and*/
|
|
|
|
0, /*nb_inplace_xor*/
|
|
|
|
0, /*nb_inplace_or*/
|
2001-08-08 13:00:18 +08:00
|
|
|
(binaryfunc)int_div, /* nb_floor_divide */
|
|
|
|
int_true_divide, /* nb_true_divide */
|
|
|
|
0, /* nb_inplace_floor_divide */
|
|
|
|
0, /* nb_inplace_true_divide */
|
1990-10-14 20:07:46 +08:00
|
|
|
};
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject PyInt_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
|
|
|
"int",
|
1997-05-02 11:12:38 +08:00
|
|
|
sizeof(PyIntObject),
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
2001-08-02 12:15:00 +08:00
|
|
|
(destructor)int_dealloc, /* tp_dealloc */
|
|
|
|
(printfunc)int_print, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
(cmpfunc)int_compare, /* tp_compare */
|
|
|
|
(reprfunc)int_repr, /* tp_repr */
|
|
|
|
&int_as_number, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
(hashfunc)int_hash, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 23:47:46 +08:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
|
|
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
2001-08-02 12:15:00 +08:00
|
|
|
int_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
int_new, /* tp_new */
|
1990-10-14 20:07:46 +08:00
|
|
|
};
|
1997-08-05 10:16:08 +08:00
|
|
|
|
|
|
|
void
|
2000-07-09 23:16:51 +08:00
|
|
|
PyInt_Fini(void)
|
1997-08-05 10:16:08 +08:00
|
|
|
{
|
1999-03-13 03:43:17 +08:00
|
|
|
PyIntObject *p;
|
|
|
|
PyIntBlock *list, *next;
|
1997-08-05 10:16:08 +08:00
|
|
|
int i;
|
1999-03-11 06:55:24 +08:00
|
|
|
int bc, bf; /* block count, number of freed blocks */
|
|
|
|
int irem, isum; /* remaining unfreed ints per block, total */
|
1997-08-05 10:16:08 +08:00
|
|
|
|
1999-03-11 06:55:24 +08:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
PyIntObject **q;
|
|
|
|
|
|
|
|
i = NSMALLNEGINTS + NSMALLPOSINTS;
|
|
|
|
q = small_ints;
|
|
|
|
while (--i >= 0) {
|
|
|
|
Py_XDECREF(*q);
|
|
|
|
*q++ = NULL;
|
|
|
|
}
|
1997-08-05 10:16:08 +08:00
|
|
|
#endif
|
1999-03-11 06:55:24 +08:00
|
|
|
bc = 0;
|
|
|
|
bf = 0;
|
|
|
|
isum = 0;
|
|
|
|
list = block_list;
|
|
|
|
block_list = NULL;
|
1999-03-20 04:30:39 +08:00
|
|
|
free_list = NULL;
|
1999-03-11 06:55:24 +08:00
|
|
|
while (list != NULL) {
|
|
|
|
bc++;
|
|
|
|
irem = 0;
|
1999-03-20 04:30:39 +08:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
2001-09-12 00:13:52 +08:00
|
|
|
if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
|
1999-03-11 06:55:24 +08:00
|
|
|
irem++;
|
|
|
|
}
|
1999-03-13 03:43:17 +08:00
|
|
|
next = list->next;
|
1999-03-11 06:55:24 +08:00
|
|
|
if (irem) {
|
1999-03-13 03:43:17 +08:00
|
|
|
list->next = block_list;
|
|
|
|
block_list = list;
|
1999-03-20 04:30:39 +08:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
2001-09-12 00:13:52 +08:00
|
|
|
if (!PyInt_CheckExact(p) ||
|
2001-08-29 23:47:46 +08:00
|
|
|
p->ob_refcnt == 0) {
|
1999-03-20 04:30:39 +08:00
|
|
|
p->ob_type = (struct _typeobject *)
|
|
|
|
free_list;
|
|
|
|
free_list = p;
|
|
|
|
}
|
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
else if (-NSMALLNEGINTS <= p->ob_ival &&
|
|
|
|
p->ob_ival < NSMALLPOSINTS &&
|
|
|
|
small_ints[p->ob_ival +
|
|
|
|
NSMALLNEGINTS] == NULL) {
|
|
|
|
Py_INCREF(p);
|
|
|
|
small_ints[p->ob_ival +
|
|
|
|
NSMALLNEGINTS] = p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
1999-03-11 06:55:24 +08:00
|
|
|
}
|
|
|
|
else {
|
2000-05-04 07:44:39 +08:00
|
|
|
PyMem_FREE(list); /* XXX PyObject_FREE ??? */
|
1999-03-11 06:55:24 +08:00
|
|
|
bf++;
|
|
|
|
}
|
|
|
|
isum += irem;
|
1999-03-13 03:43:17 +08:00
|
|
|
list = next;
|
1999-03-11 06:55:24 +08:00
|
|
|
}
|
1999-03-13 03:43:17 +08:00
|
|
|
if (!Py_VerboseFlag)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "# cleanup ints");
|
|
|
|
if (!isum) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr,
|
|
|
|
": %d unfreed int%s in %d out of %d block%s\n",
|
|
|
|
isum, isum == 1 ? "" : "s",
|
|
|
|
bc - bf, bc, bc == 1 ? "" : "s");
|
|
|
|
}
|
|
|
|
if (Py_VerboseFlag > 1) {
|
|
|
|
list = block_list;
|
|
|
|
while (list != NULL) {
|
1999-03-20 04:30:39 +08:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
2001-09-12 00:13:52 +08:00
|
|
|
if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
|
1999-03-13 03:43:17 +08:00
|
|
|
fprintf(stderr,
|
2000-06-30 23:01:00 +08:00
|
|
|
"# <int at %p, refcnt=%d, val=%ld>\n",
|
|
|
|
p, p->ob_refcnt, p->ob_ival);
|
1999-03-13 03:43:17 +08:00
|
|
|
}
|
|
|
|
list = list->next;
|
1999-03-11 06:55:24 +08:00
|
|
|
}
|
|
|
|
}
|
1997-08-05 10:16:08 +08:00
|
|
|
}
|