summaryrefslogtreecommitdiff
path: root/tests/cgiapp_data/form.cgi
diff options
context:
space:
mode:
authorMiro Hron?ok <miro@hroncok.cz>2018-06-08 18:49:42 +0200
committerMiro Hron?ok <miro@hroncok.cz>2018-06-08 18:49:42 +0200
commitbb9bdf7e5b5cb0d3458242c5b8ba6134efb282a4 (patch)
treee3f03e7ce1f234e0c91075da6d0b56040f693162 /tests/cgiapp_data/form.cgi
downloadpaste-git-bb9bdf7e5b5cb0d3458242c5b8ba6134efb282a4.tar.gz
Don't raise StopIteration from generator, return instead
See https://www.python.org/dev/peps/pep-0479/
Diffstat (limited to 'tests/cgiapp_data/form.cgi')
-rwxr-xr-xtests/cgiapp_data/form.cgi69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/cgiapp_data/form.cgi b/tests/cgiapp_data/form.cgi
new file mode 100755
index 0000000..c4c562d
--- /dev/null
+++ b/tests/cgiapp_data/form.cgi
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+
+import cgi
+import six
+
+print('Content-type: text/plain')
+print('')
+
+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)
+print('Content: %s' % form['up'].file.read())