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

Neal Norwitz nnorwitz at gmail.com
Wed Feb 25 06:41:10 CET 2009


On Tue, Feb 24, 2009 at 2:18 PM, Heracles <steve at integrityintegrators.net> 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:

I'm glad you are interested in developing Python. I'm not sure if this is the best forum. OTOH, I'm not sure if comp.lang.python would be appropriate either.

I'd suggest making a proper patch and submitting it to http://bugs.python.org

------------- 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);

I did a cursory review of the patch and if this is the exact code, this is a problem. You are missing a return statement. The compiler should have issued a warning for this too.

} -------------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?

Beyond what I said above, typically you need to go the next step. Fire up a debugger and determine exactly where and why it's crashing.

Good luck!

n



More information about the Python-Dev mailing list