[pypy-svn] r68805 - in pypy/branch/logging/pypy/rlib: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Oct 28 10:33:14 CET 2009
Author: arigo
Date: Wed Oct 28 10:33:13 2009
New Revision: 68805
Modified:
pypy/branch/logging/pypy/rlib/rlog.py
pypy/branch/logging/pypy/rlib/rlog_parsing.py
pypy/branch/logging/pypy/rlib/test/test_rlog.py
Log:
Add (big) docstrings on the two public functions of pypy.rlib.rlog.
Small tweaks.
Modified: pypy/branch/logging/pypy/rlib/rlog.py
==============================================================================
--- pypy/branch/logging/pypy/rlib/rlog.py (original)
+++ pypy/branch/logging/pypy/rlib/rlog.py Wed Oct 28 10:33:13 2009
@@ -12,9 +12,39 @@
def has_log():
+ """Check if logging is enabled. If translated with --no-rjit, this
+ returns the constant False. Otherwise, it returns True or False
+ depending on the presence of the PYPYLOG env var. (Note that the
+ debug_log() function also checks has_log() by itself, so it's only
+ useful to explicitly call has_log() to protect large pieces of code.)
+ """
return True
def debug_log(_category, _message, **_kwds):
+ """Logging main function. If translated with --no-rjit, all calls to
+ this function are ignored. Otherwise, if the PYPYLOG env var is set,
+ it records an entry in the log. Arguments:
+
+ * category: a short string, more or less of the form
+ 'mainarea-subarea-exactlocation'. To make log parsing easier,
+ whenever multiple log entries belong together, start with
+ an entry 'mainarea-subarea-{' and end with 'mainarea-subarea-}'.
+ Note that the category should be unique (use different
+ 'exactlocation' if needed).
+
+ * message: freeform string displayed to the user, with embedded
+ %-formatting commands like '%(foo)d' and '%(bar)s'. The message
+ must be a constant string; it is only recorded once per log file.
+
+ * keywords arguments: values to record in the entry, like foo=5
+ and bar='hello'.
+
+ Supported argument types:
+ 'd': signed integer
+ 'f': float
+ 's': printable string or none
+ 'r': random binary string or none
+ """
getattr(_log, _category)(_message % _kwds)
# ____________________________________________________________
@@ -89,6 +119,7 @@
'd': annmodel.SomeInteger(),
'f': annmodel.SomeFloat(),
's': annmodel.SomeString(can_be_None=True),
+ 'r': annmodel.SomeString(can_be_None=True),
}
annhelper = hop.rtyper.getannmixlevel()
args_s = [ann[t] for t in cat.types]
@@ -101,7 +132,7 @@
v = hop.inputarg(lltype.Signed, arg=arg)
elif typechar == 'f':
v = hop.inputarg(lltype.Float, arg=arg)
- elif typechar == 's':
+ elif typechar == 's' or typechar == 'r':
v = hop.inputarg(hop.rtyper.type_system.rstr.string_repr,
arg=arg)
else:
@@ -126,7 +157,7 @@
import re
-r_entry = re.compile(r"%\((\w+)\)([sdf])")
+r_entry = re.compile(r"%\((\w+)\)([srdf])")
SIZEOF_FLOAT = struct.calcsize("f")
@@ -236,5 +267,7 @@
s = '(null)'
self.write_str(s)
+ add_subentry_r = add_subentry_s
+
def add_subentry_f(self, float):
self.write_float(float)
Modified: pypy/branch/logging/pypy/rlib/rlog_parsing.py
==============================================================================
--- pypy/branch/logging/pypy/rlib/rlog_parsing.py (original)
+++ pypy/branch/logging/pypy/rlib/rlog_parsing.py Wed Oct 28 10:33:13 2009
@@ -23,9 +23,11 @@
nextc = self.f.read(1)
if not nextc:
raise EOFError
+ lastbyte = ord(nextc)
+ if lastbyte < 0x80:
+ return lastbyte
shift = 0
result = 0
- lastbyte = ord(nextc)
while lastbyte & 0x80:
result |= ((lastbyte & 0x7F) << shift)
shift += 7
@@ -47,6 +49,7 @@
readers = {
'd': self.read_int,
's': self.read_str,
+ 'r': self.read_str,
'f': self.read_float,
}
curtime = 0.0
Modified: pypy/branch/logging/pypy/rlib/test/test_rlog.py
==============================================================================
--- pypy/branch/logging/pypy/rlib/test/test_rlog.py (original)
+++ pypy/branch/logging/pypy/rlib/test/test_rlog.py Wed Oct 28 10:33:13 2009
@@ -215,6 +215,7 @@
rlog.debug_log("Aa", "hello %(foo)d %(bar)f", foo=x, bar=-7.3)
rlog.debug_log("Aa", "hello %(foo)d %(bar)f", foo=x+1, bar=x+0.5)
rlog.debug_log("Ab", "<<%(baz)s>>", baz="hi there")
+ rlog.debug_log("Ac", "[%(foo)r]", foo="\x00")
assert rlog.has_log()
def setup_method(self, _):
@@ -232,14 +233,16 @@
def check_result(self):
entries = list(rlog_parsing.parse_log(self.pypylog))
- assert len(entries) == 3
+ assert len(entries) == 4
#
assert isinstance(entries[0][0], float)
assert isinstance(entries[1][0], float)
assert isinstance(entries[2][0], float)
+ assert isinstance(entries[3][0], float)
#
Aa = entries[0][1]
Ab = entries[2][1]
+ Ac = entries[3][1]
assert entries[1][1] is Aa
assert Aa.category == 'Aa'
assert Aa.message == 'hello %(foo)d %(bar)f'
@@ -247,10 +250,14 @@
assert Ab.category == 'Ab'
assert Ab.message == '<<%(baz)s>>'
assert Ab.entries == [('baz', 's')]
+ assert Ac.category == 'Ac'
+ assert Ac.message == '[%(foo)r]'
+ assert Ac.entries == [('foo', 'r')]
#
assert entries[0][2] == [132, roughly(-7.3)]
assert entries[1][2] == [133, 132.5]
assert entries[2][2] == ['hi there']
+ assert entries[3][2] == ['\x00']
def test_interpret_f(self):
interpret(self.f.im_func, [132], malloc_check=False)
More information about the Pypy-commit
mailing list