Index: Doc/lib/libfuncs.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libfuncs.tex,v retrieving revision 1.174 diff -c -r1.174 libfuncs.tex *** Doc/lib/libfuncs.tex 25 Aug 2004 10:42:39 -0000 1.174 --- Doc/lib/libfuncs.tex 15 Sep 2004 08:44:54 -0000 *************** *** 541,559 **** \begin{funcdesc}{int}{\optional{x\optional{, radix}}} Convert a string or number to a plain integer. If the argument is a ! string, it must contain a possibly signed decimal number ! representable as a Python integer, possibly embedded in whitespace. ! The \var{radix} parameter gives the base for the ! conversion and may be any integer in the range [2, 36], or zero. If ! \var{radix} is zero, the proper radix is guessed based on the ! contents of string; the interpretation is the same as for integer ! literals. If \var{radix} is specified and \var{x} is not a string, ! \exception{TypeError} is raised. ! Otherwise, the argument may be a plain or ! long integer or a floating point number. Conversion of floating ! point numbers to integers truncates (towards zero). ! If the argument is outside the integer range a long object will ! be returned instead. If no arguments are given, returns \code{0}. \end{funcdesc} \begin{funcdesc}{isinstance}{object, classinfo} --- 541,565 ---- \begin{funcdesc}{int}{\optional{x\optional{, radix}}} Convert a string or number to a plain integer. If the argument is a ! string, it must contain a possibly signed number representable as a ! Python integer, possibly embedded in whitespace. The \var{radix} ! parameter gives the base for the conversion and may be any integer ! in the range [2, 36], or zero. If \var{radix} is zero, the proper ! radix is guessed based on the contents of the string; the ! interpretation is the same as for integer literals. ! ! If \var{radix} is 256, the argument must be a string which will be ! interpreted as a big-endian byte-array encoding of an unsigned ! integer. If \var{radix} is -256, the argument will be interpreted ! as a big-endian byte-array encoding of a 2's-complement signed ! integer. ! ! If \var{radix} is specified and \var{x} is not a string, ! \exception{TypeError} is raised. Otherwise, the argument may be a ! plain or long integer or a floating point number. Conversion of ! floating point numbers to integers truncates (towards zero). If ! the argument is outside the integer range a long object will be ! returned instead. If no arguments are given, returns \code{0}. \end{funcdesc} \begin{funcdesc}{isinstance}{object, classinfo} Index: Lib/test/test_builtin.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_builtin.py,v retrieving revision 1.34 diff -c -r1.34 test_builtin.py *** Lib/test/test_builtin.py 7 Aug 2004 19:20:05 -0000 1.34 --- Lib/test/test_builtin.py 15 Sep 2004 08:44:57 -0000 *************** *** 556,561 **** --- 556,566 ---- self.assertEqual(int("10",16), 16L) if have_unicode: self.assertEqual(int(unicode("10"),16), 16L) + # 256 / -256 + self.assertEqual(int('\xFF\xFF\xFF', 256), 0xFFFFFF) + self.assertEqual(int('\xFF\xFF\xFF', -256), -1) + self.assertEqual(int('\xFF\xFF\xFF'*2, 256), 0xFFFFFFFFFFFFL) + self.assertEqual(int('\xFF\xFF\xFF'*2, -256), -1) # Test conversion from strings and various anomalies for s, v in L: for sign in "", "+", "-": *************** *** 729,734 **** --- 734,744 ---- self.assertEqual(long("10",16), 16L) if have_unicode: self.assertEqual(long(unicode("10"),16), 16L) + # 256 / -256 + self.assertEqual(long('\xFF\xFF\xFF', 256), 0xFFFFFFL) + self.assertEqual(long('\xFF\xFF\xFF', -256), -1L) + self.assertEqual(long('\xFF\xFF\xFF'*2, 256), 0xFFFFFFFFFFFFL) + self.assertEqual(long('\xFF\xFF\xFF'*2, -256), -1L) # Check conversions from string (same test set as for int(), and then some) LL = [ ('1' + '0'*20, 10L**20), Index: Objects/intobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v retrieving revision 2.113 diff -c -r2.113 intobject.c *** Objects/intobject.c 25 Aug 2004 02:14:08 -0000 2.113 --- Objects/intobject.c 15 Sep 2004 08:45:02 -0000 *************** *** 268,274 **** if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, ! "int() base must be >= 2 and <= 36"); return NULL; } --- 268,274 ---- if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, ! "int() base must be >= 2 and <= 36, 256, or -256"); return NULL; } *************** *** 877,882 **** --- 877,884 ---- PyObject *x = NULL; int base = -909; static char *kwlist[] = {"x", "base", 0}; + PyObject *longObject = NULL; + PyIntObject *intObject = NULL; if (type != &PyInt_Type) return int_subtype_new(type, args, kwds); /* Wimp out */ *************** *** 887,894 **** return PyInt_FromLong(0L); if (base == -909) return PyNumber_Int(x); ! if (PyString_Check(x)) ! return PyInt_FromString(PyString_AS_STRING(x), NULL, base); #ifdef Py_USING_UNICODE if (PyUnicode_Check(x)) return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), --- 889,918 ---- return PyInt_FromLong(0L); if (base == -909) return PyNumber_Int(x); ! if (PyString_Check(x)) { ! if ((base == 256) || (base == -256)) { ! if (base == 256) ! longObject = _PyLong_FromByteArray( ! PyString_AS_STRING(x), ! PyString_GET_SIZE(x), ! 0, /* big-endian */ ! 0 /* unsigned */ ); ! else ! longObject = _PyLong_FromByteArray( ! PyString_AS_STRING(x), ! PyString_GET_SIZE(x), ! 0, /* big-endian */ ! 1 /* signed */ ); ! if (longObject == NULL) ! return NULL; ! intObject = (PyIntObject *)PyLong_Type.tp_as_number-> ! nb_int(longObject); ! Py_DECREF(longObject); ! return (PyObject *)intObject; ! } ! else ! return PyInt_FromString(PyString_AS_STRING(x), NULL, base); ! } #ifdef Py_USING_UNICODE if (PyUnicode_Check(x)) return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), Index: Objects/longobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v retrieving revision 1.164 diff -c -r1.164 longobject.c *** Objects/longobject.c 30 Aug 2004 02:58:59 -0000 1.164 --- Objects/longobject.c 15 Sep 2004 08:45:03 -0000 *************** *** 1250,1256 **** if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, ! "long() arg 2 must be >= 2 and <= 36"); return NULL; } while (*str != '\0' && isspace(Py_CHARMASK(*str))) --- 1250,1256 ---- if ((base != 0 && base < 2) || base > 36) { PyErr_SetString(PyExc_ValueError, ! "long() arg 2 must be >= 2 and <= 36, 256, or -256"); return NULL; } while (*str != '\0' && isspace(Py_CHARMASK(*str))) *************** *** 2896,2902 **** if (base == -909) return PyNumber_Long(x); else if (PyString_Check(x)) ! return PyLong_FromString(PyString_AS_STRING(x), NULL, base); #ifdef Py_USING_UNICODE else if (PyUnicode_Check(x)) return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), --- 2896,2913 ---- if (base == -909) return PyNumber_Long(x); else if (PyString_Check(x)) ! if (base == 256) ! return _PyLong_FromByteArray(PyString_AS_STRING(x), ! PyString_GET_SIZE(x), ! 0, /* big-endian */ ! 0 /* unsigned */ ); ! else if (base == -256) ! return _PyLong_FromByteArray(PyString_AS_STRING(x), ! PyString_GET_SIZE(x), ! 0, /* big-endian */ ! 1 /* signed */ ); ! else ! return PyLong_FromString(PyString_AS_STRING(x), NULL, base); #ifdef Py_USING_UNICODE else if (PyUnicode_Check(x)) return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),