diff options
-rw-r--r-- | Modules/_cursesmodule.c | 146 |
1 files changed, 47 insertions, 99 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 79c34e7..4c4b4aa 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -95,23 +95,19 @@ Form extension (ncurses and probably SYSV): /* Release Number */ -char *PyCursesVersion = "1.6"; +char *PyCursesVersion = "2.1"; /* Includes */ #include "Python.h" +#define CURSES_MODULE +#include "py_curses.h" #ifdef __osf__ #define _XOPEN_SOURCE_EXTENDED /* Define macro for OSF/1 */ #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */ #endif -#ifdef HAVE_NCURSES_H -#include <ncurses.h> -#else -#include <curses.h> -#endif - /* These prototypes are in <term.h>, but including this header #defines many common symbols (such as "lines") which breaks the curses module in other ways. So the code will just specify @@ -130,46 +126,40 @@ typedef chtype attr_t; /* No attr_t type is available */ static PyObject *PyCursesError; -/* general error messages */ -static char *catchall_ERR = "curses function returned ERR"; -static char *catchall_NULL = "curses function returned NULL"; - /* Tells whether setupterm() has been called to initialise terminfo. */ static int initialised_setupterm = FALSE; /* Tells whether initscr() has been called to initialise curses. */ static int initialised = FALSE; -/* Tells whether start_color() has been called to initialise colorusage. */ +/* Tells whether start_color() has been called to initialise color usage. */ static int initialisedcolors = FALSE; /* Utility Macros */ -#define ARG_COUNT(X) \ - (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1)) - #define PyCursesSetupTermCalled \ if (initialised_setupterm != TRUE) { \ PyErr_SetString(PyCursesError, \ "must call (at least) setupterm() first"); \ - return NULL; } + return 0; } #define PyCursesInitialised \ if (initialised != TRUE) { \ PyErr_SetString(PyCursesError, \ "must call initscr() first"); \ - return NULL; } + return 0; } #define PyCursesInitialisedColor \ if (initialisedcolors != TRUE) { \ PyErr_SetString(PyCursesError, \ "must call start_color() first"); \ - return NULL; } + return 0; } /* Utility Functions */ /* * Check the return code from a curses function and return None - * or raise an exception as appropriate. + * or raise an exception as appropriate. These are exported using the + * CObject API. */ static PyObject * @@ -202,22 +192,36 @@ PyCurses_ConvertToChtype(PyObject *obj, chtype *ch) return 1; } +/* Function versions of the 3 functions for tested whether curses has been + initialised or not. */ + +static int func_PyCursesSetupTermCalled(void) +{ + PyCursesSetupTermCalled; + return 1; +} + +static int func_PyCursesInitialised(void) +{ + PyCursesInitialised; + return 1; +} + +static int func_PyCursesInitialisedColor(void) +{ + PyCursesInitialisedColor; + return 1; +} + /***************************************************************************** The Window Object ******************************************************************************/ -/* Definition of the window object and window type */ - -typedef struct { - PyObject_HEAD - WINDOW *win; -} PyCursesWindowObject; +/* Definition of the window type */ PyTypeObject PyCursesWindow_Type; -#define PyCursesWindow_Check(v) ((v)->ob_type == &PyCursesWindow_Type) - -/* Function Prototype Macros - They are ugly but very, very useful. ;-) +/* Function prototype macros for Window object X - function name TYPE - parameter Type @@ -1458,75 +1462,6 @@ PyTypeObject PyCursesWindow_Type = { Global Functions **********************************************************************/ -static PyObject *ModDict; - -/* Function Prototype Macros - They are ugly but very, very useful. ;-) - - X - function name - TYPE - parameter Type - ERGSTR - format string for construction of the return value - PARSESTR - format string for argument parsing - */ - -#define NoArgNoReturnFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - PyCursesInitialised \ - if (!PyArg_NoArgs(args)) return NULL; \ - return PyCursesCheckERR(X(), # X); } - -#define NoArgOrFlagNoReturnFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - int flag = 0; \ - PyCursesInitialised \ - switch(ARG_COUNT(args)) { \ - case 0: \ - return PyCursesCheckERR(X(), # X); \ - case 1: \ - if (!PyArg_Parse(args, "i;True(1) or False(0)", &flag)) return NULL; \ - if (flag) return PyCursesCheckERR(X(), # X); \ - else return PyCursesCheckERR(no ## X (), # X); \ - default: \ - PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \ - return NULL; } } - -#define NoArgReturnIntFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - PyCursesInitialised \ - if (!PyArg_NoArgs(args)) return NULL; \ - return PyInt_FromLong((long) X()); } - - -#define NoArgReturnStringFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - PyCursesInitialised \ - if (!PyArg_NoArgs(args)) return NULL; \ - return PyString_FromString(X()); } - -#define NoArgTrueFalseFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - PyCursesInitialised \ - if (!PyArg_NoArgs(args)) return NULL; \ - if (X () == FALSE) { \ - Py_INCREF(Py_False); \ - return Py_False; \ - } \ - Py_INCREF(Py_True); \ - return Py_True; } - -#define NoArgNoReturnVoidFunction(X) \ -static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \ -{ \ - PyCursesInitialised \ - if (!PyArg_NoArgs(args)) return NULL; \ - X(); \ - Py_INCREF(Py_None); \ - return Py_None; } - NoArgNoReturnFunction(beep) NoArgNoReturnFunction(def_prog_mode) NoArgNoReturnFunction(def_shell_mode) @@ -1805,6 +1740,8 @@ PyCurses_Init_Pair(PyObject *self, PyObject *args) return PyCursesCheckERR(init_pair(pair, f, b), "init_pair"); } +static PyObject *ModDict; + static PyObject * PyCurses_InitScr(PyObject *self, PyObject *args) { @@ -2497,14 +2434,25 @@ static PyMethodDef PyCurses_methods[] = { void init_curses(void) { - PyObject *m, *d, *v; + PyObject *m, *d, *v, *c_api_object; + static void *PyCurses_API[PyCurses_API_pointers]; + + /* Initialize the C API pointer array */ + PyCurses_API[0] = (void *)&PyCursesWindow_Type; + PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled; + PyCurses_API[2] = (void *)func_PyCursesInitialised; + PyCurses_API[3] = (void *)func_PyCursesInitialisedColor; /* Create the module and add the functions */ m = Py_InitModule("_curses", PyCurses_methods); /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); - ModDict = d; /* For PyCurses_InitScr */ + ModDict = d; /* For PyCurses_InitScr to use later */ + + /* Add a CObject for the C API */ + c_api_object = PyCObject_FromVoidPtr((void *)PyCurses_API, NULL); + PyDict_SetItemString(d, "_C_API", c_api_object); /* For exception curses.error */ PyCursesError = PyErr_NewException("_curses.error", NULL, NULL); |