Sounds like the thing to do here is signal handling. I'm not an  
expert at it myself, but I know it can be done because we use it a  
lot in Sage.

- Robert

On Jul 11, 2009, at 8:06 PM, David Simmons-Duffin wrote:

> I'm still working on wrapping the computer algebra package LiE in
> cython.  For those unfamiliar, LiE is basically it's own language,
> written in C.  And I'm interested in calling some of the math-heavy
> functions, but bypassing the interpreter.
>
> Error checking is sprinkled throughout the LiE source, for instance:
>
> matrix *mat_mul_mat_mat(matrix *a, matrix *b) {
>      if (a->ncols != b->nrows)
>          error("Number columns arg1 unequal number of rows arg2 .\n");
>      return Matmult(a, b);
> }
>
> When an error is found, LiE calls the error() function, which prints
> out a traceback, cleans up a little bit, and finally does a longjump
> to "envbuf," which is at the beginning of the interpreter loop:
>
> void error(char *format, ...)
> {
>      symblst         list;
>      extern jmp_buf  envbuf;
>      va_list ap;
>      va_start(ap, format);
>      vfprintf(stderr, format, ap);
>      if (am_monitor) vfprintf(monfile, format, ap);
>      if (label->name)
>      {
>           boolean printin = (label->name != seq_name);
>           if (printin)
>              Printf("(in %s",name_tab[label->name]);
>           if (no_terminal(cur_in) || strcmp(label->fname, "stdin")! 
> =0) {
>              if (!printin) Printf("(");
>              Printf(" at line %d of file %s)\n",label->line, label-
>> fname);
>           }
>           else
>              if (printin) Printf(")\n");
>      }
>      if (fun_name)
>          Printf("[in function %s ]\n", name_tab[fun_name]);
>      for (list = top_definitions; list!=NULL; list = list->next)
>          /* Recover symbol table */
>      {   if (list->class == FUNCTION_COPIED) list->class = FUNCTION;
>       if (list->next && list->next->class == DUMMY)
>              list->next = list->next->next;      /* Remove sym */
>      }
>      if (repair_obj) {
>          setshared(repair_obj);
>          repair_obj = (object) NULL;
>      }
>      if (cur_in==stdin)
>        clear_input();
>      else
>        do exit_input_file(parsing); while (cur_in!=stdin); /* pop
> input files */
>      longjmp(envbuf, -1);
> }
>
> Instead of doing all this stuff, and longjumping to the LiE
> interpreter, I'd like to raise a python exception that could be caught
> by whatever cython function called "mat_mul_mat_mat."  Does anyone
> have any advice on how to do this?  I took a look at the python
> exception portion of the C/API.  It seems like I might want to make a
> C file with my own error function:
>
> error(char* format, ...) {
>      turn format into a string
>      PyErr_SetString(errorstring)
>      longjump somewhere ???
> }
>
> and then comment out the error function in the LiE source... but I'm
> not totally sure how to put it all together.  Also, If it's possible,
> I'd like to avoid modifying the LiE source too much -- i.e. commenting
> out an entire function.  I'm not a C master, so I wouldn't be
> surprised if I've missed an obvious way to do this.
>
> Thanks for the advice!
>
> David
>
>
>
>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to