[svn r63916] KISS: remove interaction of warnings with plugin mechanism, it's not needed.

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-09 23:04:51 +02:00
parent 58cc8c29d3
commit 3015757b0a
2 changed files with 63 additions and 98 deletions

View File

@ -1,48 +1,28 @@
import py
from py.__.log.warning import WarningPlugin
mypath = py.magic.autopath()
class TestWarningPlugin:
def setup_method(self, method):
self.pluginmanager = py._com.Registry()
self.wb = WarningPlugin(self.pluginmanager)
self.pluginmanager.register(self)
self.warnings = []
def test_forwarding_to_warnings_module():
py.test.deprecated_call(py.log._apiwarn, "1.3", "..")
def pyevent__WARNING(self, warning):
self.warnings.append(warning)
def test_apiwarn_functional():
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):
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():
self.wb.warn("x", stacklevel=2)
# 3
# 4
f()
lno = self.test_stacklevel.im_func.func_code.co_firstlineno + 5
warning = self.warnings[0]
assert warning.lineno == lno
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)
def test_stacklevel():
def f():
py.log._apiwarn("x", "some", stacklevel=2)
# 3
# 4
capture = py.io.StdCapture()
f()
out, err = capture.reset()
lno = test_stacklevel.func_code.co_firstlineno + 6
warning = str(err)
assert warning.find(":%s" % lno) != -1

View File

@ -10,60 +10,45 @@ class Warning(py.std.exceptions.DeprecationWarning):
def __str__(self):
return self.msg
# XXX probably only apiwarn() + py._com.comregistry forwarding
# warn_explicit is actually needed
def _apiwarn(startversion, msg, stacklevel=1):
# below is mostly COPIED from python2.4/warnings.py's def warn()
# Get context information
msg = "%s (since version %s)" %(msg, startversion)
warn(msg, stacklevel=stacklevel+1)
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()
# Get context information
msg = "%s (since version %s)" %(msg, startversion)
self.warn(msg, stacklevel=stacklevel+1)
def warn(msg, stacklevel=1):
try:
caller = sys._getframe(stacklevel)
except ValueError:
globals = sys.__dict__
lineno = 1
else:
globals = caller.f_globals
lineno = caller.f_lineno
if '__name__' in globals:
module = globals['__name__']
else:
module = "<string>"
filename = globals.get('__file__')
if filename:
fnl = filename.lower()
if fnl.endswith(".pyc") or fnl.endswith(".pyo"):
filename = filename[:-1]
else:
if module == "__main__":
try:
filename = sys.argv[0]
except AttributeError:
# embedded interpreters don't have sys.argv, see bug #839151
filename = '__main__'
if not filename:
filename = module
path = py.path.local(filename)
warning = Warning(msg, path, lineno)
py.std.warnings.warn_explicit(warning, category=Warning,
filename=str(warning.path),
lineno=warning.lineno,
registry=py.std.warnings.__dict__.setdefault(
"__warningsregistry__", {})
)
def warn(self, msg, stacklevel=1):
try:
caller = sys._getframe(stacklevel)
except ValueError:
globals = sys.__dict__
lineno = 1
else:
globals = caller.f_globals
lineno = caller.f_lineno
if '__name__' in globals:
module = globals['__name__']
else:
module = "<string>"
filename = globals.get('__file__')
if filename:
fnl = filename.lower()
if fnl.endswith(".pyc") or fnl.endswith(".pyo"):
filename = filename[:-1]
else:
if module == "__main__":
try:
filename = sys.argv[0]
except AttributeError:
# embedded interpreters don't have sys.argv, see bug #839151
filename = '__main__'
if not filename:
filename = module
path = py.path.local(filename)
warning = Warning(msg, path, lineno)
self.pluginmanager.call_each("pyevent__WARNING", warning)
# singleton api warner for py lib
apiwarner = WarningPlugin(py._com.comregistry)
_apiwarn = apiwarner.apiwarn