[SQL-CVS] r4173 - in SQLObject/trunk: docs sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2010-04-15 18:57:20
|
Author: phd Date: 2010-04-15 12:57:12 -0600 (Thu, 15 Apr 2010) New Revision: 4173 Modified: SQLObject/trunk/docs/News.txt SQLObject/trunk/sqlobject/col.py Log: Validators became stricter: 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__. Modified: SQLObject/trunk/docs/News.txt =================================================================== --- SQLObject/trunk/docs/News.txt 2010-04-11 16:52:57 UTC (rev 4172) +++ SQLObject/trunk/docs/News.txt 2010-04-15 18:57:12 UTC (rev 4173) @@ -16,8 +16,11 @@ * SQLObject instances that don't have a per-instance connection can be pickled and unpickled. -* Added TimedeltaCol; currently it's only implemented on PostgreSQL as an - INTERVAL type. +* Validators became stricter: StringCol and UnicodeCol now accept only str + or unicode; BoolCol accepts only bool or int; 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). @@ -25,13 +28,13 @@ Small Features -------------- +* Added TimedeltaCol; currently it's only implemented on PostgreSQL as an + INTERVAL type. + * Do not pollute the base sqlmeta class to allow Style to set idName. In the case of inherited idName inherited value takes precedence; to allow Style to set idName reset inherited idName to None. -* Validators became stricter: StringCol and UnicodeCol now accept only str - or unicode, BoolCol accepts only bool or int, and so on. - * Better handling of circular dependencies in sqlobject-admin - do not include the class in the list of other classes. Modified: SQLObject/trunk/sqlobject/col.py =================================================================== --- SQLObject/trunk/sqlobject/col.py 2010-04-11 16:52:57 UTC (rev 4172) +++ SQLObject/trunk/sqlobject/col.py 2010-04-15 18:57:12 UTC (rev 4173) @@ -591,19 +591,16 @@ return None if isinstance(value, (int, long, sqlbuilder.SQLExpression)): return value - try: - return int(value) - except: - raise validators.Invalid("expected an int in the IntCol '%s', got %s %r instead" % \ + for converter, attr_name in (int, '__int__'), (long, '__long__'): + if hasattr(value, attr_name): + try: + return converter(value) + except: + break + raise validators.Invalid("expected an int in the IntCol '%s', got %s %r instead" % \ (self.name, type(value), value), value, state) - def from_python(self, value, state): - if value is None: - return None - if isinstance(value, (int, long, sqlbuilder.SQLExpression)): - return value - raise validators.Invalid("expected an int in the IntCol '%s', got %s %r instead" % \ - (self.name, type(value), value), value, state) + from_python = to_python class SOIntCol(SOCol): # 3-03 @@: support precision, maybe max and min directly @@ -720,8 +717,14 @@ def to_python(self, value, state): if value is None: return None - if isinstance(value, (int, long, float, sqlbuilder.SQLExpression)): + if isinstance(value, (float, int, long, sqlbuilder.SQLExpression)): return value + for converter, attr_name in (float, '__float__'), (int, '__int__'), (long, '__long__'): + if hasattr(value, attr_name): + try: + return converter(value) + except: + break raise validators.Invalid("expected a float in the FloatCol '%s', got %s %r instead" % \ (self.name, type(value), value), value, state) |