From 3b9c49e5bb47a979fedb151e36f4e3a00724b096 Mon Sep 17 00:00:00 2001 From: Matteo Raso Date: Fri, 25 Nov 2022 00:28:35 -0500 Subject: [PATCH 1/2] BUG: Polynomials now copy properly (#22669) On line 502, self.symbol.copy() was called, which causes an AttributeError, since self.symbol is a string, so it doesn't have a copy() method. To fix it, I simply removed the copy() and directly assigned the string. --- numpy/polynomial/_polybase.py | 2 +- numpy/polynomial/tests/test_polynomial.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py index 9674dee0b3f2..3bea91dd239c 100644 --- a/numpy/polynomial/_polybase.py +++ b/numpy/polynomial/_polybase.py @@ -499,7 +499,7 @@ def __getstate__(self): ret['coef'] = self.coef.copy() ret['domain'] = self.domain.copy() ret['window'] = self.window.copy() - ret['symbol'] = self.symbol.copy() + ret['symbol'] = self.symbol return ret def __setstate__(self, dict): diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index a0a09fcf4a93..032bf0e76af9 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -5,6 +5,7 @@ import numpy as np import numpy.polynomial.polynomial as poly +from copy import deepcopy from numpy.testing import ( assert_almost_equal, assert_raises, assert_equal, assert_, assert_warns, assert_array_equal, assert_raises_regex) @@ -41,6 +42,10 @@ def test_polyone(self): def test_polyx(self): assert_equal(poly.polyx, [0, 1]) + def test_copy(self): + x = poly.Polynomial([1, 2, 3]) + y = deepcopy(x) + assert_equal(x, y) class TestArithmetic: From ab24072afa1beefd54e72377c500b61400e04a5c Mon Sep 17 00:00:00 2001 From: Matteo Raso Date: Sat, 26 Nov 2022 16:14:09 -0500 Subject: [PATCH 2/2] Added pickle test for polynomials --- numpy/polynomial/tests/test_polynomial.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 032bf0e76af9..6b3ef2388f63 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -5,6 +5,7 @@ import numpy as np import numpy.polynomial.polynomial as poly +import pickle from copy import deepcopy from numpy.testing import ( assert_almost_equal, assert_raises, assert_equal, assert_, @@ -47,6 +48,11 @@ def test_copy(self): y = deepcopy(x) assert_equal(x, y) + def test_pickle(self): + x = poly.Polynomial([1, 2, 3]) + y = pickle.loads(pickle.dumps(x)) + assert_equal(x, y) + class TestArithmetic: def test_polyadd(self):