Rev 4413: Change how schemas are validated (down to 1.02s) in lp:///~jameinel/bzr/bencode_serializer
John Arbash Meinel
john at arbash-meinel.com
Thu Jun 4 18:38:48 BST 2009
At lp:///~jameinel/bzr/bencode_serializer
------------------------------------------------------------
revno: 4413
revision-id: john at arbash-meinel.com-20090604173830-e9j1rpv4euxkmzqr
parent: john at arbash-meinel.com-20090604171229-kbgfatt63y3u3uh1
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: bencode_serializer
timestamp: Thu 2009-06-04 12:38:30 -0500
message:
Change how schemas are validated (down to 1.02s)
_vaidate_properties now avoids creating a new dict, but decodes the items 'in place'.
We don't copy the schema dict and then pop items out, instead we just
check that we have a valid entry for every item.
-------------- next part --------------
=== modified file 'bzrlib/chk_serializer.py'
--- a/bzrlib/chk_serializer.py 2009-06-03 19:51:50 +0000
+++ b/bzrlib/chk_serializer.py 2009-06-04 17:38:30 +0000
@@ -33,9 +33,10 @@
def _validate_properties(props, _decode=cache_utf8._utf8_decode):
# TODO: we really want an 'isascii' check for key
- unicode_props = dict([(key, _decode(value)[0])
- for key, value in props.iteritems()])
- return unicode_props
+ # Cast the utf8 properties into Unicode 'in place'
+ for key, value in props.iteritems():
+ props[key] = _decode(value)[0]
+ return props
def _is_format_10(value):
@@ -106,14 +107,13 @@
ret = bencode.bdecode(text)
if not isinstance(ret, list):
raise ValueError("invalid revision text")
- schema = dict(self._schema)
- schema_pop = schema.pop
+ schema = self._schema
# timezone is allowed to be missing, but should be set
bits = {'timezone': None}
for key, value in ret:
# Will raise KeyError if not a valid part of the schema, or an
# entry is given 2 times.
- var_name, expected_type, validator = schema_pop(key)
+ var_name, expected_type, validator = schema[key]
if value.__class__ is not expected_type:
raise ValueError('key %s did not conform to the expected type'
' %s, but was %s'
@@ -121,11 +121,12 @@
if validator is not None:
value = validator(value)
bits[var_name] = value
- if schema:
- if schema.keys() != ['timezone']:
- raise ValueError('Revision text was missing expected keys %s.'
- ' text %r' % (schema.keys(), text))
- del bits[None] # Get rid of bits that don't get mapped
+ if len(bits) != len(schema):
+ missing = [key for key, (var_name, _, _) in schema.iteritems()
+ if var_name not in bits]
+ raise ValueError('Revision text was missing expected keys %s.'
+ ' text %r' % (missing, text))
+ del bits[None] # Get rid of 'format' since it doesn't get mapped
rev = _mod_revision.Revision(**bits)
return rev
More information about the bazaar-commits
mailing list