Silly brackets (Was: Re: Problems extending Python with C.)

Steinar Knutsen sk at nvg.unit.no
Sun Feb 20 01:56:14 CET 2000


The problem described below was caused by my misplacing a bracket in
the C-part of the code. As my Python-trained brain only looked at the
indentation, the error was easily missed. Remind me to flame the next
guy who claims indentation is brittle and brackets are robust. :)

In article <Pine.NEB.4.05.10002192231120.6393-100000 at nepe.nvg.ntnu.no>,
Steinar Knutsen  <sk at nvg.ntnu.no> wrote:
>I do not understand why I only can use the function ncmp as defined below
>properly in the sort method of a list when I put a Python function as a
>wrapper around it.
>
>Or in other words:
>import numcompare
>b = somefunctiongeneratingalistofstrings()
>b.sort(numcompare.ncmp)
># b does not get sorted
>
>Whereas
>import numcompare
>b = somefunctiongeneratingalistofstrings()
>def NCMP(x,y):
>   return numcompare.ncmp(x,y)
>b.sort(NCMP)
># b is sorted correctly

For a rather... liberal definition of correctness...

>C-source for module:
>------------------------------------------------------------------------
>#include <string.h>
>#include "/usr/local/include/python1.5/Python.h"
>
>static PyObject * ncmp(PyObject *self, PyObject *args)
>{
>   const char* x;
>   const char* y;
>   long cc;
>
>   if (!PyArg_ParseTuple(args, "ss", &x, &y)) {
>      return NULL;
>   }
>   else {
>      cc = numcompare(x, y);
>      if (cc > 0) {
>         return Py_BuildValue("i", 1);
>      }
>      else if ( cc == 0 ) {
>         return Py_BuildValue("i", 0);
>      }
>      else {
>         return Py_BuildValue("i", -1);
>      }
>   }
>}
>
>int numcompare(char* x, char* y)
>{
>   int i0 = -1, i1 = -1;
>   char* endx;
>   char* endy;
>   int lenx, leny;
>   long X, Y;
>
>   lenx = strlen(x)-1;
>   leny = strlen(y)-1;
>   while ( i0 < lenx && i1 < leny ) {
>      i0++;
>      i1++;
>      if ( ( x[i0] >= '0' && x[i0] <= '9' ) &&
>           ( y[i1] >= '0' && y[i1] <= '9' ) ) {
>         X = strtol(x+i0, &endx, 10);
>         Y = strtol(y+i1, &endy, 10);
>         }
>         if ( X != Y ) {
>            return X-Y;
>         }
>         else {
>            /* -1 on cause of increment in start of while-loop */
>            i0 = (int)(endx - x) - 1;
>            i1 = (int)(endy - y) - 1;
>            continue;
>         }

And here a } is missing...

>      if ( x[i0] != y[i1] ) {
>         return x[i0]-y[i1];
>      }
>   }
>   return strcmp(x, y);
>}
>
>static struct PyMethodDef functions[] = {
>   { "ncmp", ncmp, 1},
>   { NULL, NULL}
>};
>
>void initnumcompare()
>{
>   (void)Py_InitModule("numcompare", functions);
>}
>-----------------------------------------------------------------------
>
>I guess I'm missing something very obvious here, but I just don't get
>what. The Python is 1.5.2, the compiler is GCC 2.7.2.2, the OS is NetBSD
>1.3.3. (Yeah, I know I should upgrade, but the system works. :) )
>
>Thanks for any and all help.
-- 
Steinar



More information about the Python-list mailing list