• Kirill Smelkov's avatar
    bigfile/py: Stop caring about sys.exc_{type,value,traceback} variables · 3804cc39
    Kirill Smelkov authored
    Before py3k python stores exception information not only in thread-local state
    but also globally in sys.exc_* variables (wrt sys.exc_info()) for
    "backward compatibility". However using them is not thread-safe as the
    following example demonstrates:
    
    ---- 8< ----
    from threading import Thread
    import sys
    
    def T1():
        print 'T1'
        while 1:
            exc_type = sys.exc_type
            if exc_type is not None:
                print 'AAA: %r' % exc_type
    
    def f(): g()
    def g(): h()
    def h(): 1/0
    
    def T2():
        print 'T2'
        while 1:
            try:
                f()
            except:
                pass
    
    t1, t2 = Thread(target=T1), Thread(target=T2)
    t1.start(); t2.start()
    ---- 8< ----
    
    ---- 8< ----
    kirr@deco:~/tmp/trashme/t$ ./excthreads.py
    T1
    T2
    
    AAA: <type 'exceptions.ZeroDivisionError'>
    AAA: <type 'exceptions.ZeroDivisionError'>
    AAA: <type 'exceptions.ZeroDivisionError'>
    AAA: <type 'exceptions.ZeroDivisionError'>
    AAA: <type 'exceptions.ZeroDivisionError'>
    AAA: <type 'exceptions.ZeroDivisionError'>
    ^\Выход
    ---- 8< ----
    
    Because of the above nothing modern (I've explicitly checked at least CPython
    itself and Zope) uses this variables - wherever needed per-thread exception
    state is retrieved with sys.exc_info().
    
    So on wendelin.core side it is thus thankless job to try to preserve sys.exc_*
    vars state because on a busy server they are literally changing all the -
    arbitrary from the point of view of particular thread - time while its python
    code runs.
    3804cc39
_bigfile.c 32.1 KB