mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-12-01 13:13:45 +03:00
Make Range pickleable
This commit is contained in:
parent
90ee1ebba5
commit
cde19c4d59
|
@ -171,6 +171,17 @@ class Range(object):
|
||||||
else:
|
else:
|
||||||
return self.__gt__(other)
|
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):
|
def register_range(pgrange, pyrange, conn_or_curs, globally=False):
|
||||||
"""Create and register an adapter and the typecasters to convert between
|
"""Create and register an adapter and the typecasters to convert between
|
||||||
|
|
|
@ -20,6 +20,7 @@ import sys
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from pickle import dumps, loads
|
||||||
|
|
||||||
from testutils import unittest, skip_if_no_uuid, skip_before_postgres
|
from testutils import unittest, skip_if_no_uuid, skip_before_postgres
|
||||||
from testutils import ConnectingTestCase, decorate_all_tests
|
from testutils import ConnectingTestCase, decorate_all_tests
|
||||||
|
@ -1397,6 +1398,12 @@ class RangeTestCase(unittest.TestCase):
|
||||||
with py3_raises_typeerror():
|
with py3_raises_typeerror():
|
||||||
self.assert_(Range(1, 2) >= 1)
|
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):
|
def skip_if_no_range(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user