[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 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") def f():
assert len(self.warnings) == 1 py.log._apiwarn("x", "some", stacklevel=2)
# 3
def test_location(self): # 4
self.wb.warn("again") capture = py.io.StdCapture()
warning = self.warnings[0] f()
lno = self.test_location.im_func.func_code.co_firstlineno + 1 out, err = capture.reset()
assert warning.lineno == lno lno = test_stacklevel.func_code.co_firstlineno + 6
assert warning.path == mypath warning = str(err)
locstr = "%s:%d: " %(mypath, lno+1,) assert warning.find(":%s" % 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)

View File

@ -10,60 +10,45 @@ 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 # 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 warn(msg, stacklevel=1):
def __init__(self, bus): try:
self.pluginmanager = bus caller = sys._getframe(stacklevel)
bus.register(self) except ValueError:
globals = sys.__dict__
def pyevent__WARNING(self, warning): lineno = 1
# forward to python warning system else:
py.std.warnings.warn_explicit(warning, category=Warning, globals = caller.f_globals
filename=str(warning.path), lineno = caller.f_lineno
lineno=warning.lineno, if '__name__' in globals:
registry=py.std.warnings.__dict__.setdefault( module = globals['__name__']
"__warningsregistry__", {}) else:
) module = "<string>"
filename = globals.get('__file__')
def apiwarn(self, startversion, msg, stacklevel=1): if filename:
# below is mostly COPIED from python2.4/warnings.py's def warn() fnl = filename.lower()
# Get context information if fnl.endswith(".pyc") or fnl.endswith(".pyo"):
msg = "%s (since version %s)" %(msg, startversion) filename = filename[:-1]
self.warn(msg, stacklevel=stacklevel+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