Skip to content

Commit e8563b8

Browse files
committed
sanitize test_pickle, *don't* import * from modules! causes clashes between units/constants.
1 parent 3f661af commit e8563b8

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

src/amuse/test/suite/core_tests/test_pickle.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
from amuse.test import amusetest
2-
1+
import subprocess
32
import pickle
3+
import sys
4+
import os
45

5-
from amuse.support.exceptions import AmuseException
6+
from amuse.test import amusetest
67

7-
from amuse.units import core
88
from amuse.units import si
99
from amuse.units import nbody_system
10-
from amuse.units import generic_unit_system
1110
from amuse.units.quantities import zero
12-
from amuse.units.units import *
13-
from amuse.units.constants import *
11+
from amuse.units.units import m, km, kg, parsec, stellar_type
1412

1513
from amuse.datamodel import Particles, parameters
1614

17-
import subprocess
18-
import pickle
19-
import sys
20-
import os
21-
2215

2316
class TestPicklingOfUnitsAndQuantities(amusetest.TestCase):
2417

2518
def test1(self):
26-
km = 1000 * m
27-
self.assertEqual(1000, km.value_in(m))
28-
pickled_km = pickle.dumps(km)
29-
unpickled_km = pickle.loads(pickled_km)
30-
self.assertEqual(1000, unpickled_km.value_in(m))
19+
kilometer = 1000 * m
20+
self.assertEqual(1000, kilometer.value_in(m))
21+
pickled_kilometer = pickle.dumps(kilometer)
22+
unpickled_kilometer = pickle.loads(pickled_kilometer)
23+
self.assertEqual(1000, unpickled_kilometer.value_in(m))
3124

3225
def test2(self):
33-
km = 1000 * m
34-
quantity = 12.0 | km
26+
kilometer = 1000 * m
27+
quantity = 12.0 | kilometer
3528
self.assertEqual(12000, quantity.value_in(m))
3629
pickled_quantity = pickle.dumps(quantity)
3730
unpickled_quantity = pickle.loads(pickled_quantity)
@@ -81,15 +74,22 @@ def test8(self):
8174

8275
def test9(self):
8376
quantity = 1.3 | nbody_system.time
84-
path = os.path.abspath(os.path.join(self.get_path_to_results(), "test9.pickle"))
77+
path = os.path.abspath(
78+
os.path.join(self.get_path_to_results(), "test9.pickle")
79+
)
8580

8681
with open(path, "wb") as stream:
8782
pickle.dump(quantity, stream)
8883

8984
pythonpath = os.pathsep.join(sys.path)
9085
env = os.environ.copy()
9186
env['PYTHONPATH'] = pythonpath
92-
code = "import pickle;stream = open('{0}', 'rb'); print(str(pickle.load(stream)));stream.close()".format(path)
87+
code = (
88+
f"import pickle;"
89+
f"stream = open('{path}', 'rb');"
90+
f"print(str(pickle.load(stream)));"
91+
f"stream.close()"
92+
)
9393

9494
process = subprocess.Popen([
9595
sys.executable,
@@ -100,18 +100,28 @@ def test9(self):
100100
)
101101
unpickled_quantity_string, error_string = process.communicate()
102102
self.assertEqual(process.returncode, 0)
103-
self.assertEqual(str(quantity), unpickled_quantity_string.strip().decode('utf-8'))
103+
self.assertEqual(
104+
str(quantity),
105+
unpickled_quantity_string.strip().decode('utf-8')
106+
)
104107

105108
def test10(self):
106109
quantity = 1 | parsec
107-
path = os.path.abspath(os.path.join(self.get_path_to_results(), "test10.pickle"))
110+
path = os.path.abspath(
111+
os.path.join(self.get_path_to_results(), "test10.pickle")
112+
)
108113
with open(path, "wb") as stream:
109114
pickle.dump(quantity, stream)
110115

111116
pythonpath = os.pathsep.join(sys.path)
112117
env = os.environ.copy()
113118
env['PYTHONPATH'] = pythonpath
114-
code = "import pickle;stream = open('{0}', 'rb'); print(str(pickle.load(stream)));stream.close()".format(path)
119+
code = (
120+
f"import pickle;"
121+
f"stream = open('{path}', 'rb');"
122+
f"print(str(pickle.load(stream)));"
123+
f"stream.close()"
124+
)
115125

116126
process = subprocess.Popen([
117127
sys.executable,
@@ -122,7 +132,10 @@ def test10(self):
122132
)
123133
unpickled_quantity_string, error_string = process.communicate()
124134
self.assertEqual(process.returncode, 0)
125-
self.assertEqual(str(quantity), unpickled_quantity_string.strip().decode('utf-8'))
135+
self.assertEqual(
136+
str(quantity),
137+
unpickled_quantity_string.strip().decode('utf-8')
138+
)
126139

127140
def test11(self):
128141
value = 1 | stellar_type
@@ -175,7 +188,7 @@ def test4(self):
175188
self.assertEqual(unpickled_particles.center_of_mass(), [2, 3, 0] | m)
176189

177190

178-
class BaseTestModule(object):
191+
class BaseTestModule:
179192
def before_get_parameter(self):
180193
return
181194

@@ -202,13 +215,13 @@ def set_test(self, value):
202215
self.x = value
203216

204217
o = TestModule()
205-
set = parameters.Parameters([definition,], o)
206-
set.test_name = 10 | m
218+
paramset = parameters.Parameters([definition,], o)
219+
paramset.test_name = 10 | m
207220

208221
self.assertEqual(o.x, 10 | m)
209-
self.assertEqual(set.test_name, 10 | m)
222+
self.assertEqual(paramset.test_name, 10 | m)
210223

211-
memento = set.copy()
224+
memento = paramset.copy()
212225
self.assertEqual(memento.test_name, 10 | m)
213226

214227
pickled_memento = pickle.dumps(memento)

0 commit comments

Comments
 (0)