From: Mike Bayer Date: Fri, 14 Dec 2007 23:47:33 +0000 (+0000) Subject: oof...unicode object still needs to return the value if it just warned... X-Git-Tag: rel_0_4_2~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8cf05355bb15f2557318490a92e9d802c2fd42b6;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git oof...unicode object still needs to return the value if it just warned... --- diff --git a/examples/elementtree/adjacency_list.py b/examples/elementtree/adjacency_list.py index af9084a448..706cc88a07 100644 --- a/examples/elementtree/adjacency_list.py +++ b/examples/elementtree/adjacency_list.py @@ -129,11 +129,11 @@ class ElementTreeMarshal(object): def __set__(self, document, element): def traverse(node): n = _Node() - n.tag = node.tag - n.text = node.text - n.tail = node.tail + n.tag = unicode(node.tag) + n.text = unicode(node.text) + n.tail = unicode(node.tail) n.children = [traverse(n2) for n2 in node] - n.attributes = [_Attribute(k, v) for k, v in node.attrib.iteritems()] + n.attributes = [_Attribute(unicode(k), unicode(v)) for k, v in node.attrib.iteritems()] return n document._root = traverse(element.getroot()) @@ -174,9 +174,9 @@ print document ############################################ PART VI - Searching for Paths ####################################### # manually search for a document which contains "/somefile/header/field1:hi" -d = session.query(Document).join('_root', aliased=True).filter(_Node.tag=='somefile').\ - join('children', aliased=True, from_joinpoint=True).filter(_Node.tag=='header').\ - join('children', aliased=True, from_joinpoint=True).filter(and_(_Node.tag=='field1', _Node.text=='hi')).\ +d = session.query(Document).join('_root', aliased=True).filter(_Node.tag==u'somefile').\ + join('children', aliased=True, from_joinpoint=True).filter(_Node.tag==u'header').\ + join('children', aliased=True, from_joinpoint=True).filter(and_(_Node.tag==u'field1', _Node.text==u'hi')).\ one() print d @@ -198,10 +198,10 @@ def find_document(path, compareto): return query.options(lazyload('_root')).filter(_Node.text==compareto).all() for path, compareto in ( - ('/somefile/header/field1', 'hi'), - ('/somefile/field1', 'hi'), - ('/somefile/header/field2', 'there'), - ('/somefile/header/field2[@attr=foo]', 'there') + (u'/somefile/header/field1', u'hi'), + (u'/somefile/field1', u'hi'), + (u'/somefile/header/field2', u'there'), + (u'/somefile/header/field2[@attr=foo]', u'there') ): print "\nDocuments containing '%s=%s':" % (path, compareto), line print [d.filename for d in find_document(path, compareto)] diff --git a/examples/elementtree/optimized_al.py b/examples/elementtree/optimized_al.py index 5666f879a1..316f17c679 100644 --- a/examples/elementtree/optimized_al.py +++ b/examples/elementtree/optimized_al.py @@ -137,12 +137,12 @@ class ElementTreeMarshal(object): def __set__(self, document, element): def traverse(node): n = _Node() - n.tag = node.tag - n.text = node.text - n.tail = node.tail + n.tag = unicode(node.tag) + n.text = unicode(node.text) + n.tail = unicode(node.tail) document._nodes.append(n) n.children = [traverse(n2) for n2 in node] - n.attributes = [_Attribute(k, v) for k, v in node.attrib.iteritems()] + n.attributes = [_Attribute(unicode(k), unicode(v)) for k, v in node.attrib.iteritems()] return n traverse(element.getroot()) @@ -184,9 +184,9 @@ print document # manually search for a document which contains "/somefile/header/field1:hi" print "\nManual search for /somefile/header/field1=='hi':", line -d = session.query(Document).join('_nodes', aliased=True).filter(and_(_Node.parent_id==None, _Node.tag=='somefile')).\ - join('children', aliased=True, from_joinpoint=True).filter(_Node.tag=='header').\ - join('children', aliased=True, from_joinpoint=True).filter(and_(_Node.tag=='field1', _Node.text=='hi')).\ +d = session.query(Document).join('_nodes', aliased=True).filter(and_(_Node.parent_id==None, _Node.tag==u'somefile')).\ + join('children', aliased=True, from_joinpoint=True).filter(_Node.tag==u'header').\ + join('children', aliased=True, from_joinpoint=True).filter(and_(_Node.tag==u'field1', _Node.text==u'hi')).\ one() print d @@ -213,10 +213,10 @@ def find_document(path, compareto): return query.options(lazyload('_nodes')).filter(_Node.text==compareto).all() for path, compareto in ( - ('/somefile/header/field1', 'hi'), - ('/somefile/field1', 'hi'), - ('/somefile/header/field2', 'there'), - ('/somefile/header/field2[@attr=foo]', 'there') + (u'/somefile/header/field1', u'hi'), + (u'/somefile/field1', u'hi'), + (u'/somefile/header/field2', u'there'), + (u'/somefile/header/field2[@attr=foo]', u'there') ): print "\nDocuments containing '%s=%s':" % (path, compareto), line print [d.filename for d in find_document(path, compareto)] diff --git a/examples/elementtree/pickle.py b/examples/elementtree/pickle.py index 443ca85c3e..e7cd86984e 100644 --- a/examples/elementtree/pickle.py +++ b/examples/elementtree/pickle.py @@ -22,8 +22,8 @@ logging.basicConfig() from elementtree import ElementTree -meta = MetaData() -meta.engine = 'sqlite://' +engine = create_engine('sqlite://') +meta = MetaData(engine) # stores a top level record of an XML document. # the "element" column will store the ElementTree document as a BLOB. diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index fb54db7a38..44e6ebf60d 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -348,7 +348,8 @@ class String(Concatenable, TypeEngine): return value.encode(dialect.encoding) elif assert_unicode and not isinstance(value, (unicode, NoneType)): if assert_unicode == 'warn': - warnings.warn(RuntimeWarning("Unicode type received non-unicode bind param value %r" % value)) + warnings.warn(RuntimeWarning("Unicode type received non-unicode bind param value %r" % value)) + return value else: raise exceptions.InvalidRequestError("Unicode type received non-unicode bind param value %r" % value) else: diff --git a/test/sql/testtypes.py b/test/sql/testtypes.py index 5ea0921310..073af3e39a 100644 --- a/test/sql/testtypes.py +++ b/test/sql/testtypes.py @@ -316,6 +316,13 @@ class UnicodeTest(AssertMixin): def testassert(self): import warnings + + warnings.filterwarnings("always", r".*non-unicode bind") + + # test that data still goes in if warning is emitted.... + unicode_table.insert().execute(unicode_varchar='im not unicode') + assert select([unicode_table.c.unicode_varchar]).execute().fetchall() == [('im not unicode', )] + warnings.filterwarnings("error", r".*non-unicode bind") try: @@ -323,7 +330,7 @@ class UnicodeTest(AssertMixin): assert False except RuntimeWarning, e: assert str(e) == "Unicode type received non-unicode bind param value 'im not unicode'", str(e) - + unicode_engine = engines.utf8_engine(options={'convert_unicode':True, 'assert_unicode':True}) try: try: