diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-01-13 18:42:32 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-01-13 18:42:32 +0100 | 
| commit | c7afcc1efaa1e02a77e7512c2f18d789b1c9a55b (patch) | |
| tree | c2b9988a5d380b5518320c42d008f8c8e734d5e8 | |
| parent | 3102608d4dbe5ac3813fc048cb6736208e9d8238 (diff) | |
| download | Ishtar-c7afcc1efaa1e02a77e7512c2f18d789b1c9a55b.tar.bz2 Ishtar-c7afcc1efaa1e02a77e7512c2f18d789b1c9a55b.zip | |
Close opened files
| -rw-r--r-- | ishtar_common/models.py | 16 | 
1 files changed, 10 insertions, 6 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 831479cd4..28ffcf7b2 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1164,6 +1164,7 @@ class ImageModel(models.Model, ImageContainerModel):          filename = os.path.splitext(os.path.split(self.image.name)[-1])[0]          old_path = self.image.path          filename = "%s.jpg" % filename +        image = None          try:              image = Image.open(self.image.file)              # convert to RGB @@ -1191,6 +1192,9 @@ class ImageModel(models.Model, ImageContainerModel):                  save=False)          except IOError:              pass +        finally: +            if image: +                image.close()          return super(ImageModel, self).save(*args, **kwargs)      def _get_thumb_name(self, filename): @@ -1804,8 +1808,8 @@ class QRCodeItem(models.Model, ImageContainerModel):              tmpdir_created = True          filename = tmpdir + os.sep + 'qrcode.png'          qr.png(filename, scale=settings.ISHTAR_QRCODE_SCALE) -        self.qrcode.save( -            "qrcode.png", File(open(filename, 'rb'))) +        with open(filename, 'rb') as qrfile: +            self.qrcode.save("qrcode.png", File(qrfile))          self.skip_history_when_saving = True          self._no_move = True          self.save() @@ -3571,8 +3575,8 @@ class DocumentTemplate(models.Model):              raise TemplateSyntaxError(str(e), 0)          except Exception as e:              raise TemplateSyntaxError(str(e), 0) -        output = open(output_name, 'wb') -        output.write(result) +        with open(output_name, 'wb') as output: +            output.write(result)          return output_name      def publish_labels(self, objects): @@ -3602,8 +3606,8 @@ class DocumentTemplate(models.Model):                  raise TemplateSyntaxError(str(e), e.lineno)              output_name = main_output_name + "-" + str(idx) + suffix              names.append(output_name) -            output = open(output_name, 'wb') -            output.write(result) +            with open(output_name, 'wb') as output: +                output.write(result)          output_name = main_output_name + suffix          o = OOoPy(infile=names[0], outfile=output_name)          if len(names) > 1: | 
