cpython: 9291b28157e1 (original) (raw)
--- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -133,9 +133,20 @@ class TclTest(unittest.TestCase): tcl = self.interp self.assertRaises(TclError,tcl.unsetvar,'a')
- def get_integers(self):
integers = (0, 1, -1, 2**31-1, -2**31, 2**31, -2**31-1, 2**63-1, -2**63)[](#l1.8)
if tcl_version >= (8, 5): # bignum was added in Tcl 8.5[](#l1.9)
integers += (2**63, -2**63-1, 2**1000, -2**1000)[](#l1.10)
return integers[](#l1.11)
+ def test_getint(self): tcl = self.interp.tk
self.assertEqual(tcl.getint(' 42 '), 42)[](#l1.15)
for i in self.get_integers():[](#l1.16)
self.assertEqual(tcl.getint(' %d ' % i), i)[](#l1.17)
self.assertEqual(tcl.getint(' %#o ' % i), i)[](#l1.18)
self.assertEqual(tcl.getint(' %#x ' % i), i)[](#l1.19)
if tcl_version < (8, 5): # bignum was added in Tcl 8.5[](#l1.20)
self.assertRaises(TclError, tcl.getint, str(2**1000))[](#l1.21) self.assertEqual(tcl.getint(42), 42)[](#l1.22) self.assertRaises(TypeError, tcl.getint)[](#l1.23) self.assertRaises(TypeError, tcl.getint, '42', '10')[](#l1.24)
@@ -270,7 +281,7 @@ class TclTest(unittest.TestCase): check('"a\xbd\u20ac"', 'a\xbd\u20ac') check(r'"a\xbd\u20ac"', 'a\xbd\u20ac') check(r'"a\0b"', 'a\x00b')
if tcl_version >= (8, 5):[](#l1.29)
if tcl_version >= (8, 5): # bignum was added in Tcl 8.5[](#l1.30) check('2**64', str(2**64))[](#l1.31)
def test_exprdouble(self): @@ -302,7 +313,7 @@ class TclTest(unittest.TestCase): check('[string length "a\xbd\u20ac"]', 3.0) check(r'[string length "a\xbd\u20ac"]', 3.0) self.assertRaises(TclError, tcl.exprdouble, '"abc"')
if tcl_version >= (8, 5):[](#l1.38)
if tcl_version >= (8, 5): # bignum was added in Tcl 8.5[](#l1.39) check('2**64', float(2**64))[](#l1.40)
def test_exprlong(self): @@ -334,7 +345,7 @@ class TclTest(unittest.TestCase): check('[string length "a\xbd\u20ac"]', 3) check(r'[string length "a\xbd\u20ac"]', 3) self.assertRaises(TclError, tcl.exprlong, '"abc"')
if tcl_version >= (8, 5):[](#l1.47)
if tcl_version >= (8, 5): # bignum was added in Tcl 8.5[](#l1.48) self.assertRaises(TclError, tcl.exprlong, '2**64')[](#l1.49)
def test_exprboolean(self): @@ -375,7 +386,7 @@ class TclTest(unittest.TestCase): check('[string length "a\xbd\u20ac"]', True) check(r'[string length "a\xbd\u20ac"]', True) self.assertRaises(TclError, tcl.exprboolean, '"abc"')
if tcl_version >= (8, 5):[](#l1.56)
if tcl_version >= (8, 5): # bignum was added in Tcl 8.5[](#l1.57) check('2**64', True)[](#l1.58)
def test_booleans(self): @@ -397,6 +408,21 @@ class TclTest(unittest.TestCase): check('1 < 2', True) check('1 > 2', False)
- def test_expr_bignum(self):
tcl = self.interp[](#l1.66)
for i in self.get_integers():[](#l1.67)
result = tcl.call('expr', str(i))[](#l1.68)
if self.wantobjects:[](#l1.69)
self.assertEqual(result, i)[](#l1.70)
self.assertIsInstance(result, int)[](#l1.71)
else:[](#l1.72)
self.assertEqual(result, str(i))[](#l1.73)
self.assertIsInstance(result, str)[](#l1.74)
if tcl_version < (8, 5): # bignum was added in Tcl 8.5[](#l1.75)
result = tcl.call('expr', str(2**1000))[](#l1.76)
self.assertEqual(result, str(2**1000))[](#l1.77)
self.assertIsInstance(result, str)[](#l1.78)
+ def test_passing_values(self): def passValue(value): return self.interp.call('set', '_', value) @@ -414,8 +440,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):[](#l1.87)
for i in self.get_integers():[](#l1.88) self.assertEqual(passValue(i), i if self.wantobjects else str(i))[](#l1.89)
if tcl_version < (8, 5): # bignum was added in Tcl 8.5[](#l1.90)
self.assertEqual(passValue(2**1000), str(2**1000))[](#l1.91) for f in (0.0, 1.0, -1.0, 1/3,[](#l1.92) sys.float_info.min, sys.float_info.max,[](#l1.93) -sys.float_info.min, -sys.float_info.max):[](#l1.94)
@@ -475,8 +503,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):[](#l1.99)
for i in self.get_integers():[](#l1.100) check(i, str(i))[](#l1.101)
if tcl_version < (8, 5): # bignum was added in Tcl 8.5[](#l1.102)
check(2**1000, str(2**1000))[](#l1.103) for f in (0.0, 1.0, -1.0):[](#l1.104) check(f, repr(f))[](#l1.105) for f in (1/3.0, sys.float_info.min, sys.float_info.max,[](#l1.106)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,6 +16,9 @@ Core and Builtins Library ------- +- Issue #16840: Tkinter now supports 64-bit integers added in Tcl 8.4 and
- Issue #23834: Fix socket.sendto(), use the C Py_ssize_t type to store the result of sendto() instead of the C int type.
--- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -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 <tclTomMath.h> +#endif + #if !(defined(MS_WINDOWS) || defined(CYGWIN)) #define HAVE_CREATEFILEHANDLER #endif @@ -234,6 +239,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; @@ -591,6 +598,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"); @@ -883,12 +892,49 @@ static PyType_Spec PyTclObject_Type_spec
#define CHECK_STRING_LENGTH(s) #endif +#ifdef HAVE_LIBTOMMAMTH +static Tcl_Obj* +asBignumObj(PyObject *value) +{
- neg = Py_SIZE(value) < 0;
- hexstr = _PyLong_Format(value, 16);
- if (hexstr == NULL)
return NULL;[](#l3.50)
- hexchars = PyUnicode_AsUTF8(hexstr);
- if (hexchars == NULL) {
Py_DECREF(hexstr);[](#l3.53)
return NULL;[](#l3.54)
- }
- hexchars += neg + 2; /* skip sign and "0x" */
- mp_init(&bigValue);
- if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) {
mp_clear(&bigValue);[](#l3.59)
Py_DECREF(hexstr);[](#l3.60)
PyErr_NoMemory();[](#l3.61)
return NULL;[](#l3.62)
- }
- Py_DECREF(hexstr);
- bigValue.sign = neg ? MP_NEG : MP_ZPOS;
- result = Tcl_NewBignumObj(&bigValue);
- mp_clear(&bigValue);
- if (result == NULL) {
PyErr_NoMemory();[](#l3.69)
return NULL;[](#l3.70)
- }
- return result;
+} +#endif + static Tcl_Obj* AsObj(PyObject *value) { Tcl_Obj *result;
if (PyBytes_Check(value)) { if (PyBytes_GET_SIZE(value) >= INT_MAX) { @@ -898,18 +944,45 @@ AsObj(PyObject *value) return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value), (int)PyBytes_GET_SIZE(value)); }
- else if (PyLong_CheckExact(value) &&
((longVal = PyLong_AsLongAndOverflow(value, &overflow)),[](#l3.94)
!overflow)) {[](#l3.95)
Tcl_WideInt wideValue;[](#l3.101)
longValue = PyLong_AsLongAndOverflow(value, &overflow);[](#l3.103)
if (!overflow) {[](#l3.104)
return Tcl_NewLongObj(longValue);[](#l3.105)
}[](#l3.106) /* If there is an overflow in the long conversion,[](#l3.107)
fall through to wideInt handling. */[](#l3.108)
if (_PyLong_AsByteArray((PyLongObject *)value,[](#l3.110)
(unsigned char *)(void *)&wideValue,[](#l3.111)
sizeof(wideValue),[](#l3.112)
PY_LITTLE_ENDIAN,[](#l3.113)
/* signed */ 1) == 0) {[](#l3.114)
return Tcl_NewWideIntObj(wideValue);[](#l3.115)
}[](#l3.116)
PyErr_Clear();[](#l3.117)
/* If there is an overflow in the wideInt conversion,[](#l3.119)
fall through to bignum handling. */[](#l3.120)
return asBignumObj(value);[](#l3.122)
/* If there is no wideInt or bignum support,[](#l3.124) fall through to default object handling. */[](#l3.125)
@@ -933,7 +1006,8 @@ AsObj(PyObject *value) PyMem_Free(argv); return result; }
@@ -983,12 +1057,14 @@ AsObj(PyObject *value) PyMem_Free(outbuf); return result; }
- if (PyTclObject_Check(value)) { Tcl_Obj v = ((PyTclObject)value)->value; Tcl_IncrRefCount(v); return v; }
@@ -1008,6 +1084,60 @@ fromBoolean(PyObject* tkapp, Tcl_Obj va } static PyObject +fromWideIntObj(PyObject* tkapp, Tcl_Obj *value) +{
Tcl_WideInt wideValue;[](#l3.171)
if (Tcl_GetWideIntFromObj(Tkapp_Interp(tkapp), value, &wideValue) == TCL_OK) {[](#l3.172)
if (sizeof(wideValue) <= SIZEOF_LONG_LONG)[](#l3.174)
return PyLong_FromLongLong(wideValue);[](#l3.175)
return _PyLong_FromByteArray((unsigned char *)(void *)&wideValue,[](#l3.177)
sizeof(wideValue),[](#l3.178)
PY_LITTLE_ENDIAN,[](#l3.179)
/* signed */ 1);[](#l3.180)
}[](#l3.181)
return NULL;[](#l3.182)
+} + +#ifdef HAVE_LIBTOMMAMTH +static PyObject* +fromBignumObj(PyObject* tkapp, Tcl_Obj *value) +{
- if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK)
return Tkinter_Error(tkapp);[](#l3.195)
- numBytes = mp_unsigned_bin_size(&bigValue);
- bytes = PyMem_Malloc(numBytes);
- if (bytes == NULL) {
mp_clear(&bigValue);[](#l3.199)
return PyErr_NoMemory();[](#l3.200)
- }
- if (mp_to_unsigned_bin_n(&bigValue, bytes,
&numBytes) != MP_OKAY) {[](#l3.203)
mp_clear(&bigValue);[](#l3.204)
PyMem_Free(bytes);[](#l3.205)
return PyErr_NoMemory();[](#l3.206)
- }
- res = _PyLong_FromByteArray(bytes, numBytes,
/* big-endian */ 0,[](#l3.209)
/* unsigned */ 0);[](#l3.210)
- PyMem_Free(bytes);
- if (res != NULL && bigValue.sign == MP_NEG) {
PyObject *res2 = PyNumber_Negative(res);[](#l3.213)
Py_DECREF(res);[](#l3.214)
res = res2;[](#l3.215)
- }
- mp_clear(&bigValue);
- return res;
+} +#endif + +static PyObject* FromObj(PyObject* tkapp, Tcl_Obj *value) { PyObject result = NULL; @@ -1034,9 +1164,31 @@ FromObj(PyObject tkapp, Tcl_Obj *value) } if (value->typePtr == app->IntType) {
return PyLong_FromLong(value->internalRep.longValue);[](#l3.230)
long longValue;[](#l3.231)
if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK)[](#l3.232)
return PyLong_FromLong(longValue);[](#l3.233)
/* If there is an error in the long conversion,[](#l3.234)
}fall through to wideInt handling. */[](#l3.235)
- if (value->typePtr == app->IntType ||
value->typePtr == app->WideIntType) {[](#l3.239)
result = fromWideIntObj(tkapp, value);[](#l3.240)
if (result != NULL || PyErr_Occurred())[](#l3.241)
return result;[](#l3.242)
Tcl_ResetResult(interp);[](#l3.243)
/* If there is an error in the wideInt conversion,[](#l3.244)
fall through to bignum handling. */[](#l3.245)
- }
- if (value->typePtr == app->IntType ||
value->typePtr == app->WideIntType ||[](#l3.250)
value->typePtr == app->BignumType) {[](#l3.251)
return fromBignumObj(tkapp, value);[](#l3.252)
- }
+#endif + if (value->typePtr == app->ListType) { int size; int i, status; @@ -1084,6 +1236,15 @@ FromObj(PyObject* tkapp, Tcl_Obj *value) } #endif +#ifdef HAVE_LIBTOMMAMTH
- if (app->BignumType == NULL &&
strcmp(value->typePtr->name, "bignum") == 0) {[](#l3.265)
/* bignum type is not registered in Tcl */[](#l3.266)
app->BignumType = value->typePtr;[](#l3.267)
return fromBignumObj(tkapp, value);[](#l3.268)
- }
+#endif + return newPyTclObject(value); } @@ -1718,7 +1879,8 @@ static PyObject * Tkapp_GetInt(PyObject *self, PyObject *args) { char *s;
if (PyTuple_Size(args) == 1) { PyObject* o = PyTuple_GetItem(args, 0); @@ -1730,9 +1892,24 @@ Tkapp_GetInt(PyObject *self, PyObject *a if (!PyArg_ParseTuple(args, "s:getint", &s)) return NULL; CHECK_STRING_LENGTH(s);
- /* 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).[](#l3.295)
Prefer bignum because Tcl_GetWideIntFromObj returns ambiguous result for[](#l3.297)
value in ranges -2**64..-2**63-1 and 2**63..2**64-1 (on 32-bit platform).[](#l3.298)
*/[](#l3.299)