diff options
author | Benjamin Peterson <benjamin@python.org> | 2019-08-29 05:05:57 -0700 |
---|---|---|
committer | Chris Dent <cdent@anticdent.org> | 2019-08-29 13:05:57 +0100 |
commit | cdd2a9a58c59b8e599aa42b66b3c55ea47c7a4b7 (patch) | |
tree | 447ac76f8db6b158e7217377e245443a4f911440 /paste | |
parent | 9eff34df99e9488cec65b21c95e777414c61a379 (diff) | |
download | paste-git-cdd2a9a58c59b8e599aa42b66b3c55ea47c7a4b7.tar.gz |
Avoid copying FieldStorage if possible. (#30)
On Python 3, cgi.FieldStorage has a __del__ method that closes the underlying file [1]. This means that if the copy made from UnicodeMultiDict._decode_value is garbage collected, the file underlying the original FieldStorage will be closed! Fix this by not copying FieldStorage if it is not required by decode_keys=False. I cannot think of a nice way to fix this problem if decode_keys=True.
[1] https://github.com/python/cpython/commit/f79126f373a9d5c9b584a8db736fe490fcbfa77a
Diffstat (limited to 'paste')
-rw-r--r-- | paste/util/multidict.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/paste/util/multidict.py b/paste/util/multidict.py index 0b63ebb..0a304d3 100644 --- a/paste/util/multidict.py +++ b/paste/util/multidict.py @@ -265,11 +265,13 @@ class UnicodeMultiDict(DictMixin): """ if isinstance(value, cgi.FieldStorage): # decode FieldStorage's field name and filename - value = copy.copy(value) - if self.decode_keys and isinstance(value.name, six.binary_type): - value.name = value.name.decode(self.encoding, self.errors) - if six.PY2: - value.filename = value.filename.decode(self.encoding, self.errors) + decode_name = self.decode_keys and isinstance(value.name, six.binary_type) + if six.PY2 or decode_name: + value = copy.copy(value) + if decode_name: + value.name = value.name.decode(self.encoding, self.errors) + if six.PY2: + value.filename = value.filename.decode(self.encoding, self.errors) else: try: value = value.decode(self.encoding, self.errors) |