diff options
author | Guido van Rossum <guido@python.org> | 1994-08-09 14:15:19 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-08-09 14:15:19 (GMT) |
commit | 9bb4fd6061a27f256c7b7c615b4d27592ea48d5d (patch) | |
tree | 77a5242472150903b223e4df3a04547cf86cfd36 /Modules | |
parent | 75abc6392bc75cd321564aa1d05d4e57c926c7a4 (diff) | |
download | cpython-9bb4fd6061a27f256c7b7c615b4d27592ea48d5d.zip cpython-9bb4fd6061a27f256c7b7c615b4d27592ea48d5d.tar.gz cpython-9bb4fd6061a27f256c7b7c615b4d27592ea48d5d.tar.bz2 |
* tkintermodule.c (*FileHandler): generalize to arbitrary file ids
and objects that hav a fileno() method; fix bug in FileHandler
(should call XDECREF instead of DECREF)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_tkinter.c | 58 |
1 files changed, 48 insertions, 10 deletions
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 9db12df..1f702d3 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -11,8 +11,10 @@ #include "modsupport.h" #include "sysmodule.h" +#ifndef PyObject #define PyObject object typedef struct methodlist PyMethodDef; +#endif #define PyInit_tkinter inittkinter #undef Py_True @@ -785,7 +787,42 @@ FileHandler (clientData, mask) errorInCmd = 1; PyErr_GetAndClear (&excInCmd, &valInCmd); } - Py_DECREF (res); + Py_XDECREF (res); +} + +static int +GetFileNo (file) + PyObject *file; /* Either an int >= 0 or an object with a + .fileno() method that returns an int >= 0 */ +{ + object *meth, *args, *res; + int id; + if (PyInt_Check(file)) { + id = PyInt_AsLong(file); + if (id < 0) + PyErr_SetString(PyExc_ValueError, "invalid file id"); + return id; + } + meth = PyObject_GetAttrString(file, "fileno"); + if (meth == NULL) + return -1; + args = PyTuple_New(0); + if (args == NULL) + return -1; + res = PyEval_CallObject(meth, args); + Py_DECREF(args); + Py_DECREF(meth); + if (res == NULL) + return -1; + if (PyInt_Check(res)) + id = PyInt_AsLong(res); + else + id = -1; + if (id < 0) + PyErr_SetString(PyExc_ValueError, + "invalid fileno() return value"); + Py_DECREF(res); + return id; } static PyObject * @@ -798,8 +835,10 @@ Tkapp_CreateFileHandler (self, args) if (!PyArg_Parse (args, "(OiO)", &file, &mask, &func)) return NULL; - if (!PyFile_Check (file) - || !(PyMethod_Check(func) || PyFunction_Check(func))) + id = GetFileNo (file); + if (id < 0) + return NULL; + if (!(PyMethod_Check(func) || PyFunction_Check(func))) { PyErr_SetString (PyExc_TypeError, "bad argument list"); return NULL; @@ -808,7 +847,6 @@ Tkapp_CreateFileHandler (self, args) /* ClientData is: (func, file) */ data = Py_BuildValue ("(OO)", func, file); - id = fileno (PyFile_AsFile (file)); Tk_CreateFileHandler (id, mask, FileHandler, (ClientData) data); /* XXX fileHandlerDict */ @@ -821,15 +859,15 @@ Tkapp_DeleteFileHandler (self, args) PyObject *self; PyObject *args; /* Args: file */ { + PyObject *file; int id; - if (!PyFile_Check (args)) - { - PyErr_SetString (PyExc_TypeError, "bad argument list"); - return NULL; - } + if (!PyArg_Parse (args, "O", &file)) + return NULL; + id = GetFileNo (file); + if (id < 0) + return NULL; - id = fileno (PyFile_AsFile (args)); Tk_DeleteFileHandler (id); /* XXX fileHandlerDict */ Py_INCREF (Py_None); |