From 1677de087ee6517eb3f09a4cffe6f91758ef07ba Mon Sep 17 00:00:00 2001 From: Marcel Hellkamp Date: Wed, 29 Jun 2022 09:36:25 +0200 Subject: Avoid "ResourceWarning: unclosed file" Python implements __del__ for NamedTemporaryFile (with some additional tricks) to make sure these are closed (and unlinked) if they are no longer referenced. This avoids the ResourceWarning you'd normally get if an unclosed file is garbage-collected. --- bottle.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bottle.py b/bottle.py index 6c852ab..1f78812 100755 --- a/bottle.py +++ b/bottle.py @@ -74,7 +74,7 @@ import base64, calendar, cgi, email.utils, functools, hmac, itertools,\ from types import FunctionType from datetime import date as datedate, datetime, timedelta -from tempfile import TemporaryFile +from tempfile import NamedTemporaryFile from traceback import format_exc, print_exc from unicodedata import normalize @@ -258,6 +258,7 @@ class lazy_attribute(object): setattr(cls, self.__name__, value) return value + ############################################################################### # Exceptions and Events ####################################################### ############################################################################### @@ -1355,7 +1356,7 @@ class BaseRequest(object): body.write(part) body_size += len(part) if not is_temp_file and body_size > self.MEMFILE_MAX: - body, tmp = TemporaryFile(mode='w+b'), body + body, tmp = NamedTemporaryFile(mode='w+b'), body body.write(tmp.getvalue()) del tmp is_temp_file = True -- cgit v1.2.1