Module written in C does not repond to Ctrl-C interruption.

B

Bo Peng

Dear list,

I have not done a thorough test, but it occurs to me that

1. python code can be interrupted by Ctrl-C.
2. A C module, if I add a main() function and run independently, can be
interrupted by Ctrl-C.
3. If I load the C module in python and run, the program will not
respond to Ctrl-C interruption. I have to kill python explicitly.

If this is a known behavior or just a special case of mine? Any fix to
it? I am using python 2.3.4 under Redhat EL4 with gcc 3.3.4.

Many thanks in advance.
Bo
 
D

Diez B. Roggisch

Bo said:
Dear list,

I have not done a thorough test, but it occurs to me that

1. python code can be interrupted by Ctrl-C.
2. A C module, if I add a main() function and run independently, can be
interrupted by Ctrl-C.
3. If I load the C module in python and run, the program will not
respond to Ctrl-C interruption. I have to kill python explicitly.

If this is a known behavior or just a special case of mine? Any fix to
it? I am using python 2.3.4 under Redhat EL4 with gcc 3.3.4.

I fear it is a known behavior and not easy to fix. See the module signal
docs:

"""
Although Python signal handlers are called asynchronously as far as the
Python user is concerned, they can only occur between the ``atomic''
instructions of the Python interpreter. This means that signals arriving
during long calculations implemented purely in C (such as regular
expression matches on large bodies of text) may be delayed for an arbitrary
amount of time.
"""

So - no workaround here, unless you patch python.

Diez
 
D

Daniel Dittmar

Diez said:
"""
Although Python signal handlers are called asynchronously as far as the
Python user is concerned, they can only occur between the ``atomic''
instructions of the Python interpreter. This means that signals arriving
during long calculations implemented purely in C (such as regular
expression matches on large bodies of text) may be delayed for an arbitrary
amount of time.
"""

So - no workaround here, unless you patch python.

You could set up your own signal handler when entering the C extension.
This should abort the extension (tricky) and call the Python signal handler.

Daniel
 
B

Bo Peng

Daniel said:
You could set up your own signal handler when entering the C extension.
This should abort the extension (tricky) and call the Python signal
handler.

This can be done under linux using things in signal.h but I am not
sure whether or not there is a portable way to do it (max, win32).
Does anybody know a quick way under windows?

Bo
 
F

fraca7

Bo Peng a écrit :
Dear list,

I have not done a thorough test, but it occurs to me that

1. python code can be interrupted by Ctrl-C.
2. A C module, if I add a main() function and run independently, can be
interrupted by Ctrl-C.
3. If I load the C module in python and run, the program will not
respond to Ctrl-C interruption. I have to kill python explicitly.

If this is a known behavior or just a special case of mine? Any fix to
it? I am using python 2.3.4 under Redhat EL4 with gcc 3.3.4.

You may want to call intrcheck() or PyErr_CheckSignals in your C code
and react accordingly.

From what I can see, PyErr_CheckSignals() returns 0 if no interrupt
occurred and -1 (setting a KeyboardInterrupt exception) if one did.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top