ctypes: catch system exit from C library

M

Martin Landa

Hi all,

in my python application I am calling functions from a C library via
`ctypes` interface. Some fns from that C library calls `exit()` on
error. It causes that the python application crashes even without any
notification. Is it possible to catch library system exit calls from
such python application?

Thanks in advance, Martin
 
U

Ulrich Eckhardt

Am 03.11.2011 11:07, schrieb Martin Landa:
in my python application I am calling functions from a C library via
`ctypes` interface. Some fns from that C library calls `exit()` on
error.

Just curious, which library is that?

Uli
 
G

Grant Edwards

Am 03.11.2011 11:07, schrieb Martin Landa:

Just curious, which library is that?

And who should we have thrashed and pilloried for writing it that way?
 
N

Nobody

I'm reasonably sure that he's talking about the GRASS libraries.

In which, the standard error-handling mechanism is to call
G_fatal_error(), which ultimately calls exit(). AKA the "samurai
principle": return victorious, or don't return at all.

[FWIW, re-writing everying in C++ and using exceptions is not a
realistic option].
And who should we have thrashed and pilloried for writing it that way?

No-one. It's a perfectly rational design given the context.

GRASS consists of ~350 command-line programs ("modules") in the
traditional Unix style (read command-line arguments, do processing,
terminate). The libraries exist to support the creation of such modules,
and are fit for that particular purpose.

Handling errors by having status codes which propagate up the call chain
would add a significant amount of code. More importantly, it would push
the burden of handling errors onto the authors of the various modules (and
historical evidence suggests that checking for (let alone handling) errors
is unlikely to occur).

The mechanism works fine, so long as you don't try to use the libraries
for purposes for which they weren't designed (e.g. long-running programs
such as interactive applications or servers).

As the story goes ...

Patient: "Doctor, when I do this ... it hurts"
Doctor: "Well don't do it then!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top