Bill Allombert on Wed, 06 Apr 2011 13:37:55 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Trapping errors in library mode


On Wed, Apr 06, 2011 at 12:07:40PM +0200, Dirk Laurie wrote:
> When calling the Pari library from C, I don't want my program
> to exit when I do something silly like dividing by zero.  So
> I initialize not by pari_init but by pari_init_opts, omitting
> INIT_JMPm from the list of options.  This works, in the sense
> that I no longer get 
> 
>   ***   division by zero
>   ***   Error in the PARI system. End of program.
> 
> Instead, I get (under Linux)
> 
>   ***   division by zero
> Segmentation fault
> 
> which not everybody will agree is much nicer.  
> 
> The libpari documentation has nothing else on INIT_JMPm.
> So I peeked in the source code.  I _think_ I should write
> a routine headed 
>     int my_exception_handler(long numerr)
> using gp_exception_handler as a model, and say 
>     default_exception_handler = my_exception_handler;
> 
> However, exploiting undocumented features by reverse-engineering
> code is a _bad_ idea, so I'm asking here whether this is the
> canonical way.

I suppose you are using PARI 2.3. In that case, the proper way is to set
INIT_JMPm and to call setjmp(GP_DATA->env) somewhere in your code to initialize
GP_DATA->env. In case of an error, PARI will do longjmp(GP_DATA->env).
default_exception_handler() does not allow by itself to recover from an error.

In PARI 2.4.3 we have much improved the interface:
We provided a documented call-back cb_pari_err_recover() that is called
instead of doing longjmp(GP_DATA->env).

Cheers,
Bill.