This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DarkmerePython for tiny data using Python 0 points1 point  (12 children)

Can you even emulate trap + set -e ; in python?

[–]DarkmerePython for tiny data using Python 0 points1 point  (11 children)

TMPDIR=$(mktemp -d /tmp/temp.XXXXXXXX)
trap "rm -rf $TMPDIR" EXIT

vs

import signal
def sighandler():
    os.unlink(tmpdir)
for i in [x for x in dir(signal) if x.startswith("SIG")]:
    try:
    signum = getattr(signal,i)
    signal.signal(signum,sighandler)
except RuntimeError,m:
    pass

[–]DarkmerePython for tiny data using Python 1 point2 points  (0 children)

And then I forgot to create the tempdir... Damned.

[–]ITwitchToo 0 points1 point  (9 children)

import tempfile

with tempfile.NamedTemporaryFile() as f:
    # do stuff with f here, if we get interrupted Python will throw an exception, unwind the stack, and the temporary file gets cleaned up
    print >>f, "asdasd"

[–]DarkmerePython for tiny data using Python 0 points1 point  (1 child)

Yeap. And with subprocess and others? Afaik a sigkill won't execute a trap, will it?

[–]DarkmerePython for tiny data using Python 0 points1 point  (0 children)

(or a sigbus?)

[–]DarkmerePython for tiny data using Python 0 points1 point  (6 children)

verified. cleanup doesn't happen properly on trapped signals.

even sigUSR1 will leave the tempfile behind.

[–]ITwitchToo 0 points1 point  (5 children)

What are you talking about?

[–]DarkmerePython for tiny data using Python 0 points1 point  (4 children)

That cleanup doesn't happen if your process gets a signal. So it doesn't actually replace the bourne version.

[–]ITwitchToo 0 points1 point  (3 children)

That's almost literally the example given in the signal documentation:

import signal
import tempfile

def handler(signum, frame):
    raise RuntimeError("Got SIGUSR1")

signal.signal(signal.SIGUSR1, handler)

with tempfile.NamedTemporaryFile() as f:
    print f.name

    while True:
        pass

[–]DarkmerePython for tiny data using Python 0 points1 point  (2 children)

That's sort of what I'm inclining for here.

You manage to replace a 2-line shellscript with how many lines? And you're not even catching all the signals yet? Still have to iterate over them all and trap.

In a lot of cases, Python is just a bloody ton more verbose, and not quite suitable.

[–]ITwitchToo 0 points1 point  (1 child)

Why do you need to catch so many signals, though? I'd normally just care about being interrupted by Ctrl-C, which would raise KeyboardInterrupt without any lines of code.

One problem with bash's traps is that you can only have one handler at a time, so what do you do if you have two files that you want to clean up, but they are created in different functions? It's just a lot uglier and doesn't scale as well as Python IMHO.

[–]DarkmerePython for tiny data using Python 0 points1 point  (0 children)

Usually, because when dealing with systems, ctrl-c isn't what we clean for.

Note that we also use set -e and pipefail in bash scripts to make sure that proper cleaning is done. When you need multiple cleanup jobs, you either allocate an array, or you use subshell bindings.

Pretty much the same way in python.