[Python-Dev] Adding support to curses library
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 color_set Steve Owens 2/24/2009
> // The curses library color_set function has the following signature:
> // int color_set(short color_pair_number, void* opts);
> static PyObject *
> PyCurses_color_set(PyObject *self, PyObject *args)
> {
> short color_pair_number;
> 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 color_set sets the current color of the given window to
> // the foreground/background combination described by the
> color_pair_number.
> // The parameter opts is reserved for future use, applications must
> supply a
> // null pointer.
> switch(PyTuple_Size(args))
> {
> case 1:
> // Dont make them pass a useless null pointer.
> if (!PyArg_ParseTuple(args, "h", &color_pair_number)) 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 (!PyArg_ParseTuple(args, "hO&", &color_pair_number, &opts)) return
> NULL;
> break;
> default:
> PyErr_SetString(PyExc_TypeError, "color_set requires 1 or 2 arguments
> (color_pair_number[, opts]?)");
> return NULL;
> }
>
> erg = color_set(color_pair_number, opts); // Debating on forcing null
> here.
>
> if (erg == ERR)
> return PyCursesCheckERR(erg, "color_set");
> else
> PyInt_FromLong((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 PyCurses_methods[] = {
> {"baudrate", (PyCFunction)PyCurses_baudrate, METH_NOARGS},
> {"beep", (PyCFunction)PyCurses_beep, METH_NOARGS},
> {"can_change_color", (PyCFunction)PyCurses_can_change_color,
> METH_NOARGS},
> {"cbreak", (PyCFunction)PyCurses_cbreak, METH_VARARGS},
> {"color_content", (PyCFunction)PyCurses_Color_Content,
> METH_VARARGS},
> {"color_pair", (PyCFunction)PyCurses_color_pair, METH_VARARGS},
> {"color_set", (PyCFunction)PyCurses_color_set, METH_VARARGS},
> -------------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 test_support
>
> def testCursesColorSet(stdscrn):
> curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
> curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE);
> i = curses.color_set(1, NULL);
> stdscrn.addstr("RED/BLACK (%0)\n".format(i))
> i = curses.color_set(2, NULL);
> stdscrn.print("WHITE/BLUE (%0)\n".format(i))
> i = curses.color_set(0, NULL);
> stdscrn.print("Default (%0)\n".format(i))
>
>
> def test_main(stdscrn):
> curses.savetty()
> if curses.has_color():
> testCursesColorSet(stdscrn)
> else
> stdscr.addstr( "Test Aborted: Color not supported on this terminal.")
>
>
> if __name__ == '__main__':
> curses.wrapper(test_main)
> -------------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 = color_set(color_pair_number, 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 color_set 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