[Python-Dev] Adding support to curses library (original) (raw)

Ulrich Berning ulrich.berning at denviso.de
Wed Feb 25 09:48:18 CET 2009


Heracles wrote:

Hello,

I am working on a patch to add to the cursesmodule.c file of the Python core libraries. I figured I would take on one of the implemented functions to try to get my feet wet contributing to the project. At any rate, I have the following function defined in the 2.7.a version updated from SVN this morning: ------------- Snippet --------------------------- // Insert new method colorset Steve Owens 2/24/2009 // The curses library colorset function has the following signature: // int colorset(short colorpairnumber, void* opts); static PyObject * PyCursescolorset(PyObject *self, PyObject *args) { short colorpairnumber; void * opts; int erg; // These macros ought to be documented in the API docs // but they aren't yet. PyCursesInitialised PyCursesInitialisedColor // Per ncurses Man Page: // The routine colorset sets the current color of the given window to // the foreground/background combination described by the colorpairnumber. // The parameter opts is reserved for future use, applications must supply a // null pointer. switch(PyTupleSize(args)) { case 1: // Dont make them pass a useless null pointer. if (!PyArgParseTuple(args, "h", &colorpairnumber)) return NULL; break; case 2: // Allow them to pass the opts pointer so that when ncurses is later updated. // This method will still work. if (!PyArgParseTuple(args, "hO&", &colorpairnumber, &opts)) return NULL; break; default: PyErrSetString(PyExcTypeError, "colorset requires 1 or 2 arguments (colorpairnumber[, opts]?)"); return NULL; } erg = colorset(colorpairnumber, opts); // Debating on forcing null here. if (erg == ERR) return PyCursesCheckERR(erg, "colorset"); else PyIntFromLong((long) 1L); } -------------End Snippet --------------------------- I also have the following added in (see last line of the snippet): ------------- Snippet --------------------------- static PyMethodDef PyCursesmethods[] = { {"baudrate", (PyCFunction)PyCursesbaudrate, METHNOARGS}, {"beep", (PyCFunction)PyCursesbeep, METHNOARGS}, {"canchangecolor", (PyCFunction)PyCursescanchangecolor, METHNOARGS}, {"cbreak", (PyCFunction)PyCursescbreak, METHVARARGS}, {"colorcontent", (PyCFunction)PyCursesColorContent, METHVARARGS}, {"colorpair", (PyCFunction)PyCursescolorpair, METHVARARGS}, {"colorset", (PyCFunction)PyCursescolorset, METHVARARGS}, -------------End Snippet --------------------------- The code compiles and installs fine, but when I run the following unit test, I get a segmentation fault: ------------- Snippet --------------------------- import unittest, curses from test import testsupport def testCursesColorSet(stdscrn): curses.initpair(1, curses.COLORRED, curses.COLORWHITE) curses.initpair(2, curses.COLORWHITE, curses.COLORBLUE); i = curses.colorset(1, NULL); stdscrn.addstr("RED/BLACK (%0)\n".format(i)) i = curses.colorset(2, NULL); stdscrn.print("WHITE/BLUE (%0)\n".format(i)) i = curses.colorset(0, NULL); stdscrn.print("Default (%0)\n".format(i))

def testmain(stdscrn): curses.savetty() if curses.hascolor(): testCursesColorSet(stdscrn) else stdscr.addstr( "Test Aborted: Color not supported on this terminal.") if name == 'main': curses.wrapper(testmain) -------------End Snippet --------------------------- It turns out that by commenting out this line in the cursesmodule.c code, allows the unit test to run obviously reporting the error as expected: ------------- Snippet --------------------------- //erg = colorset(colorpairnumber, opts); // Debating on forcing null here. -------------End Snippet --------------------------- At any rate I am stuck. I am still trying to build just a plain C file which will test the colorset function outside of python, but that is another task. Any suggestions?

As long as Python is written in C, please don't use C++ comments, some C compilers don't like them.

Ulli



More information about the Python-Dev mailing list