From: Mike Bayer Date: Sun, 1 Apr 2012 15:01:56 +0000 (-0400) Subject: - [bug] Fixed bug which would prevent X-Git-Tag: rel_0_7_7~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2563b33453dee1b55500f13421b7b50ba367a504;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - [bug] Fixed bug which would prevent OrderingList from being pickleable [ticket:2454]. Courtesy Jeff Dairiki --- diff --git a/CHANGES b/CHANGES index 2dcceddb6e..727e7235f3 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,10 @@ CHANGES and joins, particularly when using column_property(). [ticket:2453] + - [bug] Fixed bug which would prevent + OrderingList from being pickleable + [ticket:2454]. Courtesy Jeff Dairiki + - postgresql - [feature] Added new for_update/with_lockmode() options for Postgresql: for_update="read"/ diff --git a/lib/sqlalchemy/ext/orderinglist.py b/lib/sqlalchemy/ext/orderinglist.py index 97be329e98..0a27ee3093 100644 --- a/lib/sqlalchemy/ext/orderinglist.py +++ b/lib/sqlalchemy/ext/orderinglist.py @@ -314,9 +314,23 @@ class OrderingList(list): self._reorder() # end Py2K + def __reduce__(self): + return _reconstitute, (self.__class__, self.__dict__, list(self)) + for func_name, func in locals().items(): if (util.callable(func) and func.func_name == func_name and not func.__doc__ and hasattr(list, func_name)): func.__doc__ = getattr(list, func_name).__doc__ del func_name, func +def _reconstitute(cls, dict_, items): + """ Reconstitute an ``OrderingList``. + + This is the adjoint to ``OrderingList.__reduce__()``. It is used for + unpickling ``OrderingList``\\s + + """ + obj = cls.__new__(cls) + obj.__dict__.update(dict_) + list.extend(obj, items) + return obj diff --git a/test/ext/test_orderinglist.py b/test/ext/test_orderinglist.py index dfeac5c3ed..0b7d4328ec 100644 --- a/test/ext/test_orderinglist.py +++ b/test/ext/test_orderinglist.py @@ -1,8 +1,10 @@ -from sqlalchemy import * -from sqlalchemy.orm import * -from sqlalchemy.ext.orderinglist import * +from sqlalchemy import Integer, ForeignKey, String, MetaData +from sqlalchemy.orm import relationship, mapper, create_session +from sqlalchemy.ext.orderinglist import ordering_list from test.lib.testing import eq_ -from test.lib import * +from test.lib import fixtures, testing +from test.lib.schema import Table, Column +from test.lib.util import picklers metadata = None @@ -407,3 +409,25 @@ class OrderingListTest(fixtures.TestBase): self.assert_(alpha[li].position == pos) + def test_picklability(self): + from sqlalchemy.ext.orderinglist import OrderingList + + olist = OrderingList('order', reorder_on_append=True) + olist.append(DummyItem()) + + for loads, dumps in picklers(): + pck = dumps(olist) + copy = loads(pck) + + self.assert_(copy == olist) + self.assert_(copy.__dict__ == olist.__dict__) + +class DummyItem(object): + def __init__(self, order=None): + self.order = order + + def __eq__(self, other): + return self.order == other.order + + def __ne__(self, other): + return not (self == other)