diff roundup/admin.py @ 6818:b5e465646a7c

Try to fix warning about uncosed file descriptor ResourceWarning: unclosed file <_io.TextIOWrapper name='_test_export/priority.csv' mode='r' encoding='UTF-8'>
author John Rouillard <rouilj@ieee.org>
date Thu, 04 Aug 2022 17:15:09 -0400
parents 00fe67eb8a91
children f924af12ef50
line wrap: on
line diff
--- a/roundup/admin.py	Thu Aug 04 16:11:59 2022 -0400
+++ b/roundup/admin.py	Thu Aug 04 17:15:09 2022 -0400
@@ -1469,36 +1469,33 @@
             cl = self.get_class(classname)
 
             # ensure that the properties and the CSV file headings match
-            f = open(os.path.join(dir, file), 'r')
-            reader = csv.reader(f, colon_separated)
-            file_props = None
-            maxid = 1
-            # loop through the file and create a node for each entry
-            for n, r in enumerate(reader):
-                if file_props is None:
-                    file_props = r
-                    continue
+            with open(os.path.join(dir, file), 'r') as f:
+                reader = csv.reader(f, colon_separated)
+                file_props = None
+                maxid = 1
+                # loop through the file and create a node for each entry
+                for n, r in enumerate(reader):
+                    if file_props is None:
+                        file_props = r
+                        continue
 
-                if self.verbose:
-                    sys.stdout.write('\rImporting %s - %s' % (classname, n))
-                    sys.stdout.flush()
+                    if self.verbose:
+                        sys.stdout.write('\rImporting %s - %s' % (classname, n))
+                        sys.stdout.flush()
 
-                # do the import and figure the current highest nodeid
-                nodeid = cl.import_list(file_props, r)
-                if hasattr(cl, 'import_files') and import_files:
-                    cl.import_files(dir, nodeid)
-                maxid = max(maxid, int(nodeid))
+                    # do the import and figure the current highest nodeid
+                    nodeid = cl.import_list(file_props, r)
+                    if hasattr(cl, 'import_files') and import_files:
+                        cl.import_files(dir, nodeid)
+                    maxid = max(maxid, int(nodeid))
 
-            # (print to sys.stdout here to allow tests to squash it .. ugh)
-            print(file=sys.stdout)
-
-            f.close()
+                # (print to sys.stdout here to allow tests to squash it .. ugh)
+                print(file=sys.stdout)
 
             # import the journals
-            f = open(os.path.join(args[0], classname + '-journals.csv'), 'r')
-            reader = csv.reader(f, colon_separated)
-            cl.import_journals(reader)
-            f.close()
+            with open(os.path.join(args[0], classname + '-journals.csv'), 'r') as f:
+                reader = csv.reader(f, colon_separated)
+                cl.import_journals(reader)
 
             # (print to sys.stdout here to allow tests to squash it .. ugh)
             print('setting', classname, maxid+1, file=sys.stdout)

Roundup Issue Tracker: http://roundup-tracker.org/