Author: phd
Date: 2010-07-12 09:26:34 -0600 (Mon, 12 Jul 2010)
New Revision: 4204
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/col.py
SQLObject/trunk/sqlobject/tests/test_validation.py
Log:
StringCol and UnicodeCol now accept an instance of a class that implements __unicode__.
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2010-07-12 15:03:09 UTC (rev 4203)
+++ SQLObject/trunk/docs/News.txt 2010-07-12 15:26:34 UTC (rev 4204)
@@ -16,12 +16,13 @@
* SQLObject instances that don't have a per-instance connection can be
pickled and unpickled.
-* Validators became stricter: StringCol and UnicodeCol now accept only str
- or unicode; BoolCol accepts only bool or int or an instance of a class
- that implements __nonzero__; IntCol accepts int, long or an instance of a
- class that implements __int__ or __long__; FloatCol accepts float, int,
- long or an instance of a class that implements __float__, __int__ or
- __long__.
+* Validators became stricter: StringCol and UnicodeCol now accept only str,
+ unicode or an instance of a class that implements __unicode__ (but not
+ __str__ because every object has a __str__ method); BoolCol accepts only
+ bool or int or an instance of a class that implements __nonzero__; IntCol
+ accepts int, long or an instance of a class that implements __int__ or
+ __long__; FloatCol accepts float, int, long or an instance of a class
+ that implements __float__, __int__ or __long__.
* Added a connection class for rdbhost.com (commercial Postgres-over-Web
service).
Modified: SQLObject/trunk/sqlobject/col.py
===================================================================
--- SQLObject/trunk/sqlobject/col.py 2010-07-12 15:03:09 UTC (rev 4203)
+++ SQLObject/trunk/sqlobject/col.py 2010-07-12 15:26:34 UTC (rev 4204)
@@ -513,13 +513,15 @@
if value is None:
return None
connection = state.soObject._connection
+ dbEncoding = getattr(connection, "dbEncoding", None) or "ascii"
if isinstance(value, unicode):
- dbEncoding = getattr(connection, "dbEncoding", None) or "ascii"
return value.encode(dbEncoding)
if self.dataType and isinstance(value, self.dataType):
return value
if isinstance(value, (str, buffer, connection._binaryType, sqlbuilder.SQLExpression)):
return value
+ if hasattr(value, '__unicode__'):
+ return unicode(value).encode(dbEncoding)
raise validators.Invalid("expected a str in the StringCol '%s', got %s %r instead" % \
(self.name, type(value), value), value, state)
@@ -545,6 +547,8 @@
return unicode(value, self.dbEncoding)
if isinstance(value, array): # MySQL
return unicode(value.tostring(), self.dbEncoding)
+ if hasattr(value, '__unicode__'):
+ return unicode(value)
raise validators.Invalid("expected a str or a unicode in the UnicodeCol '%s', got %s %r instead" % \
(self.name, type(value), value), value, state)
@@ -555,6 +559,8 @@
return value
if isinstance(value, unicode):
return value.encode(self.dbEncoding)
+ if hasattr(value, '__unicode__'):
+ return unicode(value).encode(self.dbEncoding)
raise validators.Invalid("expected a str or a unicode in the UnicodeCol '%s', got %s %r instead" % \
(self.name, type(value), value), value, state)
Modified: SQLObject/trunk/sqlobject/tests/test_validation.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_validation.py 2010-07-12 15:03:09 UTC (rev 4203)
+++ SQLObject/trunk/sqlobject/tests/test_validation.py 2010-07-12 15:26:34 UTC (rev 4204)
@@ -14,6 +14,7 @@
name4 = FloatCol(default=2.718)
name5 = PickleCol(default=None)
name6 = BoolCol(default=None)
+ name7 = UnicodeCol(default=None)
class TestValidation:
@@ -32,6 +33,7 @@
raises(validators.Invalid, setattr, t, 'name3', '1')
raises(validators.Invalid, setattr, t, 'name4', '1')
raises(validators.Invalid, setattr, t, 'name6', '1')
+ raises(validators.Invalid, setattr, t, 'name7', 1)
t.name2 = 'you'
assert t.name2 == 'you'
|