[py-svn] r13223 - in py/dist/py/misc: . testing
hpk at codespeak.net
hpk at codespeak.net
Thu Jun 9 11:48:09 CEST 2005
Author: hpk
Date: Thu Jun 9 11:48:08 2005
New Revision: 13223
Added:
py/dist/py/misc/testing/test_trace.py
py/dist/py/misc/trace.py
Log:
a first stab at implementing a _minimalistic_
tracing functionality as discussed on py-dev.
It's already a bit convoluted (although it has
only 54 lines of code :-)
Added: py/dist/py/misc/testing/test_trace.py
==============================================================================
--- (empty file)
+++ py/dist/py/misc/testing/test_trace.py Thu Jun 9 11:48:08 2005
@@ -0,0 +1,41 @@
+
+import py
+
+class TestTracing:
+ def setup_method(self, meth):
+ self.state = py.trace._getstate_()
+ def teardown_method(self, meth):
+ py.trace._setstate_(self.state)
+
+ def test_tracer_repr(self):
+ t = py.trace.debug
+ assert repr(t).find('debug') != -1
+
+ def test_trace_one_keyword(self):
+ t = py.trace.debug
+ l = []
+ def f(msg):
+ l.append(msg)
+ py.trace['debug'] = f
+ py.trace.debug("hello world")
+ assert len(l) == 1
+ msg = l[0]
+ assert msg.content().startswith('hello world')
+ assert msg.prefix() == '[debug] '
+ assert str(msg) == "[debug] hello world"
+
+ def test_trace_default_writer(self):
+ l = []
+ def f(msg):
+ l.append(msg)
+
+ py.trace[...] = f
+ py.trace.debug("hello")
+ py.trace.warn("world")
+ assert len(l) == 2
+ msg1, msg2 = l
+
+ assert 'debug' in msg1.keywords
+ assert 'warn' in msg2.keywords
+ assert msg1.content() == 'hello'
+ assert msg2.content() == 'world'
Added: py/dist/py/misc/trace.py
==============================================================================
--- (empty file)
+++ py/dist/py/misc/trace.py Thu Jun 9 11:48:08 2005
@@ -0,0 +1,69 @@
+"""
+py lib's basic tracing functionality
+
+ EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL (especially the dispatching)
+
+WARNING: this module is not allowed to contain any 'py' imports,
+ Instead, it is very self-contained and should not depend on
+ CPython/stdlib versions, either. One reason for these
+ restrictions is that this module should be sendable
+ via py.execnet across the network in an very early phase.
+"""
+
+class Message(object):
+ def __init__(self, keywords, args):
+ self.keywords = keywords
+ self.args = args
+
+ def content(self):
+ return " ".join(map(str, self.args))
+
+ def prefix(self):
+ return "[%s] " % (":".join(self.keywords))
+
+ def __str__(self):
+ return self.prefix() + self.content()
+
+class Tracer(object):
+ Message = Message # to allow later customization
+ _traceregistry = {}
+
+ def __init__(self, keywords=()):
+ self.keywords = keywords
+
+ def __repr__(self):
+ return "<py.__.Tracer %s>" % ":".join(self.keywords)
+
+ def __getattr__(self, name):
+ if name[0] == '_':
+ raise AttributeError, name
+ return Tracer(self.keywords + (name,))
+
+ def __setitem__(self, name, func):
+ assert callable(func)
+ keywords = self.keywords + (name,)
+ self._traceregistry[keywords] = func
+
+ def __call__(self, *args):
+ message = self.Message(self.keywords, args)
+ try:
+ func = self._traceregistry[message.keywords]
+ except KeyError:
+ # XXX find best match, for now it's a hack/simplistic
+ try:
+ func = self._traceregistry[(Ellipsis,)]
+ except KeyError:
+ print str(message)
+ return
+ func(message)
+
+ # class methods dealing with registry
+ def _getstate_(cls):
+ return cls._traceregistry.copy()
+ _getstate_ = classmethod(_getstate_)
+
+ def _setstate_(cls, state):
+ cls._traceregistry = state
+ _setstate_ = classmethod(_setstate_)
+
+trace = Tracer()
More information about the pytest-commit
mailing list