From e429aea5bc693ffb50b7cdaefd0a6e24ead2afa1 Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Thu, 20 Aug 2015 10:30:28 +0200 Subject: Python 3: add workarounds for cgi.FieldStorage cgi.FieldStorage keeps some keys as str, some as the repr() of the byte-encoded key, duh. Fixes (well...): tests.test_cgiapp.test_form --- tests/cgiapp_data/form.cgi | 59 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/cgiapp_data/form.cgi b/tests/cgiapp_data/form.cgi index 2181998..c4c562d 100755 --- a/tests/cgiapp_data/form.cgi +++ b/tests/cgiapp_data/form.cgi @@ -1,11 +1,68 @@ #!/usr/bin/env python +from __future__ import print_function + import cgi +import six print('Content-type: text/plain') print('') -form = cgi.FieldStorage() +if six.PY3: + # Python 3: cgi.FieldStorage keeps some field names as unicode and some as + # the repr() of byte strings, duh. + + class FieldStorage(cgi.FieldStorage): + + def _key_candidates(self, key): + yield key + + try: + # assume bytes, coerce to str + try: + yield key.decode(self.encoding) + except UnicodeDecodeError: + pass + except AttributeError: + # assume str, coerce to bytes + try: + yield key.encode(self.encoding) + except UnicodeEncodeError: + pass + + def __getitem__(self, key): + + superobj = super(FieldStorage, self) + + error = None + + for candidate in self._key_candidates(key): + if isinstance(candidate, bytes): + # ouch + candidate = repr(candidate) + try: + return superobj.__getitem__(candidate) + except KeyError as e: + if error is None: + error = e + + # fall through, re-raise the first KeyError + raise error + + def __contains__(self, key): + superobj = super(FieldStorage, self) + + for candidate in self._key_candidates(key): + if superobj.__contains__(candidate): + return True + return False + +else: # PY2 + + FieldStorage = cgi.FieldStorage + + +form = FieldStorage() print('Filename: %s' % form['up'].filename) print('Name: %s' % form['name'].value) -- cgit v1.2.1