77"""
88
99import copy
10- import pickle
10+ import pickle # nosec B403 nosemgrep - the tests pickle their own graphs
1111import unittest
1212
1313import numpy as np
1414
1515from pgraph import DGraph , DVertex , Edge , UGraph , UVertex
1616
1717
18+ def dumps (obj , protocol = None ):
19+ return pickle .dumps (obj , protocol ) # nosec nosemgrep
20+
21+
22+ def loads (data ):
23+ return pickle .loads (data ) # nosec B301 nosemgrep
24+
25+
1826def manhattan (v ):
1927 return float (np .abs (v ).sum ())
2028
@@ -92,13 +100,13 @@ def check(self, g, h):
92100
93101 def test_pickle (self ):
94102 g = self .make ()
95- self .check (g , pickle . loads (pickle . dumps (g )))
103+ self .check (g , loads (dumps (g )))
96104
97105 def test_pickle_every_protocol (self ):
98106 g = self .make ()
99107 for protocol in range (2 , pickle .HIGHEST_PROTOCOL + 1 ):
100108 with self .subTest (protocol = protocol ):
101- self .check (g , pickle . loads (pickle . dumps (g , protocol )))
109+ self .check (g , loads (dumps (g , protocol )))
102110
103111 def test_copy (self ):
104112 g = self .make ()
@@ -119,7 +127,7 @@ def test_copy_is_independent(self):
119127 self .assertEqual (h .validate (), [])
120128
121129 def test_restored_graph_is_usable (self ):
122- h = pickle . loads (pickle . dumps (self .make ()))
130+ h = loads (dumps (self .make ()))
123131 e = h .add_edge (h ["d" ], h ["c" ])
124132 self .assertEqual (h .ne , self .make ().ne + 1 )
125133 h .remove_edge (e )
@@ -131,7 +139,7 @@ def test_restored_graph_is_usable(self):
131139
132140 def test_settings_survive (self ):
133141 g = self .cls (metric = "L1" , heuristic = "SE2" , dim = 3 , verbose = False )
134- h = pickle . loads (pickle . dumps (g ))
142+ h = loads (dumps (g ))
135143 self .assertEqual (h .metric ([3 , - 4 , 0 ]), 7 )
136144 with self .assertRaises (ValueError ):
137145 h .add_vertex ([1 , 2 ]) # dim is still 3
@@ -141,54 +149,54 @@ def test_builtin_metrics_pickle_by_name(self):
141149 for name , value in (("L1" , 7.0 ), ("L2" , 5.0 )):
142150 with self .subTest (metric = name ):
143151 g = self .cls (metric = name , heuristic = name )
144- h = pickle . loads (pickle . dumps (g ))
152+ h = loads (dumps (g ))
145153 self .assertEqual (h .metric (np .r_ [3.0 , - 4.0 ]), value )
146154 self .assertIs (h .metric , g .metric )
147155 self .assertIs (h .heuristic , g .heuristic )
148156
149157 def test_default_metric_pickles (self ):
150158 g = self .cls ()
151- h = pickle . loads (pickle . dumps (g ))
159+ h = loads (dumps (g ))
152160 self .assertAlmostEqual (h .metric (np .r_ [3.0 , 4.0 ]), 5.0 )
153161
154162 def test_se2_metric_pickles (self ):
155163 g = self .cls (metric = "SE2" )
156- h = pickle . loads (pickle . dumps (g ))
164+ h = loads (dumps (g ))
157165 self .assertIs (h .metric , g .metric )
158166
159167 def test_user_module_level_metric (self ):
160168 g = self .cls (metric = manhattan )
161- h = pickle . loads (pickle . dumps (g ))
169+ h = loads (dumps (g ))
162170 self .assertIs (h .metric , manhattan )
163171
164172 def test_lambda_metric_cannot_be_pickled_but_can_be_copied (self ):
165173 g = self .cls (metric = lambda v : 1.0 )
166174 with self .assertRaises ((pickle .PicklingError , AttributeError )):
167- pickle . dumps (g )
175+ dumps (g )
168176 self .assertEqual (g .copy ().metric (np .r_ [1.0 ]), 1.0 )
169177
170178 def test_empty_graph (self ):
171179 g = self .cls ()
172- h = pickle . loads (pickle . dumps (g ))
180+ h = loads (dumps (g ))
173181 self .assertEqual ((h .n , h .ne ), (0 , 0 ))
174182
175183 def test_long_chain_pickles_and_copies (self ):
176184 g = chain (5000 , self .cls )
177- for h in (pickle . loads (pickle . dumps (g )), g .copy (), copy .deepcopy (g )):
185+ for h in (loads (dumps (g )), g .copy (), copy .deepcopy (g )):
178186 self .assertEqual ((h .n , h .ne ), (5000 , 4999 ))
179187 self .assertEqual (h [2500 ].adjacent ()[0 ].name , g [2500 ].adjacent ()[0 ].name )
180188 self .assertEqual (h .validate (), [])
181189
182190 def test_vertex_alone_is_refused (self ):
183191 g = self .make ()
184- for f in (pickle . dumps , copy .deepcopy , copy .copy ):
192+ for f in (dumps , copy .deepcopy , copy .copy ):
185193 with self .subTest (f = f .__name__ ):
186194 with self .assertRaises (TypeError ):
187195 f (g ["a" ])
188196
189197 def test_edge_alone_is_refused (self ):
190198 g = self .make ()
191- for f in (pickle . dumps , copy .deepcopy , copy .copy ):
199+ for f in (dumps , copy .deepcopy , copy .copy ):
192200 with self .subTest (f = f .__name__ ):
193201 with self .assertRaises (TypeError ):
194202 f (g .edges ()[0 ])
@@ -204,14 +212,14 @@ def test_data_referring_to_a_vertex_is_refused(self):
204212 g = self .make ()
205213 g ["a" ].data = g ["b" ]
206214 with self .assertRaises (TypeError ):
207- pickle . dumps (g )
215+ dumps (g )
208216
209217
210218class TestUGraphPickle (_RoundTrip , unittest .TestCase ):
211219 cls = UGraph
212220
213221 def test_loop_listed_twice_at_vertex (self ):
214- h = pickle . loads (pickle . dumps (self .make ()))
222+ h = loads (dumps (self .make ()))
215223 loop = [e for e in h .edges () if e .v1 is e .v2 ][0 ]
216224 self .assertEqual (h ["c" ].edges ().count (loop ), 2 )
217225
@@ -221,7 +229,7 @@ class TestDGraphPickle(_RoundTrip, unittest.TestCase):
221229
222230 def test_in_edge_order_survives (self ):
223231 g = self .make ()
224- h = pickle . loads (pickle . dumps (g ))
232+ h = loads (dumps (g ))
225233 for v in g :
226234 self .assertEqual (
227235 [(e .v1 .name , e .v2 .name , e .cost ) for e in v .in_edges ()],
@@ -239,7 +247,7 @@ def test_vertex_and_edge_classes(self):
239247 a .tag , b .tag = "first" , "second"
240248 e = g .add_edge (a , b )
241249 e .weight = 11
242- for h in (pickle . loads (pickle . dumps (g )), g .copy ()):
250+ for h in (loads (dumps (g )), g .copy ()):
243251 self .assertIsInstance (h ["a" ], vcls )
244252 self .assertEqual ((h ["a" ].tag , h ["b" ].tag ), ("first" , "second" ))
245253 self .assertIsInstance (h .edges ()[0 ], WeightedEdge )
0 commit comments