From 4bcd7e38e12ca2a74de1644b7d260497a64bc7dd Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 20 Aug 2013 13:27:27 -0400 Subject: [PATCH] Fixed bug where list instrumentation would fail to represent a setslice of ``[0:0]`` correctly, which in particular could occur when using ``insert(0, item)`` with the association proxy. Due to some quirk in Python collections, the issue was much more likely with Python 3 rather than 2. Also in 0.8.3, 0.7.11. [ticket:2807] Conflicts: doc/build/changelog/changelog_09.rst Conflicts: doc/build/changelog/changelog_08.rst --- doc/build/changelog/changelog_07.rst | 10 ++++++++++ lib/sqlalchemy/orm/collections.py | 5 ++++- test/orm/test_collection.py | 11 ++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/doc/build/changelog/changelog_07.rst b/doc/build/changelog/changelog_07.rst index 738e897463..d7d3019d2e 100644 --- a/doc/build/changelog/changelog_07.rst +++ b/doc/build/changelog/changelog_07.rst @@ -6,6 +6,16 @@ .. changelog:: :version: 0.7.11 + .. change:: + :tags: bug, orm + :tickets: 2807 + + Fixed bug where list instrumentation would fail to represent a + setslice of ``[0:0]`` correctly, which in particular could occur + when using ``insert(0, item)`` with the association proxy. Due + to some quirk in Python collections, the issue was much more likely + with Python 3 rather than 2. + .. change:: :tags: bug, sql :tickets: 2801 diff --git a/lib/sqlalchemy/orm/collections.py b/lib/sqlalchemy/orm/collections.py index e26a59731f..bb2d272c4c 100644 --- a/lib/sqlalchemy/orm/collections.py +++ b/lib/sqlalchemy/orm/collections.py @@ -1078,7 +1078,10 @@ def _list_decorators(): start = index.start or 0 if start < 0: start += len(self) - stop = index.stop or len(self) + if index.stop is not None: + stop = index.stop + else: + stop = len(self) if stop < 0: stop += len(self) diff --git a/test/orm/test_collection.py b/test/orm/test_collection.py index b3de03aaef..0f54da5517 100644 --- a/test/orm/test_collection.py +++ b/test/orm/test_collection.py @@ -127,9 +127,9 @@ class CollectionsTest(fixtures.ORMTest): control = list() def assert_eq(): - self.assert_(set(direct) == canary.data) - self.assert_(set(adapter) == canary.data) - self.assert_(direct == control) + eq_(set(direct), canary.data) + eq_(set(adapter), canary.data) + eq_(direct, control) # assume append() is available for list tests e = creator() @@ -259,6 +259,11 @@ class CollectionsTest(fixtures.ORMTest): control[-2:-1] = values assert_eq() + values = [creator()] + direct[0:0] = values + control[0:0] = values + assert_eq() + if hasattr(direct, '__delitem__') or hasattr(direct, '__delslice__'): for i in range(1, 4): -- 2.47.2