mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
[ Bug #113803 ] [2.0b1 NT4.0] printing non asci char causes idle to abort
http://sourceforge.net/bugs/?func=detailbug&bug_id=113803&group_id=5470 Add Unicode support and error handling to AsString(). Both AsString() and Merge() now return NULL and set a proper Python exception condition when an error happens; Merge() and other callers of AsString() check for errors from AsString(). Also fixed cleanup in Merge() and Tkapp_Call() return cleanup code; the fv array was not necessarily completely initialized, causing calls to ckfree() with garbage arguments! (Also reindented some lines that were longer than 80 chars and reformatted some code that used an alien coding standard.)
This commit is contained in:
parent
476491396e
commit
2834b974b6
1 changed files with 86 additions and 44 deletions
|
@ -261,9 +261,25 @@ AsString(PyObject *value, PyObject *tmp)
|
|||
{
|
||||
if (PyString_Check(value))
|
||||
return PyString_AsString(value);
|
||||
else if (PyUnicode_Check(value)) {
|
||||
PyObject *v = PyUnicode_AsUTF8String(value);
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
if (PyList_Append(tmp, v) != 0) {
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(v);
|
||||
return PyString_AsString(v);
|
||||
}
|
||||
else {
|
||||
PyObject *v = PyObject_Str(value);
|
||||
PyList_Append(tmp, v);
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
if (PyList_Append(tmp, v) != 0) {
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(v);
|
||||
return PyString_AsString(v);
|
||||
}
|
||||
|
@ -281,7 +297,7 @@ Merge(PyObject *args)
|
|||
char **argv = NULL;
|
||||
int fvStore[ARGSZ];
|
||||
int *fv = NULL;
|
||||
int argc = 0, i;
|
||||
int argc = 0, fvc = 0, i;
|
||||
char *res = NULL;
|
||||
|
||||
if (!(tmp = PyList_New(0)))
|
||||
|
@ -296,7 +312,8 @@ Merge(PyObject *args)
|
|||
else if (!PyTuple_Check(args)) {
|
||||
argc = 1;
|
||||
fv[0] = 0;
|
||||
argv[0] = AsString(args, tmp);
|
||||
if (!(argv[0] = AsString(args, tmp)))
|
||||
goto finally;
|
||||
}
|
||||
else {
|
||||
argc = PyTuple_Size(args);
|
||||
|
@ -316,6 +333,7 @@ Merge(PyObject *args)
|
|||
fv[i] = 1;
|
||||
if (!(argv[i] = Merge(v)))
|
||||
goto finally;
|
||||
fvc++;
|
||||
}
|
||||
else if (v == Py_None) {
|
||||
argc = i;
|
||||
|
@ -323,14 +341,18 @@ Merge(PyObject *args)
|
|||
}
|
||||
else {
|
||||
fv[i] = 0;
|
||||
argv[i] = AsString(v, tmp);
|
||||
if (!(argv[i] = AsString(v, tmp)))
|
||||
goto finally;
|
||||
fvc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
res = Tcl_Merge(argc, argv);
|
||||
if (res == NULL)
|
||||
PyErr_SetString(Tkinter_TclError, "merge failed");
|
||||
|
||||
finally:
|
||||
for (i = 0; i < argc; i++)
|
||||
for (i = 0; i < fvc; i++)
|
||||
if (fv[i]) {
|
||||
ckfree(argv[i]);
|
||||
}
|
||||
|
@ -636,7 +658,7 @@ Tkapp_Call(PyObject *self, PyObject *args)
|
|||
char **argv = NULL;
|
||||
int fvStore[ARGSZ];
|
||||
int *fv = NULL;
|
||||
int argc = 0, i;
|
||||
int argc = 0, fvc = 0, i;
|
||||
PyObject *res = NULL; /* except this has a different type */
|
||||
Tcl_CmdInfo info; /* and this is added */
|
||||
Tcl_Interp *interp = Tkapp_Interp(self); /* and this too */
|
||||
|
@ -653,7 +675,8 @@ Tkapp_Call(PyObject *self, PyObject *args)
|
|||
else if (!PyTuple_Check(args)) {
|
||||
argc = 1;
|
||||
fv[0] = 0;
|
||||
argv[0] = AsString(args, tmp);
|
||||
if (!(argv[0] = AsString(args, tmp)))
|
||||
goto finally;
|
||||
}
|
||||
else {
|
||||
argc = PyTuple_Size(args);
|
||||
|
@ -673,6 +696,7 @@ Tkapp_Call(PyObject *self, PyObject *args)
|
|||
fv[i] = 1;
|
||||
if (!(argv[i] = Merge(v)))
|
||||
goto finally;
|
||||
fvc++;
|
||||
}
|
||||
else if (v == Py_None) {
|
||||
argc = i;
|
||||
|
@ -680,7 +704,9 @@ Tkapp_Call(PyObject *self, PyObject *args)
|
|||
}
|
||||
else {
|
||||
fv[i] = 0;
|
||||
argv[i] = AsString(v, tmp);
|
||||
if (!(argv[i] = AsString(v, tmp)))
|
||||
goto finally;
|
||||
fvc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -725,7 +751,7 @@ Tkapp_Call(PyObject *self, PyObject *args)
|
|||
|
||||
/* Copied from Merge() again */
|
||||
finally:
|
||||
for (i = 0; i < argc; i++)
|
||||
for (i = 0; i < fvc; i++)
|
||||
if (fv[i]) {
|
||||
ckfree(argv[i]);
|
||||
}
|
||||
|
@ -753,10 +779,7 @@ Tkapp_GlobalCall(PyObject *self, PyObject *args)
|
|||
PyObject *res = NULL;
|
||||
|
||||
cmd = Merge(args);
|
||||
if (!cmd)
|
||||
PyErr_SetString(Tkinter_TclError, "merge failed");
|
||||
|
||||
else {
|
||||
if (cmd) {
|
||||
int err;
|
||||
ENTER_TCL
|
||||
err = Tcl_GlobalEval(Tkapp_Interp(self), cmd);
|
||||
|
@ -766,10 +789,8 @@ Tkapp_GlobalCall(PyObject *self, PyObject *args)
|
|||
else
|
||||
res = PyString_FromString(Tkapp_Result(self));
|
||||
LEAVE_OVERLAP_TCL
|
||||
}
|
||||
|
||||
if (cmd)
|
||||
ckfree(cmd);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -892,14 +913,19 @@ SetVar(PyObject *self, PyObject *args, int flags)
|
|||
if (PyArg_ParseTuple(args, "sO:setvar", &name1, &newValue)) {
|
||||
/* XXX Merge? */
|
||||
s = AsString(newValue, tmp);
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
ENTER_TCL
|
||||
ok = Tcl_SetVar(Tkapp_Interp(self), name1, s, flags);
|
||||
LEAVE_TCL
|
||||
}
|
||||
else {
|
||||
PyErr_Clear();
|
||||
if (PyArg_ParseTuple(args, "ssO:setvar", &name1, &name2, &newValue)) {
|
||||
if (PyArg_ParseTuple(args, "ssO:setvar",
|
||||
&name1, &name2, &newValue)) {
|
||||
s = AsString(newValue, tmp);
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
ENTER_TCL
|
||||
ok = Tcl_SetVar2(Tkapp_Interp(self), name1, name2,
|
||||
s, flags);
|
||||
|
@ -1192,8 +1218,6 @@ Tkapp_Merge(PyObject *self, PyObject *args)
|
|||
res = PyString_FromString(s);
|
||||
ckfree(s);
|
||||
}
|
||||
else
|
||||
PyErr_SetString(Tkinter_TclError, "merge failed");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -1225,7 +1249,8 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
|
|||
{
|
||||
PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
|
||||
PyObject *self, *func, *arg, *res, *tmp;
|
||||
int i;
|
||||
int i, rv;
|
||||
char *s;
|
||||
|
||||
ENTER_PYTHON
|
||||
|
||||
|
@ -1257,13 +1282,21 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
|
|||
return PythonCmd_Error(interp);
|
||||
}
|
||||
|
||||
Tcl_SetResult(Tkapp_Interp(self), AsString(res, tmp), TCL_VOLATILE);
|
||||
s = AsString(res, tmp);
|
||||
if (s == NULL) {
|
||||
rv = PythonCmd_Error(interp);
|
||||
}
|
||||
else {
|
||||
Tcl_SetResult(Tkapp_Interp(self), s, TCL_VOLATILE);
|
||||
rv = TCL_OK;
|
||||
}
|
||||
|
||||
Py_DECREF(res);
|
||||
Py_DECREF(tmp);
|
||||
|
||||
LEAVE_PYTHON
|
||||
|
||||
return TCL_OK;
|
||||
return rv;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -1417,7 +1450,8 @@ Tkapp_CreateFileHandler(PyObject *self, PyObject *args)
|
|||
PyObject *file, *func;
|
||||
int mask, tfile;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OiO:createfilehandler", &file, &mask, &func))
|
||||
if (!PyArg_ParseTuple(args, "OiO:createfilehandler",
|
||||
&file, &mask, &func))
|
||||
return NULL;
|
||||
tfile = PyObject_AsFileDescriptor(file);
|
||||
if (tfile < 0)
|
||||
|
@ -1604,7 +1638,8 @@ Tkapp_CreateTimerHandler(PyObject *self, PyObject *args)
|
|||
PyObject *func;
|
||||
TkttObject *v;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "iO:createtimerhandler", &milliseconds, &func))
|
||||
if (!PyArg_ParseTuple(args, "iO:createtimerhandler",
|
||||
&milliseconds, &func))
|
||||
return NULL;
|
||||
if (!PyCallable_Check(func)) {
|
||||
PyErr_SetString(PyExc_TypeError, "bad argument list");
|
||||
|
@ -1801,8 +1836,8 @@ typedef struct {
|
|||
static int
|
||||
_bump(FlattenContext* context, int size)
|
||||
{
|
||||
/* expand tuple to hold (at least) size new items. return true if
|
||||
successful, false if an exception was raised*/
|
||||
/* expand tuple to hold (at least) size new items.
|
||||
return true if successful, false if an exception was raised */
|
||||
|
||||
int maxsize = context->maxsize * 2;
|
||||
|
||||
|
@ -1822,12 +1857,14 @@ _flatten1(FlattenContext* context, PyObject* item, int depth)
|
|||
int i, size;
|
||||
|
||||
if (depth > 1000) {
|
||||
PyErr_SetString(PyExc_ValueError,"nesting too deep in _flatten");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"nesting too deep in _flatten");
|
||||
return 0;
|
||||
} else if (PyList_Check(item)) {
|
||||
size = PyList_GET_SIZE(item);
|
||||
/* preallocate (assume no nesting) */
|
||||
if (context->size + size > context->maxsize && !_bump(context, size))
|
||||
if (context->size + size > context->maxsize &&
|
||||
!_bump(context, size))
|
||||
return 0;
|
||||
/* copy items to output tuple */
|
||||
for (i = 0; i < size; i++) {
|
||||
|
@ -1836,16 +1873,19 @@ _flatten1(FlattenContext* context, PyObject* item, int depth)
|
|||
if (!_flatten1(context, o, depth + 1))
|
||||
return 0;
|
||||
} else if (o != Py_None) {
|
||||
if (context->size + 1 > context->maxsize && !_bump(context, 1))
|
||||
if (context->size + 1 > context->maxsize &&
|
||||
!_bump(context, 1))
|
||||
return 0;
|
||||
Py_INCREF(o);
|
||||
PyTuple_SET_ITEM(context->tuple, context->size++, o);
|
||||
PyTuple_SET_ITEM(context->tuple,
|
||||
context->size++, o);
|
||||
}
|
||||
}
|
||||
} else if (PyTuple_Check(item)) {
|
||||
/* same, for tuples */
|
||||
size = PyTuple_GET_SIZE(item);
|
||||
if (context->size + size > context->maxsize && !_bump(context, size))
|
||||
if (context->size + size > context->maxsize &&
|
||||
!_bump(context, size))
|
||||
return 0;
|
||||
for (i = 0; i < size; i++) {
|
||||
PyObject *o = PyTuple_GET_ITEM(item, i);
|
||||
|
@ -1853,10 +1893,12 @@ _flatten1(FlattenContext* context, PyObject* item, int depth)
|
|||
if (!_flatten1(context, o, depth + 1))
|
||||
return 0;
|
||||
} else if (o != Py_None) {
|
||||
if (context->size + 1 > context->maxsize && !_bump(context, 1))
|
||||
if (context->size + 1 > context->maxsize &&
|
||||
!_bump(context, 1))
|
||||
return 0;
|
||||
Py_INCREF(o);
|
||||
PyTuple_SET_ITEM(context->tuple, context->size++, o);
|
||||
PyTuple_SET_ITEM(context->tuple,
|
||||
context->size++, o);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue