* 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)
This commit is contained in:
Guido van Rossum 1994-08-09 14:15:19 +00:00
parent 75abc6392b
commit 9bb4fd6061
1 changed files with 48 additions and 10 deletions

View File

@ -11,8 +11,10 @@
#include "modsupport.h" #include "modsupport.h"
#include "sysmodule.h" #include "sysmodule.h"
#ifndef PyObject
#define PyObject object #define PyObject object
typedef struct methodlist PyMethodDef; typedef struct methodlist PyMethodDef;
#endif
#define PyInit_tkinter inittkinter #define PyInit_tkinter inittkinter
#undef Py_True #undef Py_True
@ -785,7 +787,42 @@ FileHandler (clientData, mask)
errorInCmd = 1; errorInCmd = 1;
PyErr_GetAndClear (&excInCmd, &valInCmd); 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 * static PyObject *
@ -798,8 +835,10 @@ Tkapp_CreateFileHandler (self, args)
if (!PyArg_Parse (args, "(OiO)", &file, &mask, &func)) if (!PyArg_Parse (args, "(OiO)", &file, &mask, &func))
return NULL; return NULL;
if (!PyFile_Check (file) id = GetFileNo (file);
|| !(PyMethod_Check(func) || PyFunction_Check(func))) if (id < 0)
return NULL;
if (!(PyMethod_Check(func) || PyFunction_Check(func)))
{ {
PyErr_SetString (PyExc_TypeError, "bad argument list"); PyErr_SetString (PyExc_TypeError, "bad argument list");
return NULL; return NULL;
@ -808,7 +847,6 @@ Tkapp_CreateFileHandler (self, args)
/* ClientData is: (func, file) */ /* ClientData is: (func, file) */
data = Py_BuildValue ("(OO)", func, file); data = Py_BuildValue ("(OO)", func, file);
id = fileno (PyFile_AsFile (file));
Tk_CreateFileHandler (id, mask, FileHandler, (ClientData) data); Tk_CreateFileHandler (id, mask, FileHandler, (ClientData) data);
/* XXX fileHandlerDict */ /* XXX fileHandlerDict */
@ -821,15 +859,15 @@ Tkapp_DeleteFileHandler (self, args)
PyObject *self; PyObject *self;
PyObject *args; /* Args: file */ PyObject *args; /* Args: file */
{ {
PyObject *file;
int id; int id;
if (!PyFile_Check (args)) if (!PyArg_Parse (args, "O", &file))
{ return NULL;
PyErr_SetString (PyExc_TypeError, "bad argument list"); id = GetFileNo (file);
return NULL; if (id < 0)
} return NULL;
id = fileno (PyFile_AsFile (args));
Tk_DeleteFileHandler (id); Tk_DeleteFileHandler (id);
/* XXX fileHandlerDict */ /* XXX fileHandlerDict */
Py_INCREF (Py_None); Py_INCREF (Py_None);