From 10559b12cb0917aebc9fa20868334d039914acfa Mon Sep 17 00:00:00 2001 From: Emilien Kofman Date: Wed, 11 Mar 2015 14:40:27 +0100 Subject: [PATCH 1/4] Fix issue 252 (deepcopy raises TypeError) --- pint/quantity.py | 3 +++ pint/testsuite/test_issues.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/pint/quantity.py b/pint/quantity.py index a5f6614..d153c5b 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -118,6 +118,9 @@ class _Quantity(object): ret.__used = self.__used return ret + def __deepcopy__(self, memo): + return self.__copy__() + def __str__(self): return format(self) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index ab14e21..936b1fc 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -3,6 +3,7 @@ from __future__ import division, unicode_literals, print_function, absolute_import import math +import copy from pint import UnitRegistry from pint.unit import UnitsContainer @@ -471,3 +472,9 @@ class TestIssuesNP(QuantityTestCase): q1 = a * self.ureg.meter q2 = a.T * self.ureg.meter self.assertQuantityEqual(q1.T, q2) + + def test_issue252(self): + ur = UnitRegistry() + t = copy.deepcopy(ur("3 F")) + u = t.to(ur.mF) + From 26c85197442ba38d5485b6fc94c2d329a47030d8 Mon Sep 17 00:00:00 2001 From: Emilien Kofman Date: Wed, 11 Mar 2015 14:48:31 +0100 Subject: [PATCH 2/4] Properly deepcopying --- pint/quantity.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pint/quantity.py b/pint/quantity.py index d153c5b..78d8a66 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -119,7 +119,10 @@ class _Quantity(object): return ret def __deepcopy__(self, memo): - return self.__copy__() + ret = self.__class__(copy.deepcopy(self._magnitude), + copy.deepcopy(self._units)) + ret.__used = self.__used + return ret def __str__(self): return format(self) From 8ceb0b08dba29711ea92afd996ac4dd0db76ceae Mon Sep 17 00:00:00 2001 From: Emilien Kofman Date: Tue, 17 Mar 2015 11:01:48 +0100 Subject: [PATCH 3/4] Pass the memo argument inside deepcopy) --- pint/quantity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pint/quantity.py b/pint/quantity.py index 78d8a66..fa7fb38 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -119,8 +119,8 @@ class _Quantity(object): return ret def __deepcopy__(self, memo): - ret = self.__class__(copy.deepcopy(self._magnitude), - copy.deepcopy(self._units)) + ret = self.__class__(copy.deepcopy(self._magnitude, memo), + copy.deepcopy(self._units, memo)) ret.__used = self.__used return ret From a48fd46e6ab89a2b1f07f6c4d2a47c27813e33b5 Mon Sep 17 00:00:00 2001 From: Emilien Kofman Date: Wed, 11 Mar 2015 14:40:27 +0100 Subject: [PATCH 4/4] Fix issue 252 (deepcopy raises TypeError) Properly deepcopying Pass the memo argument inside deepcopy) --- pint/quantity.py | 6 ++++++ pint/testsuite/test_issues.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/pint/quantity.py b/pint/quantity.py index a5f6614..fa7fb38 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -118,6 +118,12 @@ class _Quantity(object): ret.__used = self.__used return ret + def __deepcopy__(self, memo): + ret = self.__class__(copy.deepcopy(self._magnitude, memo), + copy.deepcopy(self._units, memo)) + ret.__used = self.__used + return ret + def __str__(self): return format(self) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index ab14e21..936b1fc 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -3,6 +3,7 @@ from __future__ import division, unicode_literals, print_function, absolute_import import math +import copy from pint import UnitRegistry from pint.unit import UnitsContainer @@ -471,3 +472,9 @@ class TestIssuesNP(QuantityTestCase): q1 = a * self.ureg.meter q2 = a.T * self.ureg.meter self.assertQuantityEqual(q1.T, q2) + + def test_issue252(self): + ur = UnitRegistry() + t = copy.deepcopy(ur("3 F")) + u = t.to(ur.mF) +