6
0
Selaa lähdekoodia

Proper file closing

Ensured closing of files that up to now were not properly closed.
blunk 3 vuotta sitten
vanhempi
commit
a59c005408

+ 1 - 0
pycs/frontend/endpoints/data/UploadFile.py

@@ -48,6 +48,7 @@ class UploadFile(View):
         # abort if there is no file entry in uploaded data
         if 'file' not in files.keys():
             return abort(400, "No file entry was found in uploaded data")
+        files['file'].close()
 
         # detect file type
         try:

+ 4 - 0
pycs/util/FileOperations.py

@@ -184,6 +184,7 @@ def resize_image(file_path: str, target_path: str, max_width: int, max_height: i
 
     # abort if file is smaller than desired
     if img_width < max_width and img_height < max_height:
+        image.close()
         return False
 
     # calculate target size
@@ -199,6 +200,9 @@ def resize_image(file_path: str, target_path: str, max_width: int, max_height: i
 
     # save to file
     resized_image.save(target_path, quality=DEFAULT_JPEG_QUALITY)
+
+    # close opened files.
+    image.close()
     return True
 
 

+ 1 - 1
tests/client/__init__.py

@@ -32,7 +32,7 @@ class FolderInformationTest(BaseTestCase):
             for i in range(10):
                 self._check(url, folder, dict(exists=True, count=i))
 
-                tempfile.NamedTemporaryFile(dir=folder, delete=False, suffix=".jpg")
+                tempfile.NamedTemporaryFile(dir=folder, delete=False, suffix=".jpg").close()
 
 
 class ListModelsAndLabelProviders(BaseTestCase):

+ 7 - 2
tests/client/file_tests.py

@@ -165,6 +165,8 @@ class FileGettingTests(_BaseFileTests):
         self.assertFalse(response.is_json)
         self.assertEqual(content, response.data)
 
+        response.close()
+
     def test_get_prev_next_file(self):
 
         for i in range(1, 6):
@@ -267,7 +269,7 @@ class FileResizingTests(_BaseFileTests):
     @pаtch_tpool_execute
     def test_resize_image(self, mocked_execute):
 
-        self.get(url_for("get_resized_file", file_id=4242, resolution=300), status_code=404)
+        self.get(url_for("get_resized_file", file_id=4242, resolution=300), status_code=404).close()
 
         file_uuid = str(uuid.uuid1())
         file, is_new = self.project.add_file(
@@ -290,6 +292,7 @@ class FileResizingTests(_BaseFileTests):
             self.assertFalse(response.is_json)
 
             returned_im = _im_from_bytes(response.data)
+            response.close()
 
             self.assertEqual(image.shape, returned_im.shape)
             self._compare_images(image, returned_im)
@@ -305,6 +308,7 @@ class FileResizingTests(_BaseFileTests):
             self.assertFalse(response.is_json)
 
             returned_im = _im_from_bytes(response.data)
+            response.close()
 
             self.assertEqual(sm_image.shape, returned_im.shape)
             self._compare_images(sm_image, returned_im)
@@ -360,11 +364,11 @@ class FileResizingTests(_BaseFileTests):
         url = url_for("get_cropped_file", file_id=file.id,
             resolution=300, crop_box="0x0x1x1")
         response = self.get(url, status_code=404)
+        response.close()
 
         file.path = save
         file.commit()
 
-
     @pаtch_tpool_execute
     def test_crop_image(self, mocked_execute):
 
@@ -390,6 +394,7 @@ class FileResizingTests(_BaseFileTests):
             self.assertFalse(response.is_json)
 
             returned_im = _im_from_bytes(response.data)
+            response.close()
 
             crop = _crop(image, BoundingBox(*box))
             self.assertEqual(crop.shape, returned_im.shape)