]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in "to_list" conversion where a single bytes object
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Feb 2016 01:52:43 +0000 (20:52 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Feb 2016 01:52:43 +0000 (20:52 -0500)
would be turned into a list of individual characters.  This would
impact among other things using the :meth:`.Query.get` method
on a primary key that's a bytes object.
fixes #3660

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/util/_collections.py
lib/sqlalchemy/util/compat.py
test/base/test_utils.py

index fa1be4c16e94ede600b51f3e9b9a8a87e0a5c974..36152c2360cc882dd8a3fbc85109dc01c603908f 100644 (file)
 .. changelog::
     :version: 1.0.13
 
+    .. change::
+        :tags: bug, py3k
+        :tickets: 3660
+
+        Fixed bug in "to_list" conversion where a single bytes object
+        would be turned into a list of individual characters.  This would
+        impact among other things using the :meth:`.Query.get` method
+        on a primary key that's a bytes object.
+
     .. change::
         :tags: bug, orm
         :tickets: 3658
index ed1a3e471a46a71b8dd609974b79bf1f471a70dd..c29b81f6a1007626cfdd0a3beeba4f207fb1da09 100644 (file)
@@ -10,7 +10,8 @@
 from __future__ import absolute_import
 import weakref
 import operator
-from .compat import threading, itertools_filterfalse, string_types
+from .compat import threading, itertools_filterfalse, string_types, \
+    binary_types
 from . import py2k
 import types
 import collections
@@ -794,7 +795,8 @@ def coerce_generator_arg(arg):
 def to_list(x, default=None):
     if x is None:
         return default
-    if not isinstance(x, collections.Iterable) or isinstance(x, string_types):
+    if not isinstance(x, collections.Iterable) or \
+            isinstance(x, string_types + binary_types):
         return [x]
     elif isinstance(x, list):
         return x
index 99b5177b50a84c181347ec1368f19ed3f334c8bb..ee4a20f9bf20e090b65ac51957c88c88dc905632 100644 (file)
@@ -62,6 +62,7 @@ if py3k:
         )
 
     string_types = str,
+    binary_types = bytes,
     binary_type = bytes
     text_type = str
     int_types = int,
@@ -115,6 +116,7 @@ else:
     from cStringIO import StringIO as byte_buffer
 
     string_types = basestring,
+    binary_types = bytes,
     binary_type = str
     text_type = unicode
     int_types = int, long
index 53dbc12f9f95d5c283d979d7521c17f2226627c0..fcb9a59a3b9604b846973915dc09dc6a0d5991e9 100644 (file)
@@ -7,7 +7,7 @@ from sqlalchemy.testing import eq_, is_, ne_, fails_if, mock, expect_warnings
 from sqlalchemy.testing.util import picklers, gc_collect
 from sqlalchemy.util import classproperty, WeakSequence, get_callable_argspec
 from sqlalchemy.sql import column
-from sqlalchemy.util import langhelpers
+from sqlalchemy.util import langhelpers, compat
 import inspect
 
 
@@ -412,6 +412,20 @@ class ToListTest(fixtures.TestBase):
             [1, 2, 3]
         )
 
+    def test_from_bytes(self):
+
+        eq_(
+            util.to_list(compat.b('abc')),
+            [compat.b('abc')]
+        )
+
+        eq_(
+            util.to_list([
+                compat.b('abc'), compat.b('def')]),
+            [compat.b('abc'), compat.b('def')]
+        )
+
+
 class ColumnCollectionTest(fixtures.TestBase):
 
     def test_in(self):