Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:32:44+00:00 2026-06-10T09:32:44+00:00

I have a problem trying to abort a Python script, for example: #!/usr/bin/python import

  • 0

I have a problem trying to abort a Python script, for example:

#!/usr/bin/python

import hello

hello.draw()

exit()

where draw() is a C function. In this function there is a loop to print some text. I tried CTRL-C to break out of it but it doesn’t work.
Important thing is that C code cannot be modified!

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:32:46+00:00Added an answer on June 10, 2026 at 9:32 am

    I put together an example of how you might do this. It lets to augment the function and escape from it without needing to modify it. For example, given the function foo (which was inline in test.h for my testing):

    void foo() {
      while(1); // never returns
    }
    

    You can play a game with signals and siglongjmp. It should be noted however that you need to be very careful to ensure you only call async safe functions from within foo, or alternatively mask the signals before and unmask them after any unsafe calls.

    We can then wrap the function and use %exception to inject some extra code to allow the signal to backout of the function:

    %module test
    
    %{
    #include <setjmp.h>
    #include <signal.h>
    
    static sigjmp_buf timeout;
    
    static void backout(int sig) {
      siglongjmp(timeout, sig);
    }
    
    #include "test.h"
    %}
    
    %include <exception.i>
    
    %exception {
      if (!sigsetjmp(timeout, 1)) {
        signal(SIGALRM,backout); // Check return?
        $action
      }
      else {
        // raise a Python exception
        SWIG_exception(SWIG_RuntimeError, "Timeout in $decl");
      }
    }
    
    void foo();
    
    %exception;
    

    Which allows us to write:

    import signal
    import test
    
    signal.alarm(5)
    test.foo()
    

    Which works as hoped, given the caveats about async-safe functions. I used SIGALRM here because it allowed me to keep it all self-contained within one Python file. You could use another thread/process and other signals if you preferred.

    If you change SIGALRM in my example to SIGINT (and don’t raise a SIGALRM either obviously) it’ll work with Ctrl+C – the reason it doesn’t is because of how the default Python signal handlers work. The default handler Python uses simply sets a flag and processes the signal after the underlying call has returned control to Python, which neatly side-steps the whole async-safety issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem when trying to run hello world program using android SDK.
I have a weird, annoying problem with Python 2.6. I'm trying to run this
I have a problem trying to turn my python code into an executable using
I have a problem when trying to execute this update statement (below) using C#
I'm trying to work with sockets and I have such problem In code example:
I'm new to python, and I have the following problem: I am trying to
I have to solve this little problem in Python/Django and I'm wondering which is
I have a problem trying to register my own Event/Listener to the event dispatcher.
I have a problem trying to design some generic storage.. Basically I have the
I have a problem trying to edit. I work with Areas for better management

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.