@@ -291,14 +291,20 @@ def test_filter_words_edge_cases(self):
291
291
292
292
def test_load_filtered_policy_with_comments (self ):
293
293
"""Test loading filtering policies with comments."""
294
- temp_file = tempfile .NamedTemporaryFile (delete = False )
295
- try :
296
- shutil .copyfile (get_examples ("rbac_with_domains_policy.csv" ), temp_file .name )
294
+ import tempfile
295
+ import shutil
296
+
297
+ with tempfile .NamedTemporaryFile (mode = "w+" , delete = False ) as temp_file :
298
+ with open (get_examples ("rbac_with_domains_policy.csv" ), "r" ) as source :
299
+ shutil .copyfileobj (source , temp_file )
297
300
298
- with open ( temp_file .name , "a" ) as f :
299
- f . write ( " \n # This is a comment \n p, admin, domain1, data3, read" )
301
+ temp_file .write ( " \n # This is a comment \n p, admin, domain1, data3, read" )
302
+ temp_file . flush ( )
300
303
301
- adapter = FilteredFileAdapter (temp_file .name )
304
+ temp_path = temp_file .name
305
+
306
+ try :
307
+ adapter = FilteredFileAdapter (temp_path )
302
308
e = casbin .Enforcer (get_examples ("rbac_with_domains_model.conf" ), adapter )
303
309
filter = Filter ()
304
310
filter .P = ["" , "domain1" ]
@@ -307,4 +313,7 @@ def test_load_filtered_policy_with_comments(self):
307
313
e .load_filtered_policy (filter )
308
314
self .assertTrue (e .has_policy (["admin" , "domain1" , "data3" , "read" ]))
309
315
finally :
310
- os .unlink (temp_file .name )
316
+ try :
317
+ os .unlink (temp_path )
318
+ except OSError :
319
+ pass
0 commit comments