[SQL-CVS] r4259 - in SQLObject/branches/0.13: docs sqlobject sqlobject/tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2010-10-15 12:07:02
|
Author: phd Date: 2010-10-15 06:06:54 -0600 (Fri, 15 Oct 2010) New Revision: 4259 Added: SQLObject/branches/0.13/sqlobject/tests/test_perConnection.py Modified: SQLObject/branches/0.13/docs/News.txt SQLObject/branches/0.13/sqlobject/col.py SQLObject/branches/0.13/sqlobject/dbconnection.py SQLObject/branches/0.13/sqlobject/main.py SQLObject/branches/0.13/sqlobject/sqlbuilder.py Log: A bug was fixed in a subtle case when a per-instance connection is not passed to validators. Modified: SQLObject/branches/0.13/docs/News.txt =================================================================== --- SQLObject/branches/0.13/docs/News.txt 2010-10-10 11:54:02 UTC (rev 4258) +++ SQLObject/branches/0.13/docs/News.txt 2010-10-15 12:06:54 UTC (rev 4259) @@ -7,6 +7,12 @@ .. _start: +SQLObject 0.13.1 +================ + +* A bug was fixed in a subtle case when a per-instance connection is not + passed to validators. + SQLObject 0.13.0 ================ Modified: SQLObject/branches/0.13/sqlobject/col.py =================================================================== --- SQLObject/branches/0.13/sqlobject/col.py 2010-10-10 11:54:02 UTC (rev 4258) +++ SQLObject/branches/0.13/sqlobject/col.py 2010-10-15 12:06:54 UTC (rev 4259) @@ -498,13 +498,19 @@ def to_python(self, value, state): if value is None: return None - connection = state.soObject._connection - dbEncoding = getattr(connection, "dbEncoding", None) or "ascii" + try: + connection = state.connection or state.soObject._connection + except AttributeError: + dbEncoding = "ascii" + binaryType = type(None) # Just a simple workaround + else: + dbEncoding = getattr(connection, "dbEncoding", None) or "ascii" + binaryType = connection._binaryType if isinstance(value, unicode): return value.encode(dbEncoding) if self.dataType and isinstance(value, self.dataType): return value - if isinstance(value, (str, buffer, connection._binaryType, sqlbuilder.SQLExpression)): + if isinstance(value, (str, buffer, binaryType, sqlbuilder.SQLExpression)): return value if hasattr(value, '__unicode__'): return unicode(value).encode(dbEncoding) @@ -1300,10 +1306,14 @@ return value if isinstance(value, float): value = str(value) - connection = state.soObject._connection - if hasattr(connection, "decimalSeparator"): - value = value.replace(connection.decimalSeparator, ".") try: + connection = state.connection or state.soObject._connection + except AttributeError: + pass + else: + if hasattr(connection, "decimalSeparator"): + value = value.replace(connection.decimalSeparator, ".") + try: return Decimal(value) except: raise validators.Invalid("expected a Decimal in the DecimalCol '%s', got %s %r instead" % \ @@ -1315,10 +1325,14 @@ if isinstance(value, float): value = str(value) if isinstance(value, basestring): - connection = state.soObject._connection - if hasattr(connection, "decimalSeparator"): - value = value.replace(connection.decimalSeparator, ".") try: + connection = state.connection or state.soObject._connection + except AttributeError: + pass + else: + if hasattr(connection, "decimalSeparator"): + value = value.replace(connection.decimalSeparator, ".") + try: return Decimal(value) except: raise validators.Invalid("can not parse Decimal value '%s' in the DecimalCol from '%s'" % @@ -1422,12 +1436,19 @@ def to_python(self, value, state): if value is None: return None + try: + connection = state.connection or state.soObject._connection + except AttributeError: + dbName = None + binaryType = type(None) # Just a simple workaround + else: + dbName = connection.dbName + binaryType = connection._binaryType if isinstance(value, str): - connection = state.soObject._connection - if connection.dbName == "sqlite": + if dbName == "sqlite": value = connection.module.decode(value) return value - if isinstance(value, (buffer, state.soObject._connection._binaryType)): + if isinstance(value, (buffer, binaryType)): cachedValue = self._cachedValue if cachedValue and cachedValue[1] == value: return cachedValue[0] @@ -1440,7 +1461,8 @@ def from_python(self, value, state): if value is None: return None - binary = state.soObject._connection.createBinary(value) + connection = state.connection or state.soObject._connection + binary = connection.createBinary(value) self._cachedValue = (value, binary) return binary @@ -1492,8 +1514,12 @@ if value is None: return None if isinstance(value, unicode): - connection = state.soObject._connection - dbEncoding = getattr(connection, "dbEncoding", None) or "ascii" + try: + connection = state.connection or state.soObject._connection + except AttributeError: + dbEncoding = "ascii" + else: + dbEncoding = getattr(connection, "dbEncoding", None) or "ascii" value = value.encode(dbEncoding) if isinstance(value, str): return pickle.loads(value) Modified: SQLObject/branches/0.13/sqlobject/dbconnection.py =================================================================== --- SQLObject/branches/0.13/sqlobject/dbconnection.py 2010-10-10 11:54:02 UTC (rev 4258) +++ SQLObject/branches/0.13/sqlobject/dbconnection.py 2010-10-15 12:06:54 UTC (rev 4259) @@ -582,7 +582,7 @@ if key in kw: value = kw.pop(key) if col.from_python: - value = col.from_python(value, sqlbuilder.SQLObjectState(soClass)) + value = col.from_python(value, sqlbuilder.SQLObjectState(soClass, connection=self)) data[col.dbName] = value elif col.foreignName in kw: obj = kw.pop(col.foreignName) Modified: SQLObject/branches/0.13/sqlobject/main.py =================================================================== --- SQLObject/branches/0.13/sqlobject/main.py 2010-10-10 11:54:02 UTC (rev 4258) +++ SQLObject/branches/0.13/sqlobject/main.py 2010-10-15 12:06:54 UTC (rev 4259) @@ -1318,7 +1318,7 @@ for n, v in zip(name, value): from_python = getattr(cls, '_SO_from_python_' + n) if from_python: - v = from_python(v, sqlbuilder.SQLObjectState(cls)) + v = from_python(v, sqlbuilder.SQLObjectState(cls, connection=connection)) new_value.append(v) condition = sqlbuilder.AND(*[getattr(cls.q, n)==v for n,v in zip(name, new_value)]) return (connection or cls._connection)._SO_selectOneAlt( Modified: SQLObject/branches/0.13/sqlobject/sqlbuilder.py =================================================================== --- SQLObject/branches/0.13/sqlobject/sqlbuilder.py 2010-10-10 11:54:02 UTC (rev 4258) +++ SQLObject/branches/0.13/sqlobject/sqlbuilder.py 2010-10-15 12:06:54 UTC (rev 4259) @@ -80,8 +80,9 @@ class SQLObjectState(object): - def __init__(self, soObject): + def __init__(self, soObject, connection=None): self.soObject = weakref.proxy(soObject) + self.connection = connection safeSQLRE = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_\.]*$') Added: SQLObject/branches/0.13/sqlobject/tests/test_perConnection.py =================================================================== --- SQLObject/branches/0.13/sqlobject/tests/test_perConnection.py (rev 0) +++ SQLObject/branches/0.13/sqlobject/tests/test_perConnection.py 2010-10-15 12:06:54 UTC (rev 4259) @@ -0,0 +1,15 @@ +from sqlobject import * +from sqlobject.tests.dbtest import * + +######################################## +## Per-instance connection +######################################## + +class TestPerConnection(SQLObject): + test = StringCol() + +def test_perConnection(): + connection = getConnection() + TestPerConnection.createTable(connection=connection) + TestPerConnection(test='test', connection=connection) + assert len(list(TestPerConnection.select(TestPerConnection.q.test=='test', connection=connection))) == 1 |