Aborting Python from C code

C

Chris Angelico

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
 
C

Chris Angelico

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
 
C

Chris Angelico

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
 
S

Sells, Fred

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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top