diff options
author | Marcel Hellkamp <marc@gsites.de> | 2022-06-29 09:36:25 +0200 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2022-06-29 09:36:57 +0200 |
commit | 1677de087ee6517eb3f09a4cffe6f91758ef07ba (patch) | |
tree | 4f691a224378ff176e8d10e43dd5a1dba9b1b24a | |
parent | e14127d88a3b633ebc0a56f7ef48c3be5f9e899c (diff) | |
download | bottle-1677de087ee6517eb3f09a4cffe6f91758ef07ba.tar.gz |
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.
-rwxr-xr-x | bottle.py | 5 |
1 files changed, 3 insertions, 2 deletions
@@ -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 |