|
From: <sub...@co...> - 2007-10-17 18:17:58
|
Author: ianb
Date: 2007-10-17 12:17:57 -0600 (Wed, 17 Oct 2007)
New Revision: 3086
Modified:
FormEncode/trunk/formencode/schema.py
FormEncode/trunk/formencode/variabledecode.py
FormEncode/trunk/tests/test_schema.py
Log:
Fix handling of empty values in Schema, regression from last commit
Modified: FormEncode/trunk/formencode/schema.py
===================================================================
--- FormEncode/trunk/formencode/schema.py 2007-10-17 18:08:56 UTC (rev 3085)
+++ FormEncode/trunk/formencode/schema.py 2007-10-17 18:17:57 UTC (rev 3086)
@@ -1,5 +1,6 @@
from interfaces import *
from api import *
+from api import _
import declarative
__all__ = ['Schema']
@@ -52,8 +53,9 @@
order = []
messages = {
- 'notExpected': 'The input field %(name)s was not expected.',
- 'missingValue': "Missing value",
+ 'notExpected': _('The input field %(name)s was not expected.'),
+ 'missingValue': _("Missing value"),
+ 'badDictType': _("The input must be dict-like (not a %(type)s: %(value)r)"),
}
__mutableattributes__ = ('fields', 'chained_validators',
@@ -98,12 +100,26 @@
for name, value in self.fields.items():
self.add_field(name, value)
+ def assert_dict(self, value, state):
+ """
+ Helper to assure we have proper input
+ """
+ if not hasattr(value, 'items'):
+ # Not a dict or dict-like object
+ raise Invalid(self.message('badDictType', state, type=type(value), value=value),
+ value, state)
+
def _to_python(self, value_dict, state):
- if not value_dict and self.if_empty is not NoDefault:
- return self.if_empty
+ if not value_dict:
+ if self.if_empty is not NoDefault:
+ return self.if_empty
+ else:
+ value_dict = {}
for validator in self.pre_validators:
value_dict = validator.to_python(value_dict, state)
+
+ self.assert_dict(value_dict, state)
new = {}
errors = {}
@@ -192,6 +208,7 @@
__traceback_info__ = 'for_python chained_validator %s (finished %s)' % (validator, ', '.join(map(repr, finished)) or 'none')
finished.append(validator)
value_dict = validator.from_python(value_dict, state)
+ self.assert_dict(value_dict, state)
new = {}
errors = {}
unused = self.fields.keys()
@@ -287,6 +304,10 @@
result.extend(self.fields.values())
return result
+ def is_empty(self, value):
+ # Generally nothing is 'empty' for a schema
+ return False
+
def format_compound_error(v, indent=0):
if isinstance(v, Exception):
try:
Modified: FormEncode/trunk/formencode/variabledecode.py
===================================================================
--- FormEncode/trunk/formencode/variabledecode.py 2007-10-17 18:08:56 UTC (rev 3085)
+++ FormEncode/trunk/formencode/variabledecode.py 2007-10-17 18:17:57 UTC (rev 3086)
@@ -143,3 +143,5 @@
def _from_python(self, value, state):
return variable_encode(value)
+ def empty_value(self, value):
+ return {}
Modified: FormEncode/trunk/tests/test_schema.py
===================================================================
--- FormEncode/trunk/tests/test_schema.py 2007-10-17 18:08:56 UTC (rev 3085)
+++ FormEncode/trunk/tests/test_schema.py 2007-10-17 18:17:57 UTC (rev 3086)
@@ -52,9 +52,10 @@
all_cases.append(self)
def test(self):
- print self.raw_input
+ print 'input', repr(self.input)
actual = self.schema.to_python(self.input)
- assert actual == self.output
+ print 'output', repr(actual)
+ assert actual == self.output
class BadCase(DecodeCase):
@@ -66,9 +67,9 @@
self.output = self.output['text']
def test(self):
- print self.raw_input
+ print repr(self.raw_input)
try:
- self.schema.to_python(self.input)
+ print repr(self.schema.to_python(self.input))
except Invalid, e:
actual = e.unpack_errors()
assert actual == self.output
|