ctypes: catch system exit from C library

Nobody nobody at nowhere.com
Fri Nov 4 04:26:25 EDT 2011


On Thu, 03 Nov 2011 14:16:50 +0000, Grant Edwards wrote:

>>> 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?

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!"




More information about the Python-list mailing list