summaryrefslogtreecommitdiff
path: root/Modules/_tkinter.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_tkinter.c')
-rw-r--r--Modules/_tkinter.c81
1 files changed, 31 insertions, 50 deletions
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 3608e2d993..f42f068bb3 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -80,18 +80,6 @@ Copyright (C) 1994 Steen Lumholt.
#error "Tk older than 8.3.1 not supported"
#endif
-/* Unicode conversion assumes that Tcl_UniChar is two bytes.
- We cannot test this directly, so we test UTF-8 size instead,
- expecting that TCL_UTF_MAX is changed if Tcl ever supports
- either UTF-16 or UCS-4.
- Redhat 8 sets TCL_UTF_MAX to 6, and uses wchar_t for
- Tcl_Unichar. This is also ok as long as Python uses UCS-4,
- as well.
-*/
-#if TCL_UTF_MAX != 3 && !(defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==6)
-#error "unsupported Tcl configuration"
-#endif
-
#if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
#define HAVE_CREATEFILEHANDLER
#endif
@@ -975,23 +963,32 @@ AsObj(PyObject *value)
return result;
}
else if (PyUnicode_Check(value)) {
- Py_UNICODE *inbuf = PyUnicode_AS_UNICODE(value);
- Py_ssize_t size = PyUnicode_GET_SIZE(value);
- /* This #ifdef assumes that Tcl uses UCS-2.
- See TCL_UTF_MAX test above. */
-#if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX == 3
+ void *inbuf;
+ Py_ssize_t size;
+ int kind;
Tcl_UniChar *outbuf = NULL;
Py_ssize_t i;
- size_t allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
- if (allocsize >= size)
- outbuf = (Tcl_UniChar*)ckalloc(allocsize);
+ size_t allocsize;
+
+ if (PyUnicode_READY(value) == -1)
+ return NULL;
+
+ inbuf = PyUnicode_DATA(value);
+ size = PyUnicode_GET_LENGTH(value);
+ kind = PyUnicode_KIND(value);
+ allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
+ outbuf = (Tcl_UniChar*)ckalloc(allocsize);
/* Else overflow occurred, and we take the next exit */
if (!outbuf) {
PyErr_NoMemory();
return NULL;
}
for (i = 0; i < size; i++) {
- if (inbuf[i] >= 0x10000) {
+ Py_UCS4 ch = PyUnicode_READ(kind, inbuf, i);
+ /* We cannot test for sizeof(Tcl_UniChar) directly,
+ so we test for UTF-8 size instead. */
+#if TCL_UTF_MAX == 3
+ if (ch >= 0x10000) {
/* Tcl doesn't do UTF-16, yet. */
PyErr_Format(PyExc_ValueError,
"character U+%x is above the range "
@@ -999,16 +996,13 @@ AsObj(PyObject *value)
inbuf[i]);
ckfree(FREECAST outbuf);
return NULL;
+#endif
}
- outbuf[i] = inbuf[i];
+ outbuf[i] = ch;
}
result = Tcl_NewUnicodeObj(outbuf, size);
ckfree(FREECAST outbuf);
return result;
-#else
- return Tcl_NewUnicodeObj(inbuf, size);
-#endif
-
}
else if(PyTclObject_Check(value)) {
Tcl_Obj *v = ((PyTclObject*)value)->value;
@@ -1090,24 +1084,14 @@ FromObj(PyObject* tkapp, Tcl_Obj *value)
}
if (value->typePtr == app->StringType) {
-#if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==3
- PyObject *result;
- int size;
- Tcl_UniChar *input;
- Py_UNICODE *output;
-
- size = Tcl_GetCharLength(value);
- result = PyUnicode_FromUnicode(NULL, size);
- if (!result)
- return NULL;
- input = Tcl_GetUnicode(value);
- output = PyUnicode_AS_UNICODE(result);
- while (size--)
- *output++ = *input++;
- return result;
+#if TCL_UTF_MAX==3
+ return PyUnicode_FromKindAndData(
+ PyUnicode_2BYTE_KIND, Tcl_GetUnicode(value),
+ Tcl_GetCharLength(value));
#else
- return PyUnicode_FromUnicode(Tcl_GetUnicode(value),
- Tcl_GetCharLength(value));
+ return PyUnicode_FromKindAndData(
+ PyUnicode_4BYTE_KIND, Tcl_GetUnicode(value),
+ Tcl_GetCharLength(value));
#endif
}
@@ -2007,7 +1991,7 @@ static int
PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
- PyObject *self, *func, *arg, *res;
+ PyObject *func, *arg, *res;
int i, rv;
Tcl_Obj *obj_res;
@@ -2016,7 +2000,6 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
/* TBD: no error checking here since we know, via the
* Tkapp_CreateCommand() that the client data is a two-tuple
*/
- self = data->self;
func = data->func;
/* Create argument list (argv1, ..., argvN) */
@@ -2415,11 +2398,9 @@ static PyObject *
Tktt_Repr(PyObject *self)
{
TkttObject *v = (TkttObject *)self;
- char buf[100];
-
- PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
- v->func == NULL ? ", handler deleted" : "");
- return PyUnicode_FromString(buf);
+ return PyUnicode_FromFormat("<tktimertoken at %p%s>",
+ v,
+ v->func == NULL ? ", handler deleted" : "");
}
static PyTypeObject Tktt_Type =