[svn r63916] KISS: remove interaction of warnings with plugin mechanism, it's not needed.
--HG-- branch : trunk
This commit is contained in:
parent
58cc8c29d3
commit
3015757b0a
|
@ -1,48 +1,28 @@
|
||||||
import py
|
import py
|
||||||
from py.__.log.warning import WarningPlugin
|
|
||||||
mypath = py.magic.autopath()
|
mypath = py.magic.autopath()
|
||||||
|
|
||||||
class TestWarningPlugin:
|
def test_forwarding_to_warnings_module():
|
||||||
def setup_method(self, method):
|
py.test.deprecated_call(py.log._apiwarn, "1.3", "..")
|
||||||
self.pluginmanager = py._com.Registry()
|
|
||||||
self.wb = WarningPlugin(self.pluginmanager)
|
|
||||||
self.pluginmanager.register(self)
|
|
||||||
self.warnings = []
|
|
||||||
|
|
||||||
def pyevent__WARNING(self, warning):
|
def test_apiwarn_functional():
|
||||||
self.warnings.append(warning)
|
capture = py.io.StdCapture()
|
||||||
|
py.log._apiwarn("x.y.z", "something")
|
||||||
|
out, err = capture.reset()
|
||||||
|
print "out", out
|
||||||
|
print "err", err
|
||||||
|
assert err.find("x.y.z") != -1
|
||||||
|
lno = test_apiwarn_functional.func_code.co_firstlineno + 2
|
||||||
|
exp = "%s:%s" % (mypath, lno)
|
||||||
|
assert err.find(exp) != -1
|
||||||
|
|
||||||
def test_event_generation(self):
|
def test_stacklevel():
|
||||||
self.wb.warn("hello")
|
|
||||||
assert len(self.warnings) == 1
|
|
||||||
|
|
||||||
def test_location(self):
|
|
||||||
self.wb.warn("again")
|
|
||||||
warning = self.warnings[0]
|
|
||||||
lno = self.test_location.im_func.func_code.co_firstlineno + 1
|
|
||||||
assert warning.lineno == lno
|
|
||||||
assert warning.path == mypath
|
|
||||||
locstr = "%s:%d: " %(mypath, lno+1,)
|
|
||||||
assert repr(warning).startswith(locstr)
|
|
||||||
assert str(warning) == warning.msg
|
|
||||||
|
|
||||||
def test_stacklevel(self):
|
|
||||||
def f():
|
def f():
|
||||||
self.wb.warn("x", stacklevel=2)
|
py.log._apiwarn("x", "some", stacklevel=2)
|
||||||
# 3
|
# 3
|
||||||
# 4
|
# 4
|
||||||
|
capture = py.io.StdCapture()
|
||||||
f()
|
f()
|
||||||
lno = self.test_stacklevel.im_func.func_code.co_firstlineno + 5
|
out, err = capture.reset()
|
||||||
warning = self.warnings[0]
|
lno = test_stacklevel.func_code.co_firstlineno + 6
|
||||||
assert warning.lineno == lno
|
warning = str(err)
|
||||||
|
assert warning.find(":%s" % lno) != -1
|
||||||
def test_forwarding_to_warnings_module(self):
|
|
||||||
py.test.deprecated_call(self.wb.warn, "x")
|
|
||||||
|
|
||||||
def test_apiwarn(self):
|
|
||||||
self.wb.apiwarn("3.0", "xxx")
|
|
||||||
warning = self.warnings[0]
|
|
||||||
assert warning.msg == "xxx (since version 3.0)"
|
|
||||||
|
|
||||||
def test_default():
|
|
||||||
assert py._com.comregistry.isregistered(py.log._apiwarn.im_self)
|
|
||||||
|
|
|
@ -10,30 +10,13 @@ class Warning(py.std.exceptions.DeprecationWarning):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.msg
|
return self.msg
|
||||||
|
|
||||||
# XXX probably only apiwarn() + py._com.comregistry forwarding
|
def _apiwarn(startversion, msg, stacklevel=1):
|
||||||
# warn_explicit is actually needed
|
|
||||||
|
|
||||||
class WarningPlugin(object):
|
|
||||||
def __init__(self, bus):
|
|
||||||
self.pluginmanager = bus
|
|
||||||
bus.register(self)
|
|
||||||
|
|
||||||
def pyevent__WARNING(self, warning):
|
|
||||||
# forward to python warning system
|
|
||||||
py.std.warnings.warn_explicit(warning, category=Warning,
|
|
||||||
filename=str(warning.path),
|
|
||||||
lineno=warning.lineno,
|
|
||||||
registry=py.std.warnings.__dict__.setdefault(
|
|
||||||
"__warningsregistry__", {})
|
|
||||||
)
|
|
||||||
|
|
||||||
def apiwarn(self, startversion, msg, stacklevel=1):
|
|
||||||
# below is mostly COPIED from python2.4/warnings.py's def warn()
|
# below is mostly COPIED from python2.4/warnings.py's def warn()
|
||||||
# Get context information
|
# Get context information
|
||||||
msg = "%s (since version %s)" %(msg, startversion)
|
msg = "%s (since version %s)" %(msg, startversion)
|
||||||
self.warn(msg, stacklevel=stacklevel+1)
|
warn(msg, stacklevel=stacklevel+1)
|
||||||
|
|
||||||
def warn(self, msg, stacklevel=1):
|
def warn(msg, stacklevel=1):
|
||||||
try:
|
try:
|
||||||
caller = sys._getframe(stacklevel)
|
caller = sys._getframe(stacklevel)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -62,8 +45,10 @@ class WarningPlugin(object):
|
||||||
filename = module
|
filename = module
|
||||||
path = py.path.local(filename)
|
path = py.path.local(filename)
|
||||||
warning = Warning(msg, path, lineno)
|
warning = Warning(msg, path, lineno)
|
||||||
self.pluginmanager.call_each("pyevent__WARNING", warning)
|
py.std.warnings.warn_explicit(warning, category=Warning,
|
||||||
|
filename=str(warning.path),
|
||||||
|
lineno=warning.lineno,
|
||||||
|
registry=py.std.warnings.__dict__.setdefault(
|
||||||
|
"__warningsregistry__", {})
|
||||||
|
)
|
||||||
|
|
||||||
# singleton api warner for py lib
|
|
||||||
apiwarner = WarningPlugin(py._com.comregistry)
|
|
||||||
_apiwarn = apiwarner.apiwarn
|
|
||||||
|
|
Loading…
Reference in New Issue