diff -Naur libieee1284-0.2.11/src/ieee1284module.c libieee1284-0.2.11.new/src/ieee1284module.c --- libieee1284-0.2.11/src/ieee1284module.c 2004-02-03 12:50:57.000000000 +0100 +++ libieee1284-0.2.11.new/src/ieee1284module.c 2022-12-10 18:40:44.241491747 +0100 @@ -28,6 +28,17 @@ struct parport *port; } ParportObject; +static PyObject * +Parport_new (PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + ParportObject *self; + self = (ParportObject *) type->tp_alloc (type, 0); + if (self != NULL) + self->port = NULL; + + return (PyObject *) self; +} + static int Parport_init (ParportObject *self, PyObject *args, PyObject *kwds) { @@ -41,33 +52,32 @@ { if (self->port) ieee1284_unref (self->port); - - self->ob_type->tp_free ((PyObject *) self); + Py_TYPE(self)->tp_free((PyObject *) self); } static PyObject * Parport_getname (ParportObject *self, void *closure) { - return PyString_FromString (self->port->name); + return PyBytes_FromString (self->port->name); } static PyObject * Parport_getbase_addr (ParportObject *self, void *closure) { - return PyInt_FromLong (self->port->base_addr); + return PyLong_FromLong (self->port->base_addr); } static PyObject * Parport_gethibase_addr (ParportObject *self, void *closure) { - return PyInt_FromLong (self->port->hibase_addr); + return PyLong_FromLong (self->port->hibase_addr); } static PyObject * Parport_getfilename (ParportObject *self, void *closure) { if (self->port->filename) - return PyString_FromString (self->port->filename); + return PyBytes_FromString (self->port->filename); Py_INCREF (Py_None); return Py_None; @@ -157,7 +167,7 @@ return NULL; } - return PyString_FromStringAndSize (buffer, r); + return PyBytes_FromStringAndSize (buffer, r); } static PyObject * @@ -175,7 +185,34 @@ return NULL; } - return PyInt_FromLong (capabilities); + return PyLong_FromLong (capabilities); +} + +static PyObject * +Parport_get_irq_fd (ParportObject *self) +{ + int fd = ieee1284_get_irq_fd (self->port); + if (fd < 0) { + handle_error (fd); + return NULL; + } + + return PyLong_FromLong (fd); +} + +static PyObject * +Parport_clear_irq (ParportObject *self) +{ + int portcount = 0; + int r; + + int fd = ieee1284_clear_irq (self->port, &portcount); + if (r < 0) { + handle_error (r); + return NULL; + } + + return PyLong_FromLong (portcount); } static PyObject * @@ -215,14 +252,13 @@ static PyObject * Parport_read_data (ParportObject *self) { - unsigned char b[2]; int r = ieee1284_read_data (self->port); if (r < 0) { handle_error (r); return NULL; } - return PyInt_FromLong (r); + return PyLong_FromLong (r); } static PyObject * @@ -258,14 +294,13 @@ static PyObject * Parport_read_status (ParportObject *self) { - unsigned char b[2]; int r = ieee1284_read_status (self->port); if (r < 0) { handle_error (r); return NULL; } - return PyInt_FromLong (r); + return PyLong_FromLong (r); } static PyObject * @@ -293,14 +328,13 @@ static PyObject * Parport_read_control (ParportObject *self) { - unsigned char b[2]; int r = ieee1284_read_control (self->port); if (r < 0) { handle_error (r); return NULL; } - return PyInt_FromLong (r); + return PyLong_FromLong (r); } static PyObject * @@ -417,7 +451,7 @@ return NULL; \ } \ \ - ret = PyString_FromStringAndSize (buffer, got); \ + ret = PyBytes_FromStringAndSize (buffer, got); \ free (buffer); \ return ret; \ } @@ -435,7 +469,6 @@ int len; \ char *buffer; \ ssize_t wrote; \ - PyObject *ret; \ \ if (!PyArg_ParseTuple (args, "s#|i", &buffer, &len, &flags)) \ return NULL; \ @@ -446,7 +479,7 @@ return NULL; \ } \ \ - return PyInt_FromLong (wrote); \ + return PyLong_FromLong (wrote); \ } #define WRITE_METHOD(x) \ @@ -477,6 +510,12 @@ { "close", (PyCFunction) Parport_close, METH_NOARGS, "close() -> None\n" "Closes a port." }, + { "get_irq_fd", (PyCFunction) Parport_get_irq_fd, METH_VARARGS, + "get_irq_fd() -> int\n" + "Returns a pollable IRQ file descriptor." }, + { "clear_irq", (PyCFunction) Parport_clear_irq, METH_NOARGS, + "clear_irq(portcount) -> int\n" + "Clears IRQ and returns number of IRQs raised." }, { "claim", (PyCFunction) Parport_claim, METH_NOARGS, "claim() -> None\n" "Claims a port." }, @@ -541,27 +580,53 @@ static PyTypeObject ParportType = { PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ - "ieee1284.Parport", /* tp_name */ - sizeof (ParportObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Parport_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - "parallel port object", /* tp_doc */ + "ieee1284.Parport", /* const char *tp_name; */ + sizeof (ParportObject), /* Py_ssize_t tp_basicsize */ + 0, /* Py_ssize_t tp_itemsize; */ + (destructor)Parport_dealloc, /* destructor tp_dealloc; */ + 0, /* printfunc tp_print; */ + 0, /* getattrfunc tp_getattr; */ + 0, /* setattrfunc tp_setattr; */ + 0, /* PyAsyncMethods *tp_as_async; */ + 0, /* reprfunc tp_repr; */ + 0, /* PyNumberMethods *tp_as_number; */ + 0, /* PySequenceMethods *tp_as_sequence; */ + 0, /* PyMappingMethods *tp_as_mapping; */ + 0, /* hashfunc tp_hash; */ + 0, /* ternaryfunc tp_call; */ + 0, /* reprfunc tp_str; */ + 0, /* getattrofunc tp_getattro; */ + 0, /* setattrofunc tp_setattro; */ + 0, /* PyBufferProcs *tp_as_buffer; */ + Py_TPFLAGS_DEFAULT, /* unsigned long tp_flags; */ + "parallel port object", /* const char *tp_doc; */ + 0, /* traverseproc tp_traverse; */ + 0, /* inquiry tp_clear; */ + 0, /* richcmpfunc tp_richcompare; */ + 0, /* Py_ssize_t tp_weaklistoffset; */ + 0, /* getiterfunc tp_iter; */ + 0, /* iternextfunc tp_iternext; */ + Parport_methods, /* struct PyMethodDef *tp_methods; */ + 0, /* struct PyMemberDef *tp_members; */ + Parport_getseters, /* struct PyGetSetDef *tp_getset; */ + 0, /* struct _typeobject *tp_base; */ + 0, /* PyObject *tp_dict; */ + 0, /* descrgetfunc tp_descr_get; */ + 0, /* descrsetfunc tp_descr_set; */ + 0, /* Py_ssize_t tp_dictoffset; */ + (initproc)Parport_init, /* initproc tp_init; */ + 0, /* allocfunc tp_alloc; */ + Parport_new, /* newfunc tp_new; */ + 0, /* freefunc tp_free; */ + 0, /* inquiry tp_is_gc; */ + 0, /* PyObject *tp_bases; */ + 0, /* PyObject *tp_mro; */ + 0, /* PyObject *tp_cache; */ + 0, /* PyObject *tp_subclasses; */ + 0, /* PyObject *tp_weaklist; */ + 0, /* destructor tp_del; */ + 0, /* unsigned int tp_version_tag; */ + 0 /* destructor tp_finalize; */ }; static PyObject * @@ -615,24 +680,31 @@ {NULL, NULL, 0, NULL} }; +static struct PyModuleDef Ieee1284Module = { + PyModuleDef_HEAD_INIT, + "ieee1284", + NULL, /* documentation */ + -1, + Ieee1284Methods, + NULL, + NULL, + NULL, + NULL +}; + #ifndef PyMODINIT_FUNC #define PyMODINIT_FUNC void #endif PyMODINIT_FUNC -initieee1284 (void) +PyInit_ieee1284module (void) { - PyObject *m = Py_InitModule ("ieee1284", Ieee1284Methods); + PyObject *m = PyModule_Create (&Ieee1284Module); PyObject *d = PyModule_GetDict (m); PyObject *c; - ParportType.tp_new = PyType_GenericNew; - ParportType.tp_init = (initproc) Parport_init; - ParportType.tp_getset = Parport_getseters; - ParportType.tp_methods = Parport_methods; if (PyType_Ready (&ParportType) < 0) - return; + return NULL; - Py_INCREF (&ParportType); PyModule_AddObject (m, "Parport", (PyObject *) &ParportType); pyieee1284_error = PyErr_NewException("ieee1284.error", NULL, NULL); @@ -641,7 +713,7 @@ #define CONSTANT(x) \ do { \ - c = PyInt_FromLong (x); \ + c = PyLong_FromLong (x); \ PyDict_SetItemString (d, #x, c); \ Py_DECREF (c); \ } while (0) @@ -688,4 +760,6 @@ CONSTANT (F1284_SWE); CONSTANT (F1284_RLE); CONSTANT (F1284_FASTEPP); + + return m; }