Commit c00d94c7 authored by Kirill Smelkov's avatar Kirill Smelkov

X workaround lack of exception chaining on Python2 with xdefer

parent 49e73a6d
......@@ -33,7 +33,7 @@ from persistent import Persistent
from persistent.timestamp import TimeStamp
from ZODB.utils import z64
import os, os.path, subprocess, threading
import sys, os, os.path, subprocess, threading, inspect, traceback
from errno import EINVAL
from golang import chan, func, defer, select
from golang import context, sync, time
......@@ -76,6 +76,36 @@ def teardown_function(f):
os.rmdir(testmntpt)
# xdefer is like defer, but makes sure exception raised before deferred
# function is called is not lost.
#
# if deferred function raises exception itself - it prints previous exception to stderr.
#
# XXX xdefer is workaround for Python2 not having exception chanining (PEP 3134)
# without which, if e.g. tDB.close() raises exception, it prevents to see
# whether and which an assert in the test failed.
#
# XXX merge into defer?
def xdefer(f):
# hack - imitate as if defer called from under xdefer was called directly by caller func
fgo = inspect.currentframe().f_back.f_back
__goframe__ = fgo.f_locals['__goframe__']
_xdefer(f)
def _xdefer(f):
def _():
# call f, but print previous exception if f raises
exc_type, exc_value, exc_traceback = sys.exc_info()
try:
f()
except:
traceback.print_exception(exc_type, exc_value, exc_traceback)
raise
defer(_)
# ---- test join/autostart ----
# test that zurl does not change from one open to another storage open.
......@@ -94,14 +124,14 @@ def test_join():
wcfs.join(zurl, autostart=False)
wc = wcfs._start(zurl)
defer(wc.close)
xdefer(wc.close)
assert wc.mountpoint == testmntpt
assert readfile(wc.mountpoint + "/.wcfs/zurl") == zurl
assert os.path.isdir(wc.mountpoint + "/head")
assert os.path.isdir(wc.mountpoint + "/head/bigfile")
wc2 = wcfs.join(zurl, autostart=False)
defer(wc2.close)
xdefer(wc2.close)
assert wc2.mountpoint == wc.mountpoint
# test that join(autostart=y) works.
......@@ -112,7 +142,7 @@ def test_join_autostart():
wcfs.join(zurl, autostart=False)
wc = wcfs.join(zurl, autostart=True)
defer(wc.close)
xdefer(wc.close)
assert wc.mountpoint == testmntpt
assert readfile(wc.mountpoint + "/.wcfs/zurl") == zurl
assert os.path.isdir(wc.mountpoint + "/head")
......@@ -764,12 +794,11 @@ def _expectPin(w, ctx, zf, expect):
return reqv
# test_wcfs exercises wcfs functionality.
@func
def test_wcfs():
t = tDB()
defer(t.close)
xdefer(t.close)
t.root['!file'] = nonfile = Persistent()
t.root['zfile'] = zf = ZBigFile(blksize)
......@@ -850,11 +879,7 @@ def test_wcfs():
# XXX -> separate test?
w = t.openwatch()
print('\n\nzzzzzzz\n\n')
try:
assert w.sendReq(context.background(), b'bla bla') == ""
except:
#import traceback; traceback.print_exc()
raise
# assert w closed
print('\n\n0000000\n\n')
......@@ -919,21 +944,17 @@ def test_wcfs():
@func
def test_wcfs_invproto():
# XXX temp debug
import sys, traceback
"""
def _():
print('BBB')
1/0
defer(_)
"""
xdefer(_)
#raise RuntimeError('zzz')
try:
print('\nAAA')
assert 1 == 2
finally:
2/0
# ---- misc ---
......@@ -957,7 +978,7 @@ def tidtime(tid):
@func
def test_tidtime_notrough():
t = tDB()
defer(t.close)
xdefer(t.close)
atprev = t.commit()
for i in range(10):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment