From ddefaf31b366ea84250fc5090837c2b764a04102 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 14 Jan 2007 03:31:43 +0000 Subject: Merged the int/long unification branch, by very crude means (sorry Thomas!). I banged on the code (beyond what's in that branch) to make fewer tests fail; the only tests that fail now are: test_descr -- can't pickle ints?! test_pickletools -- ??? test_socket -- See python.org/sf/1619659 test_sqlite -- ??? I'll deal with those later. --- BROKEN_TESTS | 4 + INTBENCH | 50 ++++ Include/abstract.h | 2 +- Include/boolobject.h | 6 +- Include/intobject.h | 31 +-- Include/longobject.h | 8 +- Lib/pickle.py | 22 +- Lib/test/output/test_class | 2 +- Lib/test/test_builtin.py | 8 +- Lib/test/test_descr.py | 7 - Lib/test/test_marshal.py | 2 +- Lib/test/test_optparse.py | 6 +- Modules/_csv.c | 4 +- Modules/_ctypes/_ctypes.c | 2 +- Modules/_ctypes/callproc.c | 6 - Modules/_cursesmodule.c | 6 +- Modules/_sre.c | 13 +- Modules/_struct.c | 2 - Modules/_testcapimodule.c | 116 +++++++++ Modules/_tkinter.c | 2 +- Modules/cPickle.c | 42 +-- Modules/cjkcodecs/multibytecodec.c | 9 + Modules/datetimemodule.c | 7 +- Modules/dlmodule.c | 6 +- Modules/posixmodule.c | 33 +-- Modules/socketmodule.c | 16 +- Modules/timemodule.c | 2 +- Objects/abstract.c | 85 ++---- Objects/boolobject.c | 43 ++- Objects/complexobject.c | 4 - Objects/exceptions.c | 16 +- Objects/fileobject.c | 4 +- Objects/floatobject.c | 31 +-- Objects/frameobject.c | 2 +- Objects/intobject.c | 2 + Objects/listobject.c | 2 +- Objects/longobject.c | 520 ++++++++++++++++++++++++++++--------- Objects/stringobject.c | 12 + Objects/unicodeobject.c | 4 + PC/_msi.c | 2 +- Python/bltinmodule.c | 2 +- Python/ceval.c | 2 +- Python/getargs.c | 4 +- Python/marshal.c | 43 +-- Python/pythonrun.c | 8 +- Python/traceback.c | 2 +- 46 files changed, 798 insertions(+), 404 deletions(-) create mode 100644 BROKEN_TESTS create mode 100644 INTBENCH diff --git a/BROKEN_TESTS b/BROKEN_TESTS new file mode 100644 index 0000000..f45d333 --- /dev/null +++ b/BROKEN_TESTS @@ -0,0 +1,4 @@ +test_descr -- can't pickle int objects?!?! +test_pickletools -- ??? +test_socket -- OverflowError: can't convert negative value to unsigned int +test_sqlite -- ??? diff --git a/INTBENCH b/INTBENCH new file mode 100644 index 0000000..495a824 --- /dev/null +++ b/INTBENCH @@ -0,0 +1,50 @@ +Measurements of _testcapi.profile_int, on an 800MHz G3, OSX 10.4.7 +best of three runs + +r51476 (original p3yk) +Test 1: 1.601978s +Test 2: 1.696623s +Test 3: 1.900683s +Test 4: 5.307155s +Test 5: 2.546707s +Test 6: 1.670252s +Test 7: 1.910734s + +r51506 (int type dropped) +Test 1: 4.134757s +Test 2: 4.398235s +Test 3: 4.611636s +Test 4: 10.665429s + +r51509 (small int cache) +Test 1: 3.457184s +Test 2: 4.514800s +Test 3: 4.999010s +Test 4: 10.818277s + +r51452 (special-casing medium int allocation) +Test 1: 3.258219s +Test 2: 4.255007s +Test 3: 4.547923s +Test 4: 10.615123s +Test 5: 5.255545s +Test 6: 3.775941s +Test 7: 4.001805s + +r51562 (special-case one-digit operations) +Test 1: 3.527860s +Test 2: 3.975953s +Test 3: 4.226751s +Test 4: 10.605721s +Test 5: 5.233576s +Test 6: 2.161525s +Test 7: 3.421624s + +r51573 (speed up PyLong_FromLong) +Test 1: 3.149116s +Test 2: 3.948204s +Test 3: 4.012080s +Test 4: 8.589921s +Test 5: 4.481723s +Test 6: 2.259849s +Test 7: 3.453212s diff --git a/Include/abstract.h b/Include/abstract.h index 87b975d..1eab5f8 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -716,7 +716,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ is cleared and the value is clipped. */ - PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o); + #define PyNumber_Int PyNumber_Long /* Returns the o converted to an integer object on success, or diff --git a/Include/boolobject.h b/Include/boolobject.h index 7f9ad01..f5d7eec 100644 --- a/Include/boolobject.h +++ b/Include/boolobject.h @@ -7,8 +7,6 @@ extern "C" { #endif -typedef PyIntObject PyBoolObject; - PyAPI_DATA(PyTypeObject) PyBool_Type; #define PyBool_Check(x) ((x)->ob_type == &PyBool_Type) @@ -17,10 +15,10 @@ PyAPI_DATA(PyTypeObject) PyBool_Type; Don't forget to apply Py_INCREF() when returning either!!! */ /* Don't use these directly */ -PyAPI_DATA(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct; +PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct; /* Use these macros */ -#define Py_False ((PyObject *) &_Py_ZeroStruct) +#define Py_False ((PyObject *) &_Py_FalseStruct) #define Py_True ((PyObject *) &_Py_TrueStruct) /* Macros for returning Py_True or Py_False, respectively */ diff --git a/Include/intobject.h b/Include/intobject.h index 1f4846e..3a555d6 100644 --- a/Include/intobject.h +++ b/Include/intobject.h @@ -20,34 +20,31 @@ _Py_TrueStruct and _Py_ZeroStruct in boolobject.h; don't use this. extern "C" { #endif +/* typedef struct { PyObject_HEAD long ob_ival; } PyIntObject; PyAPI_DATA(PyTypeObject) PyInt_Type; +*/ -#define PyInt_Check(op) PyObject_TypeCheck(op, &PyInt_Type) -#define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type) +#define PyInt_Check(op) PyLong_Check(op) +#define PyInt_CheckExact(op) (PyLong_CheckExact(op) && _PyLong_FitsInLong(op)) -PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int); -#ifdef Py_USING_UNICODE -PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, Py_ssize_t, int); -#endif -PyAPI_FUNC(PyObject *) PyInt_FromLong(long); -PyAPI_FUNC(PyObject *) PyInt_FromSize_t(size_t); -PyAPI_FUNC(PyObject *) PyInt_FromSsize_t(Py_ssize_t); -PyAPI_FUNC(long) PyInt_AsLong(PyObject *); -PyAPI_FUNC(Py_ssize_t) PyInt_AsSsize_t(PyObject *); -PyAPI_FUNC(unsigned long) PyInt_AsUnsignedLongMask(PyObject *); -#ifdef HAVE_LONG_LONG -PyAPI_FUNC(unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *); -#endif +#define PyInt_FromString PyLong_FromString +#define PyInt_FromUnicode PyLong_FromUnicode +#define PyInt_FromLong PyLong_FromLong +#define PyInt_FromSize_t PyLong_FromSize_t +#define PyInt_FromSsize_t PyLong_FromSsize_t +#define PyInt_AsLong PyLong_AsLong +#define PyInt_AsSsize_t PyLong_AsSsize_t +#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask +#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask PyAPI_FUNC(long) PyInt_GetMax(void); -/* Macro, trading safety for speed */ -#define PyInt_AS_LONG(op) (((PyIntObject *)(op))->ob_ival) +#define PyInt_AS_LONG(op) PyLong_AsLong(op) /* These aren't really part of the Int object, but they're handy; the protos * are necessary for systems that need the magic of PyAPI_FUNC and that want diff --git a/Include/longobject.h b/Include/longobject.h index eef4e9b..7f2cda6 100644 --- a/Include/longobject.h +++ b/Include/longobject.h @@ -16,15 +16,16 @@ PyAPI_DATA(PyTypeObject) PyLong_Type; PyAPI_FUNC(PyObject *) PyLong_FromLong(long); PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long); +PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t); +PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t); PyAPI_FUNC(PyObject *) PyLong_FromDouble(double); PyAPI_FUNC(long) PyLong_AsLong(PyObject *); +PyAPI_FUNC(ssize_t) PyLong_AsSsize_t(PyObject *); +PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); /* For use by intobject.c only */ -PyAPI_FUNC(Py_ssize_t) _PyLong_AsSsize_t(PyObject *); -PyAPI_FUNC(PyObject *) _PyLong_FromSize_t(size_t); -PyAPI_FUNC(PyObject *) _PyLong_FromSsize_t(Py_ssize_t); PyAPI_DATA(int) _PyLong_DigitValue[256]; /* _PyLong_AsScaledDouble returns a double x and an exponent e such that @@ -34,6 +35,7 @@ PyAPI_DATA(int) _PyLong_DigitValue[256]; be multiplied by SHIFT! There may not be enough room in an int to store e*SHIFT directly. */ PyAPI_FUNC(double) _PyLong_AsScaledDouble(PyObject *vv, int *e); + PyAPI_FUNC(int) _PyLong_FitsInLong(PyObject* vv); PyAPI_FUNC(double) PyLong_AsDouble(PyObject *); PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *); diff --git a/Lib/pickle.py b/Lib/pickle.py index b32ee83..85c79d3 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -456,9 +456,29 @@ class Pickler: return # Text pickle, or int too big to fit in signed 4-byte format. self.write(INT + repr(obj) + '\n') - dispatch[IntType] = save_int + # XXX save_int is merged into save_long + # dispatch[IntType] = save_int def save_long(self, obj, pack=struct.pack): + if self.bin: + # If the int is small enough to fit in a signed 4-byte 2's-comp + # format, we can store it more efficiently than the general + # case. + # First one- and two-byte unsigned ints: + if obj >= 0: + if obj <= 0xff: + self.write(BININT1 + chr(obj)) + return + if obj <= 0xffff: + self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8)) + return + # Next check for 4-byte signed ints: + high_bits = obj >> 31 # note that Python shift sign-extends + if high_bits == 0 or high_bits == -1: + # All high bits are copies of bit 2**31, so the value + # fits in a 4-byte signed int. + self.write(BININT + pack("= 2: bytes = encode_long(obj) n = len(bytes) diff --git a/Lib/test/output/test_class b/Lib/test/output/test_class index f3dc490..f6ba475 100644 --- a/Lib/test/output/test_class +++ b/Lib/test/output/test_class @@ -44,7 +44,7 @@ __neg__: () __pos__: () __abs__: () __int__: () -__long__: () +__int__: () __float__: () __oct__: () __hex__: () diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index b001478..fb859d4 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1081,10 +1081,12 @@ class BuiltinTest(unittest.TestCase): self.assertEqual(long(Foo0()), 42L) self.assertEqual(long(Foo1()), 42L) - self.assertEqual(long(Foo2()), 42L) + # XXX invokes __int__ now + # self.assertEqual(long(Foo2()), 42L) self.assertEqual(long(Foo3()), 0) - self.assertEqual(long(Foo4()), 42) - self.assertRaises(TypeError, long, Foo5()) + # XXX likewise + # self.assertEqual(long(Foo4()), 42) + # self.assertRaises(TypeError, long, Foo5()) def test_map(self): self.assertEqual( diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 96ff9f3..bc95226 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -430,13 +430,6 @@ def ints(): pass else: raise TestFailed, "NotImplemented should have caused TypeError" - import sys - try: - C(sys.maxint+1) - except OverflowError: - pass - else: - raise TestFailed, "should have raised OverflowError" def longs(): if verbose: print "Testing long operations..." diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index 4467577..fa1f688 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -204,7 +204,7 @@ class BugsTestCase(unittest.TestCase): def test_patch_873224(self): self.assertRaises(Exception, marshal.loads, '0') self.assertRaises(Exception, marshal.loads, 'f') - self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1]) + self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65L)[:-1]) def test_version_argument(self): # Python 2.4.0 crashes for any call to marshal.dumps(x, y) diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index 1c4970f..aa6525e 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -1600,7 +1600,7 @@ class TestParseNumber(BaseTest): self.assertRaises( _parse_num, ("0xOoops", long), {}, ValueError, - re.compile(r"invalid literal for long().*: '?0xOoops'?")) + re.compile(r"invalid literal for int().*: '?0xOoops'?")) def test_parse_num_ok(self): self.assertEqual(_parse_num("0", int), 0) @@ -1618,9 +1618,9 @@ class TestParseNumber(BaseTest): self.assertParseFail(["-n008"], "option -n: invalid integer value: '008'") self.assertParseFail(["-l0b0123"], - "option -l: invalid long integer value: '0b0123'") + "option -l: invalid integer value: '0b0123'") self.assertParseFail(["-l", "0x12x"], - "option -l: invalid long integer value: '0x12x'") + "option -l: invalid integer value: '0x12x'") def _testclasses(): diff --git a/Modules/_csv.c b/Modules/_csv.c index a341eb4..e99acf9 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -219,7 +219,7 @@ _set_int(const char *name, int *target, PyObject *src, int dflt) if (src == NULL) *target = dflt; else { - if (!PyInt_Check(src)) { + if (!PyInt_CheckExact(src)) { PyErr_Format(PyExc_TypeError, "\"%s\" must be an integer", name); return -1; @@ -1410,7 +1410,7 @@ csv_field_size_limit(PyObject *module, PyObject *args) if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit)) return NULL; if (new_limit != NULL) { - if (!PyInt_Check(new_limit)) { + if (!PyInt_CheckExact(new_limit)) { PyErr_Format(PyExc_TypeError, "limit must be an integer"); return NULL; diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index dc7feeb..319d15e 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -991,7 +991,7 @@ ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */ - if (!proto || !PyInt_Check(proto)) { + if (!proto || !PyInt_CheckExact(proto)) { PyErr_SetString(PyExc_AttributeError, "class must define a '_length_' attribute, " "which must be a positive integer"); diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 18e1991..572a9fc 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -496,12 +496,6 @@ static int ConvParam(PyObject *obj, int index, struct argument *pa) return 0; } - if (PyInt_Check(obj)) { - pa->ffi_type = &ffi_type_sint; - pa->value.i = PyInt_AS_LONG(obj); - return 0; - } - if (PyLong_Check(obj)) { pa->ffi_type = &ffi_type_sint; pa->value.i = (long)PyLong_AsUnsignedLong(obj); diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index aeb1ef5..19c3b26 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -193,7 +193,7 @@ PyCursesCheckERR(int code, char *fname) static int PyCurses_ConvertToChtype(PyObject *obj, chtype *ch) { - if (PyInt_Check(obj)) { + if (PyInt_CheckExact(obj)) { *ch = (chtype) PyInt_AsLong(obj); } else if(PyString_Check(obj) && (PyString_Size(obj) == 1)) { @@ -2364,7 +2364,7 @@ PyCurses_UnCtrl(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL; - if (PyInt_Check(temp)) + if (PyInt_CheckExact(temp)) ch = (chtype) PyInt_AsLong(temp); else if (PyString_Check(temp)) ch = (chtype) *PyString_AsString(temp); @@ -2386,7 +2386,7 @@ PyCurses_UngetCh(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL; - if (PyInt_Check(temp)) + if (PyInt_CheckExact(temp)) ch = (int) PyInt_AsLong(temp); else if (PyString_Check(temp)) ch = (int) *PyString_AsString(temp); diff --git a/Modules/_sre.c b/Modules/_sre.c index a24f286..d402965 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2688,8 +2688,7 @@ _compile(PyObject* self_, PyObject* args) for (i = 0; i < n; i++) { PyObject *o = PyList_GET_ITEM(code, i); - unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o) - : PyLong_AsUnsignedLong(o); + unsigned long value = PyLong_AsUnsignedLong(o); self->code[i] = (SRE_CODE) value; if ((unsigned long) self->code[i] != value) { PyErr_SetString(PyExc_OverflowError, @@ -2763,6 +2762,10 @@ match_getindex(MatchObject* self, PyObject* index) { Py_ssize_t i; + if (index == NULL) + /* Default value */ + return 0; + if (PyInt_Check(index)) return PyInt_AsSsize_t(index); @@ -2913,7 +2916,7 @@ match_start(MatchObject* self, PyObject* args) { Py_ssize_t index; - PyObject* index_ = Py_False; /* zero */ + PyObject* index_ = NULL; if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_)) return NULL; @@ -2936,7 +2939,7 @@ match_end(MatchObject* self, PyObject* args) { Py_ssize_t index; - PyObject* index_ = Py_False; /* zero */ + PyObject* index_ = NULL; if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_)) return NULL; @@ -2986,7 +2989,7 @@ match_span(MatchObject* self, PyObject* args) { Py_ssize_t index; - PyObject* index_ = Py_False; /* zero */ + PyObject* index_ = NULL; if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_)) return NULL; diff --git a/Modules/_struct.c b/Modules/_struct.c index 8d0a3bc..518a57e 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -118,8 +118,6 @@ get_pylong(PyObject *v) PyNumberMethods *m; assert(v != NULL); - if (PyInt_Check(v)) - return PyLong_FromLong(PyInt_AS_LONG(v)); if (PyLong_Check(v)) { Py_INCREF(v); return v; diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index b11f0ae..09bb4ff 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -718,6 +718,119 @@ test_with_docstring(PyObject *self) Py_RETURN_NONE; } +#ifdef HAVE_GETTIMEOFDAY +/* Profiling of integer performance */ +void print_delta(int test, struct timeval *s, struct timeval *e) +{ + e->tv_sec -= s->tv_sec; + e->tv_usec -= s->tv_usec; + if (e->tv_usec < 0) { + e->tv_sec -=1; + e->tv_usec += 1000000; + } + printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, e->tv_usec); +} + +static PyObject * +profile_int(PyObject *self, PyObject* args) +{ + int i, k; + struct timeval start, stop; + PyObject *single, **multiple, *op1, *result; + + /* Test 1: Allocate and immediately deallocate + many small integers */ + gettimeofday(&start, NULL); + for(k=0; k < 20000; k++) + for(i=0; i < 1000; i++) { + single = PyInt_FromLong(i); + Py_DECREF(single); + } + gettimeofday(&stop, NULL); + print_delta(1, &start, &stop); + + /* Test 2: Allocate and immediately deallocate + many large integers */ + gettimeofday(&start, NULL); + for(k=0; k < 20000; k++) + for(i=0; i < 1000; i++) { + single = PyInt_FromLong(i+1000000); + Py_DECREF(single); + } + gettimeofday(&stop, NULL); + print_delta(2, &start, &stop); + + /* Test 3: Allocate a few integers, then release + them all simultaneously. */ + multiple = malloc(sizeof(PyObject*) * 1000); + gettimeofday(&start, NULL); + for(k=0; k < 20000; k++) { + for(i=0; i < 1000; i++) { + multiple[i] = PyInt_FromLong(i+1000000); + } + for(i=0; i < 1000; i++) { + Py_DECREF(multiple[i]); + } + } + gettimeofday(&stop, NULL); + print_delta(3, &start, &stop); + + /* Test 4: Allocate many integers, then release + them all simultaneously. */ + multiple = malloc(sizeof(PyObject*) * 1000000); + gettimeofday(&start, NULL); + for(k=0; k < 20; k++) { + for(i=0; i < 1000000; i++) { + multiple[i] = PyInt_FromLong(i+1000000); + } + for(i=0; i < 1000000; i++) { + Py_DECREF(multiple[i]); + } + } + gettimeofday(&stop, NULL); + print_delta(4, &start, &stop); + + /* Test 5: Allocate many integers < 32000 */ + multiple = malloc(sizeof(PyObject*) * 1000000); + gettimeofday(&start, NULL); + for(k=0; k < 10; k++) { + for(i=0; i < 1000000; i++) { + multiple[i] = PyInt_FromLong(i+1000); + } + for(i=0; i < 1000000; i++) { + Py_DECREF(multiple[i]); + } + } + gettimeofday(&stop, NULL); + print_delta(5, &start, &stop); + + /* Test 6: Perform small int addition */ + op1 = PyInt_FromLong(1); + gettimeofday(&start, NULL); + for(i=0; i < 10000000; i++) { + result = PyNumber_Add(op1, op1); + Py_DECREF(result); + } + gettimeofday(&stop, NULL); + Py_DECREF(op1); + print_delta(6, &start, &stop); + + /* Test 7: Perform medium int addition */ + op1 = PyInt_FromLong(1000); + gettimeofday(&start, NULL); + for(i=0; i < 10000000; i++) { + result = PyNumber_Add(op1, op1); + Py_DECREF(result); + } + gettimeofday(&stop, NULL); + Py_DECREF(op1); + print_delta(7, &start, &stop); + + Py_INCREF(Py_None); + return Py_None; +} +#endif + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, {"test_config", (PyCFunction)test_config, METH_NOARGS}, @@ -756,6 +869,9 @@ static PyMethodDef TestMethods[] = { #ifdef WITH_THREAD {"_test_thread_state", test_thread_state, METH_VARARGS}, #endif +#ifdef HAVE_GETTIMEOFDAY + {"profile_int", profile_int, METH_NOARGS}, +#endif {NULL, NULL} /* sentinel */ }; diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 4347383..7be7002 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -913,7 +913,7 @@ AsObj(PyObject *value) PyString_GET_SIZE(value)); else if (PyBool_Check(value)) return Tcl_NewBooleanObj(PyObject_IsTrue(value)); - else if (PyInt_Check(value)) + else if (PyInt_CheckExact(value)) return Tcl_NewLongObj(PyInt_AS_LONG(value)); else if (PyFloat_Check(value)) return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); diff --git a/Modules/cPickle.c b/Modules/cPickle.c index f3178ab..4c55428 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -711,7 +711,9 @@ get(Picklerobject *self, PyObject *id) PyErr_SetString(PicklingError, "no int where int expected in memo"); return -1; } - c_value = PyInt_AS_LONG((PyIntObject*)value); + c_value = PyInt_AsLong(value); + if (c_value == -1 && PyErr_Occurred()) + return -1; if (!self->bin) { s[0] = GET; @@ -958,7 +960,7 @@ save_bool(Picklerobject *self, PyObject *args) { static const char *buf[2] = {FALSE, TRUE}; static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; - long l = PyInt_AS_LONG((PyIntObject *)args); + long l = args == Py_True; if (self->proto >= 2) { char opcode = l ? NEWTRUE : NEWFALSE; @@ -971,10 +973,9 @@ save_bool(Picklerobject *self, PyObject *args) } static int -save_int(Picklerobject *self, PyObject *args) +save_int(Picklerobject *self, long l) { char c_str[32]; - long l = PyInt_AS_LONG((PyIntObject *)args); int len = 0; if (!self->bin @@ -1027,9 +1028,16 @@ save_long(Picklerobject *self, PyObject *args) Py_ssize_t size; int res = -1; PyObject *repr = NULL; - + int val = PyInt_AsLong(args); static char l = LONG; + if (val == -1 && PyErr_Occurred()) { + /* out of range for int pickling */ + PyErr_Clear(); + } + else + return save_int(self, val); + if (self->proto >= 2) { /* Linear-time pickling. */ size_t nbits; @@ -2183,13 +2191,6 @@ save(Picklerobject *self, PyObject *args, int pers_save) goto finally; } break; - case 'i': - if (type == &PyInt_Type) { - res = save_int(self, args); - goto finally; - } - break; - case 'l': if (type == &PyLong_Type) { res = save_long(self, args); @@ -2486,7 +2487,9 @@ Pickle_getvalue(Picklerobject *self, PyObject *args) rsize += PyString_GET_SIZE(k); else if (PyInt_Check(k)) { /* put */ - ik = PyInt_AS_LONG((PyIntObject*)k); + ik = PyInt_AsLong(k); + if (ik == -1 && PyErr_Occurred()) + goto err; if (ik >= lm || ik == 0) { PyErr_SetString(PicklingError, "Invalid get data"); @@ -2506,7 +2509,9 @@ Pickle_getvalue(Picklerobject *self, PyObject *args) } else { /* put */ - ik = PyInt_AS_LONG((PyIntObject *)k); + ik = PyInt_AsLong(k); + if (ik == -1 && PyErr_Occurred()) + goto err; if (ik >= lm || ik == 0) { PyErr_SetString(PicklingError, "Invalid get data"); @@ -2535,8 +2540,9 @@ Pickle_getvalue(Picklerobject *self, PyObject *args) } else if (PyTuple_Check(k)) { /* get */ - ik = PyInt_AS_LONG((PyIntObject *) - PyTuple_GET_ITEM(k, 0)); + ik = PyLong_AsLong(PyTuple_GET_ITEM(k, 0)); + if (ik == -1 && PyErr_Occurred()) + goto err; if (ik < 256) { *s++ = BINGET; *s++ = (int)(ik & 0xff); @@ -2551,7 +2557,9 @@ Pickle_getvalue(Picklerobject *self, PyObject *args) } else { /* put */ - ik = PyInt_AS_LONG((PyIntObject*)k); + ik = PyLong_AsLong(k); + if (ik == -1 && PyErr_Occurred()) + goto err; if (have_get[ik]) { /* with matching get */ if (ik < 256) { diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 7c6b989..42178ff 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -1314,6 +1314,9 @@ mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args) return NULL; } + if (size == -1 && PyErr_Occurred()) + return NULL; + return mbstreamreader_iread(self, "read", size); } @@ -1335,6 +1338,9 @@ mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args) return NULL; } + if (size == -1 && PyErr_Occurred()) + return NULL; + return mbstreamreader_iread(self, "readline", size); } @@ -1356,6 +1362,9 @@ mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args) return NULL; } + if (sizehint == -1 && PyErr_Occurred()) + return NULL; + r = mbstreamreader_iread(self, "read", sizehint); if (r == NULL) return NULL; diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 7ffa316..3757513 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -1844,10 +1844,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, * lose a little info. */ assert(PyInt_Check(factor) || PyLong_Check(factor)); - if (PyInt_Check(factor)) - dnum = (double)PyInt_AsLong(factor); - else - dnum = PyLong_AsDouble(factor); + dnum = PyLong_AsDouble(factor); dnum *= fracpart; fracpart = modf(dnum, &intpart); @@ -3800,7 +3797,7 @@ datetime_strptime(PyObject *cls, PyObject *args) Py_DECREF(obj); return NULL; } - if (PyInt_Check(p)) + if (PyInt_CheckExact(p)) ia[i] = PyInt_AsLong(p); else good_timetuple = 0; diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c index 5622ed9..4af7e30 100644 --- a/Modules/dlmodule.c +++ b/Modules/dlmodule.c @@ -107,9 +107,11 @@ dl_call(dlobject *xp, PyObject *args) } for (i = 1; i < n; i++) { PyObject *v = PyTuple_GetItem(args, i); - if (PyInt_Check(v)) + if (PyInt_Check(v)) { alist[i-1] = PyInt_AsLong(v); - else if (PyString_Check(v)) + if (alist[i-1] == -1 && PyErr_Occurred()) + return NULL; + } else if (PyString_Check(v)) alist[i-1] = (long)PyString_AsString(v); else if (v == Py_None) alist[i-1] = (long) ((char *)NULL); diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 3260c3d..f8df0c4 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -5437,32 +5437,21 @@ posix_setgroups(PyObject *self, PyObject *groups) elem = PySequence_GetItem(groups, i); if (!elem) return NULL; - if (!PyInt_Check(elem)) { - if (!PyLong_Check(elem)) { - PyErr_SetString(PyExc_TypeError, - "groups must be integers"); + if (!PyLong_Check(elem)) { + PyErr_SetString(PyExc_TypeError, + "groups must be integers"); + Py_DECREF(elem); + return NULL; + } else { + unsigned long x = PyLong_AsUnsignedLong(elem); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "group id too big"); Py_DECREF(elem); return NULL; - } else { - unsigned long x = PyLong_AsUnsignedLong(elem); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } - grouplist[i] = x; - /* read back the value to see if it fitted in gid_t */ - if (grouplist[i] != x) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } } - } else { - long x = PyInt_AsLong(elem); grouplist[i] = x; + /* read back the value to see if it fitted in gid_t */ if (grouplist[i] != x) { PyErr_SetString(PyExc_TypeError, "group id too big"); diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 8ec0ed7..31efa0a 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3483,12 +3483,7 @@ socket_ntohl(PyObject *self, PyObject *arg) { unsigned long x; - if (PyInt_Check(arg)) { - x = PyInt_AS_LONG(arg); - if (x == (unsigned long) -1 && PyErr_Occurred()) - return NULL; - } - else if (PyLong_Check(arg)) { + if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; @@ -3542,12 +3537,7 @@ socket_htonl(PyObject *self, PyObject *arg) { unsigned long x; - if (PyInt_Check(arg)) { - x = PyInt_AS_LONG(arg); - if (x == (unsigned long) -1 && PyErr_Occurred()) - return NULL; - } - else if (PyLong_Check(arg)) { + if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; @@ -3827,7 +3817,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args) "getaddrinfo() argument 1 must be string or None"); return NULL; } - if (PyInt_Check(pobj)) { + if (PyInt_CheckExact(pobj)) { PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); pptr = pbuf; } else if (PyString_Check(pobj)) { diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 9ab2724..13c0535 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -357,7 +357,7 @@ gettmarg(PyObject *args, struct tm *p) if (y < 1900) { PyObject *accept = PyDict_GetItemString(moddict, "accept2dyear"); - if (accept == NULL || !PyInt_Check(accept) || + if (accept == NULL || !PyInt_CheckExact(accept) || PyInt_AsLong(accept) == 0) { PyErr_SetString(PyExc_ValueError, "year >= 1900 required"); diff --git a/Objects/abstract.c b/Objects/abstract.c index 03883be..8405782 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -790,25 +790,6 @@ PyNumber_Absolute(PyObject *o) return type_error("bad operand type for abs(): '%.200s'", o); } -/* Add a check for embedded NULL-bytes in the argument. */ -static PyObject * -int_from_string(const char *s, Py_ssize_t len) -{ - char *end; - PyObject *x; - - x = PyInt_FromString((char*)s, &end, 10); - if (x == NULL) - return NULL; - if (end != s + len) { - PyErr_SetString(PyExc_ValueError, - "null byte in argument for int()"); - Py_DECREF(x); - return NULL; - } - return x; -} - /* Return a Python Int or Long from the object item Raise TypeError if the result is not an int-or-long or if the object cannot be interpreted as an index. @@ -828,7 +809,7 @@ PyNumber_Index(PyObject *item) if (result && !PyInt_Check(result) && !PyLong_Check(result)) { PyErr_Format(PyExc_TypeError, - "__index__ returned non-(int,long) " \ + "__index__ returned non-int " \ "(type %.200s)", result->ob_type->tp_name); Py_DECREF(result); @@ -890,51 +871,6 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err) } -PyObject * -PyNumber_Int(PyObject *o) -{ - PyNumberMethods *m; - const char *buffer; - Py_ssize_t buffer_len; - - if (o == NULL) - return null_error(); - if (PyInt_CheckExact(o)) { - Py_INCREF(o); - return o; - } - m = o->ob_type->tp_as_number; - if (m && m->nb_int) { /* This should include subclasses of int */ - PyObject *res = m->nb_int(o); - if (res && (!PyInt_Check(res) && !PyLong_Check(res))) { - PyErr_Format(PyExc_TypeError, - "__int__ returned non-int (type %.200s)", - res->ob_type->tp_name); - Py_DECREF(res); - return NULL; - } - return res; - } - if (PyInt_Check(o)) { /* A int subclass without nb_int */ - PyIntObject *io = (PyIntObject*)o; - return PyInt_FromLong(io->ob_ival); - } - if (PyString_Check(o)) - return int_from_string(PyString_AS_STRING(o), - PyString_GET_SIZE(o)); -#ifdef Py_USING_UNICODE - if (PyUnicode_Check(o)) - return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o), - PyUnicode_GET_SIZE(o), - 10); -#endif - if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) - return int_from_string((char*)buffer, buffer_len); - - return type_error("int() argument must be a string or a " - "number, not '%.200s'", o); -} - /* Add a check for embedded NULL-bytes in the argument. */ static PyObject * long_from_string(const char *s, Py_ssize_t len) @@ -947,7 +883,7 @@ long_from_string(const char *s, Py_ssize_t len) return NULL; if (end != s + len) { PyErr_SetString(PyExc_ValueError, - "null byte in argument for long()"); + "null byte in argument for int()"); Py_DECREF(x); return NULL; } @@ -963,7 +899,22 @@ PyNumber_Long(PyObject *o) if (o == NULL) return null_error(); + if (PyLong_CheckExact(o)) { + Py_INCREF(o); + return o; + } m = o->ob_type->tp_as_number; + if (m && m->nb_int) { /* This should include subclasses of int */ + PyObject *res = m->nb_int(o); + if (res && !PyLong_Check(res)) { + PyErr_Format(PyExc_TypeError, + "__int__ returned non-int (type %.200s)", + res->ob_type->tp_name); + Py_DECREF(res); + return NULL; + } + return res; + } if (m && m->nb_long) { /* This should include subclasses of long */ PyObject *res = m->nb_long(o); if (res && (!PyInt_Check(res) && !PyLong_Check(res))) { @@ -994,7 +945,7 @@ PyNumber_Long(PyObject *o) if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return long_from_string(buffer, buffer_len); - return type_error("long() argument must be a string or a " + return type_error("int() argument must be a string or a " "number, not '%.200s'", o); } diff --git a/Objects/boolobject.c b/Objects/boolobject.c index cd19481..f98af9f 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -1,13 +1,14 @@ /* Boolean type, a subtype of int */ #include "Python.h" +#include "longintrepr.h" /* We need to define bool_print to override int_print */ static int -bool_print(PyBoolObject *self, FILE *fp, int flags) +bool_print(PyObject *self, FILE *fp, int flags) { - fputs(self->ob_ival == 0 ? "False" : "True", fp); + fputs(self == Py_False ? "False" : "True", fp); return 0; } @@ -17,11 +18,11 @@ static PyObject *false_str = NULL; static PyObject *true_str = NULL; static PyObject * -bool_repr(PyBoolObject *self) +bool_repr(PyObject *self) { PyObject *s; - if (self->ob_ival) + if (self == Py_True) s = true_str ? true_str : (true_str = PyString_InternFromString("True")); else @@ -68,27 +69,24 @@ static PyObject * bool_and(PyObject *a, PyObject *b) { if (!PyBool_Check(a) || !PyBool_Check(b)) - return PyInt_Type.tp_as_number->nb_and(a, b); - return PyBool_FromLong( - ((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival); + return PyLong_Type.tp_as_number->nb_and(a, b); + return PyBool_FromLong((a == Py_True) & (b == Py_True)); } static PyObject * bool_or(PyObject *a, PyObject *b) { if (!PyBool_Check(a) || !PyBool_Check(b)) - return PyInt_Type.tp_as_number->nb_or(a, b); - return PyBool_FromLong( - ((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival); + return PyLong_Type.tp_as_number->nb_or(a, b); + return PyBool_FromLong((a == Py_True) | (b == Py_True)); } static PyObject * bool_xor(PyObject *a, PyObject *b) { if (!PyBool_Check(a) || !PyBool_Check(b)) - return PyInt_Type.tp_as_number->nb_xor(a, b); - return PyBool_FromLong( - ((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival); + return PyLong_Type.tp_as_number->nb_xor(a, b); + return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); } /* Doc string */ @@ -139,6 +137,7 @@ static PyNumberMethods bool_as_number = { 0, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ + 0, /* nb_index */ }; /* The type object for bool. Note that this cannot be subclassed! */ @@ -147,20 +146,20 @@ PyTypeObject PyBool_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, "bool", - sizeof(PyIntObject), + sizeof(struct _longobject), 0, 0, /* tp_dealloc */ - (printfunc)bool_print, /* tp_print */ + bool_print, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - (reprfunc)bool_repr, /* tp_repr */ + bool_repr, /* tp_repr */ &bool_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ - (reprfunc)bool_repr, /* tp_str */ + bool_repr, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ @@ -175,7 +174,7 @@ PyTypeObject PyBool_Type = { 0, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ - &PyInt_Type, /* tp_base */ + &PyLong_Type, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ @@ -188,12 +187,12 @@ PyTypeObject PyBool_Type = { /* The objects representing bool values False and True */ /* Named Zero for link-level compatibility */ -PyIntObject _Py_ZeroStruct = { +struct _longobject _Py_FalseStruct = { PyObject_HEAD_INIT(&PyBool_Type) - 0 + 0, { 0 } }; -PyIntObject _Py_TrueStruct = { +struct _longobject _Py_TrueStruct = { PyObject_HEAD_INIT(&PyBool_Type) - 1 + 1, { 1 } }; diff --git a/Objects/complexobject.c b/Objects/complexobject.c index da47f81..8e1f4d7 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -349,10 +349,6 @@ to_complex(PyObject **pobj, Py_complex *pc) PyObject *obj = *pobj; pc->real = pc->imag = 0.0; - if (PyInt_Check(obj)) { - pc->real = PyInt_AS_LONG(obj); - return 0; - } if (PyLong_Check(obj)) { pc->real = PyLong_AsDouble(obj); if (pc->real == -1.0 && PyErr_Occurred()) { diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 0cd819c..9e298f0 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1089,7 +1089,7 @@ SyntaxError_str(PySyntaxErrorObject *self) have_filename = (self->filename != NULL) && PyString_Check(self->filename); - have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno); + have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno); if (!have_filename && !have_lineno) return str; @@ -1225,10 +1225,8 @@ get_int(PyObject *attr, Py_ssize_t *value, const char *name) return -1; } - if (PyInt_Check(attr)) { - *value = PyInt_AS_LONG(attr); - } else if (PyLong_Check(attr)) { - *value = _PyLong_AsSsize_t(attr); + if (PyLong_Check(attr)) { + *value = PyLong_AsSsize_t(attr); if (*value == -1 && PyErr_Occurred()) return -1; } else { @@ -1515,8 +1513,8 @@ UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds, if (!PyArg_ParseTuple(args, "O!O!O!O!O!", &PyString_Type, &self->encoding, objecttype, &self->object, - &PyInt_Type, &self->start, - &PyInt_Type, &self->end, + &PyLong_Type, &self->start, + &PyLong_Type, &self->end, &PyString_Type, &self->reason)) { self->encoding = self->object = self->start = self->end = self->reason = NULL; @@ -1748,8 +1746,8 @@ UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args, if (!PyArg_ParseTuple(args, "O!O!O!O!", &PyUnicode_Type, &self->object, - &PyInt_Type, &self->start, - &PyInt_Type, &self->end, + &PyLong_Type, &self->start, + &PyLong_Type, &self->end, &PyString_Type, &self->reason)) { self->object = self->start = self->end = self->reason = NULL; return -1; diff --git a/Objects/fileobject.c b/Objects/fileobject.c index f2aeb9d..8ebb482 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -2136,7 +2136,7 @@ PyFile_SoftSpace(PyObject *f, int newflag) if (v == NULL) PyErr_Clear(); else { - if (PyInt_Check(v)) + if (PyInt_CheckExact(v)) oldflag = PyInt_AsLong(v); assert(oldflag < INT_MAX); Py_DECREF(v); @@ -2301,6 +2301,8 @@ int PyObject_AsFileDescriptor(PyObject *o) return -1; } + if (fd == -1 && PyErr_Occurred()) + return -1; if (fd < 0) { PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 9c6dadf..514dd39 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -273,10 +273,7 @@ convert_to_double(PyObject **v, double *dbl) { register PyObject *obj = *v; - if (PyInt_Check(obj)) { - *dbl = (double)PyInt_AS_LONG(obj); - } - else if (PyLong_Check(obj)) { + if (PyLong_Check(obj)) { *dbl = PyLong_AsDouble(obj); if (*dbl == -1.0 && PyErr_Occurred()) { *v = NULL; @@ -376,32 +373,6 @@ float_richcompare(PyObject *v, PyObject *w, int op) goto Unimplemented; } - else if (PyInt_Check(w)) { - long jj = PyInt_AS_LONG(w); - /* In the worst realistic case I can imagine, C double is a - * Cray single with 48 bits of precision, and long has 64 - * bits. - */ -#if SIZEOF_LONG > 6 - unsigned long abs = (unsigned long)(jj < 0 ? -jj : jj); - if (abs >> 48) { - /* Needs more than 48 bits. Make it take the - * PyLong path. - */ - PyObject *result; - PyObject *ww = PyLong_FromLong(jj); - - if (ww == NULL) - return NULL; - result = float_richcompare(v, ww, op); - Py_DECREF(ww); - return result; - } -#endif - j = (double)jj; - assert((long)j == jj); - } - else if (PyLong_Check(w)) { int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1; int wsign = _PyLong_Sign(w); diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 3a073b6..83dacfd5 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -88,7 +88,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno) int setup_op = 0; /* (ditto) */ /* f_lineno must be an integer. */ - if (!PyInt_Check(p_new_lineno)) { + if (!PyInt_CheckExact(p_new_lineno)) { PyErr_SetString(PyExc_ValueError, "lineno must be an integer"); return -1; diff --git a/Objects/intobject.c b/Objects/intobject.c index 0b746de..256824c 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -10,6 +10,7 @@ PyInt_GetMax(void) return LONG_MAX; /* To initialize sys.maxint */ } +#if 0 /* 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.) @@ -1254,3 +1255,4 @@ PyInt_Fini(void) } } } +#endif /* if 0 */ diff --git a/Objects/listobject.c b/Objects/listobject.c index 4c9c4c3..3108ab4 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -945,7 +945,7 @@ islt(PyObject *x, PyObject *y, PyObject *compare) Py_DECREF(args); if (res == NULL) return -1; - if (!PyInt_Check(res)) { + if (!PyInt_CheckExact(res)) { PyErr_Format(PyExc_TypeError, "comparison function must return int, not %.200s", res->ob_type->tp_name); diff --git a/Objects/longobject.c b/Objects/longobject.c index f68656b..03d22f5 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -7,6 +7,53 @@ #include +#ifndef NSMALLPOSINTS +#define NSMALLPOSINTS 257 +#endif +#ifndef NSMALLNEGINTS +#define NSMALLNEGINTS 5 +#endif +#if NSMALLNEGINTS + NSMALLPOSINTS > 0 +/* Small integers are preallocated in this array so that they + can be shared. + The integers that are preallocated are those in the range + -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). +*/ +static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; +#ifdef COUNT_ALLOCS +int quick_int_allocs, quick_neg_int_allocs; +#endif + +static inline PyObject * +get_small_int(int ival) +{ + PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); + Py_INCREF(v); +#ifdef COUNT_ALLOCS + if (ival >= 0) + quick_int_allocs++; + else + quick_neg_int_allocs++; +#endif + return v; +} +#define CHECK_SMALL_INT(ival) \ + do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ + return get_small_int(ival); \ + } while(0) + +#else +#define CHECK_SMALL_INT(ival) +#endif + +#define MEDIUM_VALUE(x) ((x)->ob_size < 0 ? -(x)->ob_digit[0] : ((x)->ob_size == 0 ? 0 : (x)->ob_digit[0])) +/* If a freshly-allocated long is already shared, it must + be a small integer, so negating it must go to PyLong_FromLong */ +#define NEGATE(x) \ + do if ((x)->ob_refcnt == 1) (x)->ob_size = -(x)->ob_size; \ + else { PyObject* tmp=PyInt_FromLong(-MEDIUM_VALUE(x)); \ + Py_DECREF(x); (x) = (PyLongObject*)tmp; } \ + while(0) /* For long multiplication, use the O(N**2) school algorithm unless * both operands contain more than KARATSUBA_CUTOFF digits (this * being an internal Python long digit, in base BASE). @@ -64,11 +111,21 @@ long_normalize(register PyLongObject *v) PyLongObject * _PyLong_New(Py_ssize_t size) { - if (size > PY_SSIZE_T_MAX) { + PyLongObject *result; + /* Can't use sizeof(PyLongObject) here, since the + compiler takes padding at the end into account. + As the consequence, this would waste 2 bytes on + a 32-bit system, and 6 bytes on a 64-bit system. + This computation would be incorrect on systems + which have padding before the digits; with 16-bit + digits this should not happen. */ + result = PyObject_MALLOC(sizeof(PyVarObject) + + size*sizeof(digit)); + if (!result) { PyErr_NoMemory(); return NULL; } - return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); + return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); } PyObject * @@ -81,6 +138,12 @@ _PyLong_Copy(PyLongObject *src) i = src->ob_size; if (i < 0) i = -(i); + if (i < 2) { + int ival = src->ob_digit[0]; + if (src->ob_size < 0) + ival = -ival; + CHECK_SMALL_INT(ival); + } result = _PyLong_New(i); if (result != NULL) { result->ob_size = src->ob_size; @@ -98,17 +161,37 @@ PyLong_FromLong(long ival) PyLongObject *v; unsigned long t; /* unsigned so >> doesn't propagate sign bit */ int ndigits = 0; - int negative = 0; + int sign = 1; + + CHECK_SMALL_INT(ival); if (ival < 0) { ival = -ival; - negative = 1; + sign = -1; } - /* Count the number of Python digits. - We used to pick 5 ("big enough for anything"), but that's a - waste of time and space given that 5*15 = 75 bits are rarely - needed. */ + /* Fast path for single-digits ints */ + if (!(ival>>SHIFT)) { + v = _PyLong_New(1); + if (v) { + v->ob_size = sign; + v->ob_digit[0] = ival; + } + return (PyObject*)v; + } + + /* 2 digits */ + if (!(ival >> 2*SHIFT)) { + v = _PyLong_New(2); + if (v) { + v->ob_size = 2*sign; + v->ob_digit[0] = (digit)ival & MASK; + v->ob_digit[1] = ival >> SHIFT; + } + return (PyObject*)v; + } + + /* Larger numbers: loop to determine number of digits */ t = (unsigned long)ival; while (t) { ++ndigits; @@ -117,7 +200,7 @@ PyLong_FromLong(long ival) v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; - v->ob_size = negative ? -ndigits : ndigits; + v->ob_size = ndigits*sign; t = (unsigned long)ival; while (t) { *p++ = (digit)(t & MASK); @@ -136,6 +219,8 @@ PyLong_FromUnsignedLong(unsigned long ival) unsigned long t; int ndigits = 0; + if (ival < BASE) + return PyLong_FromLong(ival); /* Count the number of Python digits. */ t = (unsigned long)ival; while (t) { @@ -165,9 +250,10 @@ PyLong_FromDouble(double dval) neg = 0; if (Py_IS_INFINITY(dval)) { PyErr_SetString(PyExc_OverflowError, - "cannot convert float infinity to long"); + "cannot convert float infinity to int"); return NULL; } + CHECK_SMALL_INT((int)dval); if (dval < 0.0) { neg = 1; dval = -dval; @@ -214,15 +300,39 @@ PyLong_AsLong(PyObject *vv) unsigned long x, prev; Py_ssize_t i; int sign; + int do_decref = 0; /* if nb_int was called */ - if (vv == NULL || !PyLong_Check(vv)) { - if (vv != NULL && PyInt_Check(vv)) - return PyInt_AsLong(vv); + if (vv == NULL) { PyErr_BadInternalCall(); return -1; } + + if (!PyLong_Check(vv)) { + PyNumberMethods *nb; + if ((nb = vv->ob_type->tp_as_number) == NULL || + nb->nb_int == NULL) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + vv = (*nb->nb_int) (vv); + if (vv == NULL) + return -1; + do_decref = 1; + if (!PyLong_Check(vv)) { + Py_DECREF(vv); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return -1; + } + } + v = (PyLongObject *)vv; i = v->ob_size; + switch (i) { + case -1: return -v->ob_digit[0]; + case 0: return 0; + case 1: return v->ob_digit[0]; + } sign = 1; x = 0; if (i < 0) { @@ -235,6 +345,9 @@ PyLong_AsLong(PyObject *vv) if ((x >> SHIFT) != prev) goto overflow; } + if (do_decref) { + Py_DECREF(vv); + } /* Haven't lost any bits, but casting to long requires extra care * (see comment above). */ @@ -247,16 +360,32 @@ PyLong_AsLong(PyObject *vv) /* else overflow */ overflow: + if (do_decref) { + Py_DECREF(vv); + } PyErr_SetString(PyExc_OverflowError, - "long int too large to convert to int"); + "int too large to convert to int"); return -1; } +int +_PyLong_FitsInLong(PyObject *vv) +{ + int size; + if (!PyLong_CheckExact(vv)) { + PyErr_BadInternalCall(); + return 0; + } + /* conservative estimate */ + size = ((PyLongObject*)vv)->ob_size; + return -2 <= size && size <= 2; +} + /* Get a Py_ssize_t from a long int object. Returns -1 and sets an error condition if overflow occurs. */ Py_ssize_t -_PyLong_AsSsize_t(PyObject *vv) { +PyLong_AsSsize_t(PyObject *vv) { register PyLongObject *v; size_t x, prev; Py_ssize_t i; @@ -268,6 +397,11 @@ _PyLong_AsSsize_t(PyObject *vv) { } v = (PyLongObject *)vv; i = v->ob_size; + switch (i) { + case -1: return -v->ob_digit[0]; + case 0: return 0; + case 1: return v->ob_digit[0]; + } sign = 1; x = 0; if (i < 0) { @@ -293,7 +427,7 @@ _PyLong_AsSsize_t(PyObject *vv) { overflow: PyErr_SetString(PyExc_OverflowError, - "long int too large to convert to int"); + "int too large to convert to "); return -1; } @@ -308,15 +442,6 @@ PyLong_AsUnsignedLong(PyObject *vv) Py_ssize_t i; if (vv == NULL || !PyLong_Check(vv)) { - if (vv != NULL && PyInt_Check(vv)) { - long val = PyInt_AsLong(vv); - if (val < 0) { - PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to unsigned long"); - return (unsigned long) -1; - } - return val; - } PyErr_BadInternalCall(); return (unsigned long) -1; } @@ -325,15 +450,57 @@ PyLong_AsUnsignedLong(PyObject *vv) x = 0; if (i < 0) { PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to unsigned long"); + "can't convert negative value to unsigned int"); return (unsigned long) -1; } + switch (i) { + case 0: return 0; + case 1: return v->ob_digit[0]; + } + while (--i >= 0) { + prev = x; + x = (x << SHIFT) + v->ob_digit[i]; + if ((x >> SHIFT) != prev) { + PyErr_SetString(PyExc_OverflowError, + "int too large to convert"); + return (unsigned long) -1; + } + } + return x; +} + +/* Get a C unsigned long int from a long int object. + Returns -1 and sets an error condition if overflow occurs. */ + +size_t +PyLong_AsSize_t(PyObject *vv) +{ + register PyLongObject *v; + size_t x, prev; + Py_ssize_t i; + + if (vv == NULL || !PyLong_Check(vv)) { + PyErr_BadInternalCall(); + return (unsigned long) -1; + } + v = (PyLongObject *)vv; + i = v->ob_size; + x = 0; + if (i < 0) { + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to size_t"); + return (size_t) -1; + } + switch (i) { + case 0: return 0; + case 1: return v->ob_digit[0]; + } while (--i >= 0) { prev = x; x = (x << SHIFT) + v->ob_digit[i]; if ((x >> SHIFT) != prev) { PyErr_SetString(PyExc_OverflowError, - "long int too large to convert"); + "int too large to convert"); return (unsigned long) -1; } } @@ -343,8 +510,8 @@ PyLong_AsUnsignedLong(PyObject *vv) /* Get a C unsigned long int from a long int object, ignoring the high bits. Returns -1 and sets an error condition if an error occurs. */ -unsigned long -PyLong_AsUnsignedLongMask(PyObject *vv) +static unsigned long +_PyLong_AsUnsignedLongMask(PyObject *vv) { register PyLongObject *v; unsigned long x; @@ -352,13 +519,15 @@ PyLong_AsUnsignedLongMask(PyObject *vv) int sign; if (vv == NULL || !PyLong_Check(vv)) { - if (vv != NULL && PyInt_Check(vv)) - return PyInt_AsUnsignedLongMask(vv); PyErr_BadInternalCall(); return (unsigned long) -1; } v = (PyLongObject *)vv; i = v->ob_size; + switch (i) { + case 0: return 0; + case 1: return v->ob_digit[0]; + } sign = 1; x = 0; if (i < 0) { @@ -371,6 +540,41 @@ PyLong_AsUnsignedLongMask(PyObject *vv) return x * sign; } +unsigned long +PyLong_AsUnsignedLongMask(register PyObject *op) +{ + PyNumberMethods *nb; + PyLongObject *lo; + unsigned long val; + + if (op && PyLong_Check(op)) + return _PyLong_AsUnsignedLongMask(op); + + if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || + nb->nb_int == NULL) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (unsigned long)-1; + } + + lo = (PyLongObject*) (*nb->nb_int) (op); + if (lo == NULL) + return (unsigned long)-1; + if (PyLong_Check(lo)) { + val = _PyLong_AsUnsignedLongMask((PyObject *)lo); + Py_DECREF(lo); + if (PyErr_Occurred()) + return (unsigned long)-1; + return val; + } + else + { + Py_DECREF(lo); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return (unsigned long)-1; + } +} + int _PyLong_Sign(PyObject *vv) { @@ -409,7 +613,7 @@ _PyLong_NumBits(PyObject *vv) return result; Overflow: - PyErr_SetString(PyExc_OverflowError, "long has too many bits " + PyErr_SetString(PyExc_OverflowError, "int has too many bits " "to express in a platform size_t"); return (size_t)-1; } @@ -542,7 +746,7 @@ _PyLong_AsByteArray(PyLongObject* v, ndigits = -(v->ob_size); if (!is_signed) { PyErr_SetString(PyExc_TypeError, - "can't convert negative long to unsigned"); + "can't convert negative int to unsigned"); return -1; } do_twos_comp = 1; @@ -653,7 +857,7 @@ _PyLong_AsByteArray(PyLongObject* v, return 0; Overflow: - PyErr_SetString(PyExc_OverflowError, "long too big to convert"); + PyErr_SetString(PyExc_OverflowError, "int too big to convert"); return -1; } @@ -739,7 +943,7 @@ PyLong_AsDouble(PyObject *vv) overflow: PyErr_SetString(PyExc_OverflowError, - "long int too large to convert to float"); + "int too large to convert to float"); return -1.0; } @@ -748,24 +952,17 @@ overflow: PyObject * PyLong_FromVoidPtr(void *p) { -#if SIZEOF_VOID_P <= SIZEOF_LONG - if ((long)p < 0) - return PyLong_FromUnsignedLong((unsigned long)p); - return PyInt_FromLong((long)p); -#else - #ifndef HAVE_LONG_LONG # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" #endif #if SIZEOF_LONG_LONG < SIZEOF_VOID_P # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" #endif - /* optimize null pointers */ - if (p == NULL) + /* special-case null pointer */ + if (!p) return PyInt_FromLong(0); - return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p); + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); -#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ } /* Get a C pointer from a long object (or an int object in some cases) */ @@ -780,9 +977,7 @@ PyLong_AsVoidPtr(PyObject *vv) #if SIZEOF_VOID_P <= SIZEOF_LONG long x; - if (PyInt_Check(vv)) - x = PyInt_AS_LONG(vv); - else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) + if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) x = PyLong_AsLong(vv); else x = PyLong_AsUnsignedLong(vv); @@ -796,9 +991,7 @@ PyLong_AsVoidPtr(PyObject *vv) #endif PY_LONG_LONG x; - if (PyInt_Check(vv)) - x = PyInt_AS_LONG(vv); - else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) + if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) x = PyLong_AsLongLong(vv); else x = PyLong_AsUnsignedLongLong(vv); @@ -828,6 +1021,7 @@ PyLong_FromLongLong(PY_LONG_LONG ival) int ndigits = 0; int negative = 0; + CHECK_SMALL_INT(ival); if (ival < 0) { ival = -ival; negative = 1; @@ -864,6 +1058,8 @@ PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) unsigned PY_LONG_LONG t; int ndigits = 0; + if (ival < BASE) + return PyLong_FromLong(ival); /* Count the number of Python digits. */ t = (unsigned PY_LONG_LONG)ival; while (t) { @@ -885,22 +1081,26 @@ PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) /* Create a new long int object from a C Py_ssize_t. */ PyObject * -_PyLong_FromSsize_t(Py_ssize_t ival) +PyLong_FromSsize_t(Py_ssize_t ival) { Py_ssize_t bytes = ival; int one = 1; + if (ival < BASE) + return PyLong_FromLong(ival); return _PyLong_FromByteArray( (unsigned char *)&bytes, - SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0); + SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1); } /* Create a new long int object from a C size_t. */ PyObject * -_PyLong_FromSize_t(size_t ival) +PyLong_FromSize_t(size_t ival) { size_t bytes = ival; int one = 1; + if (ival < BASE) + return PyLong_FromLong(ival); return _PyLong_FromByteArray( (unsigned char *)&bytes, SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0); @@ -912,6 +1112,7 @@ _PyLong_FromSize_t(size_t ival) PY_LONG_LONG PyLong_AsLongLong(PyObject *vv) { + PyLongObject *v; PY_LONG_LONG bytes; int one = 1; int res; @@ -923,8 +1124,6 @@ PyLong_AsLongLong(PyObject *vv) if (!PyLong_Check(vv)) { PyNumberMethods *nb; PyObject *io; - if (PyInt_Check(vv)) - return (PY_LONG_LONG)PyInt_AsLong(vv); if ((nb = vv->ob_type->tp_as_number) == NULL || nb->nb_int == NULL) { PyErr_SetString(PyExc_TypeError, "an integer is required"); @@ -933,11 +1132,6 @@ PyLong_AsLongLong(PyObject *vv) io = (*nb->nb_int) (vv); if (io == NULL) return -1; - if (PyInt_Check(io)) { - bytes = PyInt_AsLong(io); - Py_DECREF(io); - return bytes; - } if (PyLong_Check(io)) { bytes = PyLong_AsLongLong(io); Py_DECREF(io); @@ -948,6 +1142,12 @@ PyLong_AsLongLong(PyObject *vv) return -1; } + v = (PyLongObject*)vv; + switch(v->ob_size) { + case -1: return -v->ob_digit[0]; + case 0: return 0; + case 1: return v->ob_digit[0]; + } res = _PyLong_AsByteArray( (PyLongObject *)vv, (unsigned char *)&bytes, SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); @@ -965,6 +1165,7 @@ PyLong_AsLongLong(PyObject *vv) unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *vv) { + PyLongObject *v; unsigned PY_LONG_LONG bytes; int one = 1; int res; @@ -974,6 +1175,12 @@ PyLong_AsUnsignedLongLong(PyObject *vv) return (unsigned PY_LONG_LONG)-1; } + v = (PyLongObject*)vv; + switch(v->ob_size) { + case 0: return 0; + case 1: return v->ob_digit[0]; + } + res = _PyLong_AsByteArray( (PyLongObject *)vv, (unsigned char *)&bytes, SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); @@ -988,8 +1195,8 @@ PyLong_AsUnsignedLongLong(PyObject *vv) /* Get a C unsigned long int from a long int object, ignoring the high bits. Returns -1 and sets an error condition if an error occurs. */ -unsigned PY_LONG_LONG -PyLong_AsUnsignedLongLongMask(PyObject *vv) +static unsigned PY_LONG_LONG +_PyLong_AsUnsignedLongLongMask(PyObject *vv) { register PyLongObject *v; unsigned PY_LONG_LONG x; @@ -1001,6 +1208,10 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv) return (unsigned long) -1; } v = (PyLongObject *)vv; + switch(v->ob_size) { + case 0: return 0; + case 1: return v->ob_digit[0]; + } i = v->ob_size; sign = 1; x = 0; @@ -1013,6 +1224,41 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv) } return x * sign; } + +unsigned PY_LONG_LONG +PyLong_AsUnsignedLongLongMask(register PyObject *op) +{ + PyNumberMethods *nb; + PyLongObject *lo; + unsigned PY_LONG_LONG val; + + if (op && PyLong_Check(op)) + return _PyLong_AsUnsignedLongLongMask(op); + + if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || + nb->nb_int == NULL) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (unsigned PY_LONG_LONG)-1; + } + + lo = (PyLongObject*) (*nb->nb_int) (op); + if (lo == NULL) + return (unsigned PY_LONG_LONG)-1; + if (PyLong_Check(lo)) { + val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); + Py_DECREF(lo); + if (PyErr_Occurred()) + return (unsigned PY_LONG_LONG)-1; + return val; + } + else + { + Py_DECREF(lo); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return (unsigned PY_LONG_LONG)-1; + } +} #undef IS_LITTLE_ENDIAN #endif /* HAVE_LONG_LONG */ @@ -1024,9 +1270,6 @@ convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { *a = (PyLongObject *) v; Py_INCREF(v); } - else if (PyInt_Check(v)) { - *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v)); - } else { return 0; } @@ -1034,9 +1277,6 @@ convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { *b = (PyLongObject *) w; Py_INCREF(w); } - else if (PyInt_Check(w)) { - *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w)); - } else { Py_DECREF(*a); return 0; @@ -1206,7 +1446,7 @@ long_format(PyObject *aa, int base) sz = i + j / bits; if (j / SHIFT < size_a || sz < i) { PyErr_SetString(PyExc_OverflowError, - "long is too large to format"); + "int is too large to format"); return NULL; } str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz); @@ -1386,7 +1626,7 @@ long_from_binary_base(char **str, int base) n = (p - start) * bits_per_char + SHIFT - 1; if (n / bits_per_char < p - start) { PyErr_SetString(PyExc_ValueError, - "long string too large to convert"); + "int string too large to convert"); return NULL; } n = n / SHIFT; @@ -1433,7 +1673,7 @@ PyLong_FromString(char *str, char **pend, int base) if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, - "long() arg 2 must be >= 2 and <= 36"); + "int() arg 2 must be >= 2 and <= 36"); return NULL; } while (*str != '\0' && isspace(Py_CHARMASK(*str))) @@ -1683,7 +1923,7 @@ digit beyond the first. if (strrepr == NULL) return NULL; PyErr_Format(PyExc_ValueError, - "invalid literal for long() with base %d: %s", + "invalid literal for int() with base %d: %s", base, PyString_AS_STRING(strrepr)); Py_DECREF(strrepr); return NULL; @@ -1727,14 +1967,14 @@ long_divrem(PyLongObject *a, PyLongObject *b, if (size_b == 0) { PyErr_SetString(PyExc_ZeroDivisionError, - "long division or modulo by zero"); + "integer division or modulo by zero"); return -1; } if (size_a < size_b || (size_a == size_b && a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { /* |a| < |b|. */ - *pdiv = _PyLong_New(0); + *pdiv = (PyLongObject*)PyLong_FromLong(0); Py_INCREF(a); *prem = (PyLongObject *) a; return 0; @@ -1756,9 +1996,9 @@ long_divrem(PyLongObject *a, PyLongObject *b, the remainder r has the sign of a, so a = b*z + r. */ if ((a->ob_size < 0) != (b->ob_size < 0)) - z->ob_size = -(z->ob_size); + NEGATE(z); if (a->ob_size < 0 && (*prem)->ob_size != 0) - (*prem)->ob_size = -((*prem)->ob_size); + NEGATE(*prem); *pdiv = z; return 0; } @@ -1922,6 +2162,11 @@ long_hash(PyLongObject *v) same value hash to the same value, otherwise comparisons of mapping keys will turn out weird */ i = v->ob_size; + switch(i) { + case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0]; + case 0: return 0; + case 1: return v->ob_digit[0]; + } sign = 1; x = 0; if (i < 0) { @@ -2027,7 +2272,7 @@ x_sub(PyLongObject *a, PyLongObject *b) } assert(borrow == 0); if (sign < 0) - z->ob_size = -(z->ob_size); + NEGATE(z); return long_normalize(z); } @@ -2038,6 +2283,9 @@ long_add(PyLongObject *v, PyLongObject *w) CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); + if (ABS(a->ob_size) <= 1 && ABS(b->ob_size) <= 1) + return PyInt_FromLong(MEDIUM_VALUE(a) + + MEDIUM_VALUE(b)); if (a->ob_size < 0) { if (b->ob_size < 0) { z = x_add(a, b); @@ -2065,6 +2313,8 @@ long_sub(PyLongObject *v, PyLongObject *w) CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); + if (ABS(a->ob_size) <= 1 && ABS(b->ob_size) <= 1) + return PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); if (a->ob_size < 0) { if (b->ob_size < 0) z = x_sub(a, b); @@ -2494,10 +2744,13 @@ long_mul(PyLongObject *v, PyLongObject *w) return Py_NotImplemented; } + if (ABS(v->ob_size) <= 1 && ABS(w->ob_size) <= 1) + return PyLong_FromLong(MEDIUM_VALUE(v)*MEDIUM_VALUE(w)); + z = k_mul(a, b); /* Negate if exactly one of the inputs is negative. */ if (((a->ob_size ^ b->ob_size) < 0) && z) - z->ob_size = -(z->ob_size); + NEGATE(z); Py_DECREF(a); Py_DECREF(b); return (PyObject *)z; @@ -2603,7 +2856,7 @@ long_true_divide(PyObject *v, PyObject *w) if (bd == 0.0) { PyErr_SetString(PyExc_ZeroDivisionError, - "long division or modulo by zero"); + "int division or modulo by zero"); return NULL; } @@ -2622,7 +2875,7 @@ long_true_divide(PyObject *v, PyObject *w) overflow: PyErr_SetString(PyExc_OverflowError, - "long/long too large for a float"); + "int/int too large for a float"); return NULL; } @@ -2691,11 +2944,6 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) c = (PyLongObject *)x; Py_INCREF(x); } - else if (PyInt_Check(x)) { - c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x)); - if (c == NULL) - goto Error; - } else if (x == Py_None) c = NULL; else { @@ -2741,7 +2989,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) Py_DECREF(c); c = temp; temp = NULL; - c->ob_size = - c->ob_size; + NEGATE(c); } /* if modulus == 1: @@ -2862,6 +3110,8 @@ long_invert(PyLongObject *v) /* Implement ~x as -(x+1) */ PyLongObject *x; PyLongObject *w; + if (ABS(v->ob_size) <=1) + return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); w = (PyLongObject *)PyLong_FromLong(1L); if (w == NULL) return NULL; @@ -2888,11 +3138,8 @@ static PyObject * long_neg(PyLongObject *v) { PyLongObject *z; - if (v->ob_size == 0 && PyLong_CheckExact(v)) { - /* -0 == 0 */ - Py_INCREF(v); - return (PyObject *) v; - } + if (ABS(v->ob_size) <= 1) + return PyLong_FromLong(-MEDIUM_VALUE(v)); z = (PyLongObject *)_PyLong_Copy(v); if (z != NULL) z->ob_size = -(v->ob_size); @@ -3016,7 +3263,7 @@ long_lshift(PyObject *v, PyObject *w) if (z == NULL) goto lshift_error; if (a->ob_size < 0) - z->ob_size = -(z->ob_size); + NEGATE(z); for (i = 0; i < wordshift; i++) z->ob_digit[i] = 0; accum = 0; @@ -3194,22 +3441,7 @@ long_long(PyObject *v) static PyObject * long_int(PyObject *v) { - long x; - x = PyLong_AsLong(v); - if (PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_OverflowError)) { - PyErr_Clear(); - if (PyLong_CheckExact(v)) { - Py_INCREF(v); - return v; - } - else - return _PyLong_Copy((PyLongObject *)v); - } - else - return NULL; - } - return PyInt_FromLong(x); + return long_long(v); } static PyObject * @@ -3246,15 +3478,25 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (type != &PyLong_Type) return long_subtype_new(type, args, kwds); /* Wimp out */ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, &x, &base)) return NULL; if (x == NULL) return PyLong_FromLong(0L); if (base == -909) return PyNumber_Long(x); - else if (PyString_Check(x)) - return PyLong_FromString(PyString_AS_STRING(x), NULL, base); + else if (PyString_Check(x)) { + char *s = PyString_AS_STRING(x); + char *end; + PyObject *r = PyLong_FromString(s, &end, base); + if (r != NULL && end != s + PyString_GET_SIZE(x)) { + PyErr_SetString(PyExc_ValueError, + "null byte in argument for int()"); + Py_DECREF(r); + r = NULL; + } + return r; + } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(x)) return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), @@ -3263,7 +3505,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) #endif else { PyErr_SetString(PyExc_TypeError, - "long() can't convert non-string with explicit base"); + "int() can't convert non-string with explicit base"); return NULL; } } @@ -3312,9 +3554,9 @@ static PyMethodDef long_methods[] = { }; PyDoc_STRVAR(long_doc, -"long(x[, base]) -> integer\n\ +"int(x[, base]) -> integer\n\ \n\ -Convert a string or number to a long integer, if possible. A floating\n\ +Convert a string or number to an integer, if possible. A floating\n\ point argument will be truncated towards zero (this does not include a\n\ string representation of a floating point number!) When converting a\n\ string, use the optional base. It is an error to supply a base when\n\ @@ -3363,8 +3605,10 @@ static PyNumberMethods long_as_number = { PyTypeObject PyLong_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ - "long", /* tp_name */ - sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */ + "int", /* tp_name */ + /* See _PyLong_New for why this isn't + sizeof(PyLongObject) - sizeof(digit) */ + sizeof(PyVarObject), /* tp_basicsize */ sizeof(digit), /* tp_itemsize */ long_dealloc, /* tp_dealloc */ 0, /* tp_print */ @@ -3377,7 +3621,7 @@ PyTypeObject PyLong_Type = { 0, /* tp_as_mapping */ (hashfunc)long_hash, /* tp_hash */ 0, /* tp_call */ - 0, /* tp_str */ + long_repr, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ @@ -3402,3 +3646,43 @@ PyTypeObject PyLong_Type = { long_new, /* tp_new */ PyObject_Del, /* tp_free */ }; + +int +_PyLong_Init(void) +{ +#if NSMALLNEGINTS + NSMALLPOSINTS > 0 + int ival; + PyLongObject *v = small_ints; + for (ival = -NSMALLNEGINTS; ival < 0; ival++, v++) { + PyObject_INIT(v, &PyLong_Type); + v->ob_size = -1; + v->ob_digit[0] = -ival; + } + for (; ival < NSMALLPOSINTS; ival++, v++) { + PyObject_INIT(v, &PyLong_Type); + v->ob_size = ival ? 1 : 0; + v->ob_digit[0] = ival; + } +#endif + return 1; +} + +void +PyLong_Fini(void) +{ +#if 0 + int i; + /* This is currently not needed; the small integers + are statically allocated */ +#if NSMALLNEGINTS + NSMALLPOSINTS > 0 + PyIntObject **q; + + i = NSMALLNEGINTS + NSMALLPOSINTS; + q = small_ints; + while (--i >= 0) { + Py_XDECREF(*q); + *q++ = NULL; + } +#endif +#endif +} diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 61e6af5..5f6d0a6 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -4221,6 +4221,14 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type, int numdigits; /* len == numnondigits + numdigits */ int numnondigits = 0; + /* Avoid exceeding SSIZE_T_MAX */ + if (prec > PY_SSIZE_T_MAX-3) { + PyErr_SetString(PyExc_OverflowError, + "precision too large"); + return NULL; + } + + switch (type) { case 'd': case 'u': @@ -4565,6 +4573,8 @@ PyString_Format(PyObject *format, PyObject *args) goto error; } width = PyInt_AsLong(v); + if (width == -1 && PyErr_Occurred()) + goto error; if (width < 0) { flags |= F_LJUST; width = -width; @@ -4602,6 +4612,8 @@ PyString_Format(PyObject *format, PyObject *args) goto error; } prec = PyInt_AsLong(v); + if (prec == -1 && PyErr_Occurred()) + goto error; if (prec < 0) prec = 0; if (--fmtcnt >= 0) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index bde3454..fbff31b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7585,6 +7585,8 @@ PyObject *PyUnicode_Format(PyObject *format, goto onError; } width = PyInt_AsLong(v); + if (width == -1 && PyErr_Occurred()) + goto onError; if (width < 0) { flags |= F_LJUST; width = -width; @@ -7620,6 +7622,8 @@ PyObject *PyUnicode_Format(PyObject *format, goto onError; } prec = PyInt_AsLong(v); + if (prec == -1 && PyErr_Occurred()) + goto onError; if (prec < 0) prec = 0; if (--fmtcnt >= 0) diff --git a/PC/_msi.c b/PC/_msi.c index f4af92a..2aa2003 100644 --- a/PC/_msi.c +++ b/PC/_msi.c @@ -543,7 +543,7 @@ summary_setproperty(msiobj* si, PyObject *args) if (PyString_Check(data)) { status = MsiSummaryInfoSetProperty(si->h, field, VT_LPSTR, 0, NULL, PyString_AsString(data)); - } else if (PyInt_Check(data)) { + } else if (PyInt_CheckExact(data)) { status = MsiSummaryInfoSetProperty(si->h, field, VT_I4, PyInt_AsLong(data), NULL, NULL); } else { diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 3e6d237..93e6058 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2124,7 +2124,7 @@ _PyBuiltin_Init(void) SETBUILTIN("float", &PyFloat_Type); SETBUILTIN("frozenset", &PyFrozenSet_Type); SETBUILTIN("property", &PyProperty_Type); - SETBUILTIN("int", &PyInt_Type); + SETBUILTIN("int", &PyLong_Type); SETBUILTIN("list", &PyList_Type); SETBUILTIN("long", &PyLong_Type); SETBUILTIN("object", &PyBaseObject_Type); diff --git a/Python/ceval.c b/Python/ceval.c index 978ff61..1f18545 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3900,7 +3900,7 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) { if (v != NULL) { Py_ssize_t x; - if (PyInt_Check(v)) { + if (PyInt_CheckExact(v)) { /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, however, it looks like it should be AsSsize_t. There should be a comment here explaining why. diff --git a/Python/getargs.c b/Python/getargs.c index f6ddfaa..91d9b47 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -720,9 +720,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, case 'K': { /* long long sized bitfield */ unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); unsigned PY_LONG_LONG ival; - if (PyInt_Check(arg)) - ival = PyInt_AsUnsignedLongMask(arg); - else if (PyLong_Check(arg)) + if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongLongMask(arg); else return converterr("integer", arg, msgbuf, bufsize); diff --git a/Python/marshal.c b/Python/marshal.c index 1819eac..fd1bd72 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -144,31 +144,34 @@ w_object(PyObject *v, WFILE *p) else if (v == Py_True) { w_byte(TYPE_TRUE, p); } - else if (PyInt_Check(v)) { - long x = PyInt_AS_LONG((PyIntObject *)v); + else if (PyLong_Check(v)) { + long x = PyLong_AsLong(v); + if ((x == -1) && PyErr_Occurred()) { + PyLongObject *ob = (PyLongObject *)v; + PyErr_Clear(); + w_byte(TYPE_LONG, p); + n = ob->ob_size; + w_long((long)n, p); + if (n < 0) + n = -n; + for (i = 0; i < n; i++) + w_short(ob->ob_digit[i], p); + } + else { #if SIZEOF_LONG > 4 - long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); - if (y && y != -1) { - w_byte(TYPE_INT64, p); - w_long64(x, p); - } - else + long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); + if (y && y != -1) { + w_byte(TYPE_INT64, p); + w_long64(x, p); + } + else #endif { - w_byte(TYPE_INT, p); - w_long(x, p); + w_byte(TYPE_INT, p); + w_long(x, p); + } } } - else if (PyLong_Check(v)) { - PyLongObject *ob = (PyLongObject *)v; - w_byte(TYPE_LONG, p); - n = ob->ob_size; - w_long((long)n, p); - if (n < 0) - n = -n; - for (i = 0; i < n; i++) - w_short(ob->ob_digit[i], p); - } else if (PyFloat_Check(v)) { if (p->version > 1) { unsigned char buf[8]; diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 15fad81..ad6c44f 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -60,6 +60,8 @@ static void call_sys_exitfunc(void); static void call_ll_exitfuncs(void); extern void _PyUnicode_Init(void); extern void _PyUnicode_Fini(void); +extern int _PyLong_Init(void); +extern void PyLong_Fini(void); #ifdef WITH_THREAD extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); @@ -181,8 +183,8 @@ Py_InitializeEx(int install_sigs) if (!_PyFrame_Init()) Py_FatalError("Py_Initialize: can't init frames"); - if (!_PyInt_Init()) - Py_FatalError("Py_Initialize: can't init ints"); + if (!_PyLong_Init()) + Py_FatalError("Py_Initialize: can't init longs"); _PyFloat_Init(); @@ -453,7 +455,7 @@ Py_Finalize(void) PyList_Fini(); PySet_Fini(); PyString_Fini(); - PyInt_Fini(); + PyLong_Fini(); PyFloat_Fini(); #ifdef Py_USING_UNICODE diff --git a/Python/traceback.c b/Python/traceback.c index cfbd833..04078b9 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -250,7 +250,7 @@ PyTraceBack_Print(PyObject *v, PyObject *f) return -1; } limitv = PySys_GetObject("tracebacklimit"); - if (limitv && PyInt_Check(limitv)) { + if (limitv && PyInt_CheckExact(limitv)) { limit = PyInt_AsLong(limitv); if (limit <= 0) return 0; -- cgit v0.12