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

Discussion in 'Python' started by Bo Peng, Feb 24, 2006.

  1. Bo Peng

    Bo Peng Guest

    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 Peng, Feb 24, 2006
    1. Advertisements

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

    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 B. Roggisch, Feb 24, 2006
    1. Advertisements

  3. 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 Dittmar, Feb 24, 2006
  4. Bo Peng

    Bo Peng Guest

    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 Peng, Feb 25, 2006
  5. Daniel Dittmar, Feb 25, 2006
  6. Bo Peng

    fraca7 Guest

    Bo Peng a écrit :
    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.
    fraca7, Mar 3, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.