[svn r63913] make py.log.APIWARN available

--HG--
branch : trunk
This commit is contained in:
hpk
2009-04-09 22:32:04 +02:00
parent 73529ce63d
commit 2ffb68c177
7 changed files with 11 additions and 15 deletions
-49
View File
@@ -1,49 +0,0 @@
import py
from py.__.misc.warn 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 pyevent__WARNING(self, warning):
self.warnings.append(warning)
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():
from py.__.misc.warn import APIWARN
assert py._com.comregistry.isregistered(APIWARN.im_self)
-69
View File
@@ -1,69 +0,0 @@
import py, sys
class Warning(py.std.exceptions.DeprecationWarning):
def __init__(self, msg, path, lineno):
self.msg = msg
self.path = path
self.lineno = lineno
def __repr__(self):
return "%s:%d: %s" %(self.path, self.lineno+1, self.msg)
def __str__(self):
return self.msg
# XXX probably only apiwarn() + py._com.comregistry forwarding
# 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()
# Get context information
msg = "%s (since version %s)" %(msg, startversion)
self.warn(msg, stacklevel=stacklevel+1)
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