diff -r 4f6f4aa0d80f Lib/test/test_tcl.py --- a/Lib/test/test_tcl.py Sat Feb 21 08:44:05 2015 -0800 +++ b/Lib/test/test_tcl.py Sat Feb 21 19:34:57 2015 +0200 @@ -135,7 +135,13 @@ class TclTest(unittest.TestCase): def test_getint(self): tcl = self.interp.tk - self.assertEqual(tcl.getint(' 42 '), 42) + integers = (0, 1, -1, 2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63) + if tcl_version >= (8, 5): + integers += (2**63, -2**63-1, 2**1000, -2**1000) + for i in integers: + self.assertEqual(tcl.getint(' %d ' % i), i) + self.assertEqual(tcl.getint(' %#o ' % i), i) + self.assertEqual(tcl.getint(' %#x ' % i), i) self.assertEqual(tcl.getint(42), 42) self.assertRaises(TypeError, tcl.getint) self.assertRaises(TypeError, tcl.getint, '42', '10') @@ -244,8 +250,8 @@ class TclTest(unittest.TestCase): tcl.call('set', 'b', 6) def check(expr, expected): result = tcl.exprstring(expr) + self.assertIsInstance(result, str) self.assertEqual(result, expected) - self.assertIsInstance(result, str) self.assertRaises(TypeError, tcl.exprstring) self.assertRaises(TypeError, tcl.exprstring, '8.2', '+6') @@ -279,8 +285,8 @@ class TclTest(unittest.TestCase): tcl.call('set', 'b', 6) def check(expr, expected): result = tcl.exprdouble(expr) + self.assertIsInstance(result, float) self.assertEqual(result, expected) - self.assertIsInstance(result, float) self.assertRaises(TypeError, tcl.exprdouble) self.assertRaises(TypeError, tcl.exprdouble, '8.2', '+6') @@ -311,8 +317,8 @@ class TclTest(unittest.TestCase): tcl.call('set', 'b', 6) def check(expr, expected): result = tcl.exprlong(expr) + self.assertIsInstance(result, int) self.assertEqual(result, expected) - self.assertIsInstance(result, int) self.assertRaises(TypeError, tcl.exprlong) self.assertRaises(TypeError, tcl.exprlong, '8.2', '+6') @@ -343,9 +349,9 @@ class TclTest(unittest.TestCase): tcl.call('set', 'b', 6) def check(expr, expected): result = tcl.exprboolean(expr) - self.assertEqual(result, expected) self.assertIsInstance(result, int) self.assertNotIsInstance(result, bool) + self.assertEqual(result, expected) self.assertRaises(TypeError, tcl.exprboolean) self.assertRaises(TypeError, tcl.exprboolean, '8.2', '+6') @@ -378,6 +384,20 @@ class TclTest(unittest.TestCase): if tcl_version >= (8, 5): check('2**64', True) + def test_expr_bignum(self): + tcl = self.interp + integers = (2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63) + if tcl_version >= (8, 5): + integers += (2**63, -2**63-1, 2**1000, -2**1000) + for i in integers: + result = tcl.call('expr', str(i)) + if self.wantobjects: + self.assertIsInstance(result, int) + self.assertEqual(result, i) + else: + self.assertIsInstance(result, str) + self.assertEqual(result, str(i)) + def test_passing_values(self): def passValue(value): return self.interp.call('set', '_', value) @@ -395,7 +415,10 @@ class TclTest(unittest.TestCase): b'str\xc0\x80ing' if self.wantobjects else 'str\xc0\x80ing') self.assertEqual(passValue(b'str\xbding'), b'str\xbding' if self.wantobjects else 'str\xbding') - for i in (0, 1, -1, 2**31-1, -2**31): + integers = (0, 1, -1, 2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63) + if tcl_version >= (8, 5): + integers += (2**63, -2**63-1, 2**1000, -2**1000) + for i in integers: self.assertEqual(passValue(i), i if self.wantobjects else str(i)) for f in (0.0, 1.0, -1.0, 1/3, sys.float_info.min, sys.float_info.max, @@ -456,7 +479,10 @@ class TclTest(unittest.TestCase): check(b'str\x00ing', 'str\x00ing') check(b'str\xc0\x80ing', 'str\xc0\x80ing') check(b'str\xc0\x80ing\xe2\x82\xac', 'str\xc0\x80ing\xe2\x82\xac') - for i in (0, 1, -1, 2**31-1, -2**31): + integers = (0, 1, -1, 2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63) + if tcl_version >= (8, 5): + integers += (2**63, -2**63-1, 2**1000, -2**1000) + for i in integers: check(i, str(i)) for f in (0.0, 1.0, -1.0): check(f, repr(f)) diff -r 4f6f4aa0d80f Modules/_tkinter.c --- a/Modules/_tkinter.c Sat Feb 21 08:44:05 2015 -0800 +++ b/Modules/_tkinter.c Sat Feb 21 19:34:57 2015 +0200 @@ -56,6 +56,11 @@ Copyright (C) 1994 Steen Lumholt. #error "Tk older than 8.4 not supported" #endif +#if TK_VERSION_HEX >= 0x08050000 +#define HAVE_LIBTOMMAMTH +#include +#endif + #if !(defined(MS_WINDOWS) || defined(__CYGWIN__)) #define HAVE_CREATEFILEHANDLER #endif @@ -233,6 +238,8 @@ typedef struct { const Tcl_ObjType *ByteArrayType; const Tcl_ObjType *DoubleType; const Tcl_ObjType *IntType; + const Tcl_ObjType *WideIntType; + const Tcl_ObjType *BignumType; const Tcl_ObjType *ListType; const Tcl_ObjType *ProcBodyType; const Tcl_ObjType *StringType; @@ -589,6 +596,8 @@ Tkapp_New(const char *screenName, const v->ByteArrayType = Tcl_GetObjType("bytearray"); v->DoubleType = Tcl_GetObjType("double"); v->IntType = Tcl_GetObjType("int"); + v->WideIntType = Tcl_GetObjType("wideInt"); + v->BignumType = Tcl_GetObjType("bignum"); v->ListType = Tcl_GetObjType("list"); v->ProcBodyType = Tcl_GetObjType("procbody"); v->StringType = Tcl_GetObjType("string"); @@ -881,12 +890,49 @@ static PyType_Spec PyTclObject_Type_spec #define CHECK_STRING_LENGTH(s) #endif +#ifdef HAVE_LIBTOMMAMTH +static Tcl_Obj* +asBignumObj(PyObject *value) +{ + Tcl_Obj *result; + int neg; + PyObject *hexstr; + char *hexchars; + mp_int bigValue; + + neg = Py_SIZE(value) < 0; + hexstr = _PyLong_Format(value, 16); + if (hexstr == NULL) + return NULL; + hexchars = PyUnicode_AsUTF8(hexstr); + if (hexchars == NULL) { + Py_DECREF(hexstr); + return NULL; + } + hexchars += neg + 2; /* skip sign and "0x" */ + mp_init(&bigValue); + if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) { + mp_clear(&bigValue); + Py_DECREF(hexstr); + PyErr_NoMemory(); + return NULL; + } + Py_DECREF(hexstr); + bigValue.sign = neg ? MP_NEG : MP_ZPOS; + result = Tcl_NewBignumObj(&bigValue); + mp_clear(&bigValue); + if (result == NULL) { + PyErr_NoMemory(); + return NULL; + } + return result; +} +#endif + static Tcl_Obj* AsObj(PyObject *value) { Tcl_Obj *result; - long longVal; - int overflow; if (PyBytes_Check(value)) { if (PyBytes_GET_SIZE(value) >= INT_MAX) { @@ -896,18 +942,39 @@ AsObj(PyObject *value) return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value), (int)PyBytes_GET_SIZE(value)); } - else if (PyBool_Check(value)) + if (PyBool_Check(value)) return Tcl_NewBooleanObj(PyObject_IsTrue(value)); - else if (PyLong_CheckExact(value) && - ((longVal = PyLong_AsLongAndOverflow(value, &overflow)), - !overflow)) { + + if (PyLong_CheckExact(value)) { + int overflow; + long longValue; + Tcl_WideInt wideValue; + + longValue = PyLong_AsLongAndOverflow(value, &overflow); + if (!overflow) { + return Tcl_NewLongObj(longValue); + } /* If there is an overflow in the long conversion, - fall through to default object handling. */ - return Tcl_NewLongObj(longVal); + fall through to wideInt handling. */ + + if (_PyLong_AsByteArray((PyLongObject *)value, + (unsigned char *)(void *)&wideValue, + sizeof(wideValue), + PY_LITTLE_ENDIAN, + /* signed */ 1) == 0) { + return Tcl_NewWideIntObj(wideValue); + } + PyErr_Clear(); + /* If there is an overflow in the wideInt conversion, + fall through to bignum handling. */ +#ifdef HAVE_LIBTOMMAMTH + return asBignumObj(value); +#endif } - else if (PyFloat_Check(value)) + + if (PyFloat_Check(value)) return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); - else if (PyTuple_Check(value) || PyList_Check(value)) { + if (PyTuple_Check(value) || PyList_Check(value)) { Tcl_Obj **argv; Py_ssize_t size, i; @@ -931,7 +998,7 @@ AsObj(PyObject *value) PyMem_Free(argv); return result; } - else if (PyUnicode_Check(value)) { + if (PyUnicode_Check(value)) { void *inbuf; Py_ssize_t size; int kind; @@ -981,12 +1048,12 @@ AsObj(PyObject *value) PyMem_Free(outbuf); return result; } - else if(PyTclObject_Check(value)) { + if(PyTclObject_Check(value)) { Tcl_Obj *v = ((PyTclObject*)value)->value; Tcl_IncrRefCount(v); return v; } - else { + { PyObject *v = PyObject_Str(value); if (!v) return 0; @@ -997,10 +1064,65 @@ AsObj(PyObject *value) } static PyObject* +fromWideIntObj(PyObject* tkapp, Tcl_Obj *value) +{ + Tcl_WideInt wideValue; + if (Tcl_GetWideIntFromObj(Tkapp_Interp(tkapp), value, &wideValue) == TCL_OK) { +#ifdef HAVE_LONG_LONG + if (sizeof(wideValue) <= SIZEOF_LONG_LONG) + return PyLong_FromLongLong(wideValue); +#endif + return _PyLong_FromByteArray((unsigned char *)(void *)&wideValue, + sizeof(wideValue), + PY_LITTLE_ENDIAN, + /* signed */ 1); + } + return NULL; +} + +#ifdef HAVE_LIBTOMMAMTH +static PyObject* +fromBignumObj(PyObject* tkapp, Tcl_Obj *value) +{ + mp_int bigValue; + unsigned long numBytes; + unsigned char *bytes; + PyObject *res; + + if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK) + return Tkinter_Error(tkapp); + numBytes = mp_unsigned_bin_size(&bigValue); + bytes = PyMem_Malloc(numBytes); + if (bytes == NULL) { + mp_clear(&bigValue); + return NULL; + } + if (mp_to_unsigned_bin_n(&bigValue, bytes, + &numBytes) != MP_OKAY) { + mp_clear(&bigValue); + PyMem_Free(bytes); + return PyErr_NoMemory(); + } + res = _PyLong_FromByteArray(bytes, numBytes, + /* big-endian */ 0, + /* unsigned */ 0); + PyMem_Free(bytes); + if (res != NULL && bigValue.sign == MP_NEG) { + PyObject *res2 = PyNumber_Negative(res); + Py_DECREF(res); + res = res2; + } + mp_clear(&bigValue); + return res; +} +#endif + +static PyObject* FromObj(PyObject* tkapp, Tcl_Obj *value) { PyObject *result = NULL; TkappObject *app = (TkappObject*)tkapp; + Tcl_Interp *interp = Tkapp_Interp(tkapp); if (value->typePtr == NULL) { return unicodeFromTclStringAndSize(value->bytes, value->length); @@ -1023,7 +1145,21 @@ FromObj(PyObject* tkapp, Tcl_Obj *value) } if (value->typePtr == app->IntType) { - return PyLong_FromLong(value->internalRep.longValue); + long longValue; + if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK) + return PyLong_FromLong(longValue); + /* If there is an error in the long conversion, + fall through to wideInt handling. */ + } + + if (value->typePtr == app->IntType || + value->typePtr == app->WideIntType) { + result = fromWideIntObj(tkapp, value); + if (result != NULL || PyErr_Occurred()) + return result; + Tcl_ResetResult(interp); + /* If there is an error in the wideInt conversion, + fall through to bignum handling. */ } if (value->typePtr == app->ListType) { @@ -1032,15 +1168,14 @@ FromObj(PyObject* tkapp, Tcl_Obj *value) PyObject *elem; Tcl_Obj *tcl_elem; - status = Tcl_ListObjLength(Tkapp_Interp(tkapp), value, &size); + status = Tcl_ListObjLength(interp, value, &size); if (status == TCL_ERROR) return Tkinter_Error(tkapp); result = PyTuple_New(size); if (!result) return NULL; for (i = 0; i < size; i++) { - status = Tcl_ListObjIndex(Tkapp_Interp(tkapp), - value, i, &tcl_elem); + status = Tcl_ListObjIndex(interp, value, i, &tcl_elem); if (status == TCL_ERROR) { Py_DECREF(result); return Tkinter_Error(tkapp); @@ -1065,6 +1200,20 @@ FromObj(PyObject* tkapp, Tcl_Obj *value) Tcl_GetCharLength(value)); } +#ifdef HAVE_LIBTOMMAMTH + if (value->typePtr == app->IntType || + value->typePtr == app->WideIntType || + value->typePtr == app->BignumType) { + return fromBignumObj(tkapp, value); + } + if (app->BignumType == NULL && + strcmp(value->typePtr->name, "bignum") == 0) { + /* bignum type is not registered in Tcl */ + app->BignumType = value->typePtr; + return fromBignumObj(tkapp, value); + } +#endif + return newPyTclObject(value); } @@ -1699,7 +1848,8 @@ static PyObject * Tkapp_GetInt(PyObject *self, PyObject *args) { char *s; - int v; + Tcl_Obj *value; + PyObject *result; if (PyTuple_Size(args) == 1) { PyObject* o = PyTuple_GetItem(args, 0); @@ -1711,9 +1861,24 @@ Tkapp_GetInt(PyObject *self, PyObject *a if (!PyArg_ParseTuple(args, "s:getint", &s)) return NULL; CHECK_STRING_LENGTH(s); - if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) + value = Tcl_NewStringObj(s, -1); + if (value == NULL) return Tkinter_Error(self); - return Py_BuildValue("i", v); + /* Don't use Tcl_GetInt() because it returns ambiguous result for value + in ranges -2**32..-2**31-1 and 2**31..2**32-1 (on 32-bit platform). + + Prefer bignum because Tcl_GetWideIntFromObj returns ambiguous result for + value in ranges -2**64..-2**63-1 and 2**63..2**64-1 (on 32-bit platform). + */ +#ifdef HAVE_LIBTOMMAMTH + result = fromBignumObj(self, value); +#else + result = fromWideIntObj(self, value); +#endif + Tcl_DecrRefCount(value); + if (result != NULL || PyErr_Occurred()) + return result; + return Tkinter_Error(self); } static PyObject *