Aborting Python from C code

Discussion in 'Python' started by Chris Angelico, Apr 29, 2011.

  1. In our sandboxed Python environment, I would like to be able to
    trigger an abort of the currently-running Python script (from a signal
    handler or another thread). Currently, I use PyErr_SetInterrupt()
    which raises KeyboardInterrupt in the script; this works for the most
    part, but can be bypassed with a script like:

    while True:
    try:
    while True:
    pass
    except:
    pass

    I want to be able to administratively order Python to stop executing.
    Is it possible to make KeyboardInterrupt non-catchable from Python?
    Alternatively, can I use Py_AddPendingCall to do this? I'd rather not
    use setjmp/longjmp if I can help it, as they seem a tad brutal, but
    I'm thinking in terms of a call that, in effect, doesn't return.

    Any ideas would be appreciated!

    Chris Angelico
     
    Chris Angelico, Apr 29, 2011
    #1
    1. Advertising

  2. On Sat, Apr 30, 2011 at 6:08 PM, Miki Tebeka <> wrote:
    >> In our sandboxed Python environment, I would like to be able to
    >> trigger an abort of the currently-running Python script (from a signal
    >> handler or another thread).

    > There's os.abort


    That core dumps the process; what I want is to force the
    PyRun_StringFlags to return. Normally PyErr_SetInterrupt will do
    exactly this (within a few instructions is fine), but the Python
    script is able to prevent that from happening, which I don't like.

    Chris Angelico
     
    Chris Angelico, Apr 30, 2011
    #2
    1. Advertising

  3. On Sat, Apr 30, 2011 at 7:29 PM, Dylan Evans <> wrote:
    > I think i see what you are trying to do but it depends on the environment
    > and your goals.
    > Generally i think you need to separate your code by forking (or perhaps you
    > have already done that?),
    > then you can run a check to see if the process died as expected. I don't
    > know though, this not much
    > information to go on, but if you are running untrusted code then you needto
    > be able to isolate and kill it.


    Well, I've gone with a slight variant on this. If the Python script
    doesn't terminate in a timely manner, the process will be killed with
    a longjmp straight from the signal handler (the setjmp having been
    done long long ago back when the process initialized itself). So
    Py_Finalize() will be called, but no other Python-related functions
    _at all_, and the process promptly terminates. I'm assuming that
    Python will flush out all its state on process termination (that is,
    it doesn't hang onto any system-global resources).

    Thanks for the advice!

    Chris Angelico
     
    Chris Angelico, May 4, 2011
    #3
  4. Chris Angelico

    Sells, Fred Guest

    newbie needs help with cookielib

    I'm using Python 2.4 and 2.7 for different apps. I'm happy with a
    solution for either one.

    I've got to talk to a url that uses a session cookie. I only need to
    set this when I'm developing/debugging so I don't need a robust
    production solution and I'm somewhat confused by the docs on cookielib.
    I can use urllib2 without cookielib just fine, but need the cookie to
    add some security.

    I'm normally using Firefox 4.0 to login to the server and get the
    cookie. After that I need some way to set the same cookie in my python
    script. I can do this by editing my code, since I only need it while
    defeloping from my test W7 box.


    I was hoping to find something like

    ....set_cookie('mycookiename', 'myvalue', 'mydomain.org')

    I've googled this most of the morning and found everything but what I
    need, or I just don't understand the basic concept. Any pointers would
    be greatly appreciated. One of my false starts looks like this. But I
    get a

    ....
    File "C:\alltools\python26\lib\urllib2.py", line 518, in
    http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 500: Access Deinied

    def test1():
    cj = cookielib.MozillaCookieJar()
    cj.load('C:/Users/myname/Desktop/cookies.txt')
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    r = opener.open("http://daffyduck.mydomain.org/wsgi/myapp.wsgi")
    print r.read()
    return
     
    Sells, Fred, May 4, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willem Oosthuizen

    Aborting Fucntions

    Willem Oosthuizen, Jul 10, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,065
  2. Wayne MJ

    Aborting a _Click event?

    Wayne MJ, Jul 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    398
    Wayne MJ
    Jul 23, 2003
  3. Wayne MJ

    Re: Aborting a _Click event?

    Wayne MJ, Jul 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    514
    Wayne MJ
    Jul 29, 2003
  4. Jeff Greenland

    .NET Timers aborting in web application

    Jeff Greenland, Dec 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    582
    Jeff Greenland
    Dec 18, 2003
  5. Russ
    Replies:
    5
    Views:
    509
    Fabrizio Milo
    Feb 19, 2006
Loading...

Share This Page