From 60017fec0e5e7401a111a8595cad86c2752ec2e3 Mon Sep 17 00:00:00 2001 From: Jonathan Ross Rogers Date: Mon, 1 Aug 2016 12:40:52 -0400 Subject: [PATCH 1/2] Make Range pickleable --- lib/_range.py | 11 +++++++++++ tests/test_types_extras.py | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/lib/_range.py b/lib/_range.py index 47b82086..b6fe0bdc 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -171,6 +171,17 @@ class Range(object): else: return self.__gt__(other) + def __getstate__(self): + return dict( + (slot, getattr(self, slot)) + for slot in self.__slots__ + if hasattr(self, slot) + ) + + def __setstate__(self, state): + for slot, value in state.items(): + setattr(self, slot, value) + def register_range(pgrange, pyrange, conn_or_curs, globally=False): """Create and register an adapter and the typecasters to convert between diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index b81cecab..d8444010 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -20,6 +20,7 @@ import sys from decimal import Decimal from datetime import date, datetime from functools import wraps +from pickle import dumps, loads from testutils import unittest, skip_if_no_uuid, skip_before_postgres from testutils import ConnectingTestCase, decorate_all_tests @@ -1397,6 +1398,12 @@ class RangeTestCase(unittest.TestCase): with py3_raises_typeerror(): self.assert_(Range(1, 2) >= 1) + def test_pickling(self): + from psycopg2.extras import Range + + r = Range(0, 4) + self.assertEqual(loads(dumps(r)), r) + def skip_if_no_range(f): @wraps(f) From cc054514611633f51e6d761a1ae245ea8774b5d6 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 7 Aug 2016 02:33:38 +0100 Subject: [PATCH 2/2] Report range picklable in NEWS file Fix #462 --- NEWS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS b/NEWS index 1a29628f..f0379065 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,12 @@ Current release --------------- +What's new in psycopg 2.6.3 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Make `~psycopg2.extras.Range` objects picklable (:ticket:`#462`). + + What's new in psycopg 2.6.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^