Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49d1b4f

Browse files
committed
Remove old backwards-compatibility classes from the cgi module.
1 parent fb16cf1 commit 49d1b4f

3 files changed

Lines changed: 11 additions & 190 deletions

File tree

Doc/library/cgi.rst

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ prints a simple piece of HTML::
6363
Using the cgi module
6464
--------------------
6565

66-
Begin by writing ``import cgi``. Do not use ``from cgi import *`` --- the
67-
module defines all sorts of names for its own use or for backward compatibility
68-
that you don't want in your namespace.
66+
Begin by writing ``import cgi``.
6967

7068
When you write a new script, consider adding the line::
7169

@@ -83,12 +81,11 @@ produced by :mod:`cgitb` provide information that can save you a lot of time in
8381
tracking down bugs. You can always remove the ``cgitb`` line later when you
8482
have tested your script and are confident that it works correctly.
8583

86-
To get at submitted form data, it's best to use the :class:`FieldStorage` class.
87-
The other classes defined in this module are provided mostly for backward
88-
compatibility. Instantiate it exactly once, without arguments. This reads the
89-
form contents from standard input or the environment (depending on the value of
90-
various environment variables set according to the CGI standard). Since it may
91-
consume standard input, it should be instantiated only once.
84+
To get at submitted form data, use the :class:`FieldStorage` class. Instantiate
85+
it exactly once, without arguments. This reads the form contents from standard
86+
input or the environment (depending on the value of various environment
87+
variables set according to the CGI standard). Since it may consume standard
88+
input, it should be instantiated only once.
9289

9390
The :class:`FieldStorage` instance can be indexed like a Python dictionary, and
9491
also supports the standard dictionary methods :meth:`__contains__` and
@@ -245,26 +242,6 @@ Using these methods you can write nice compact code::
245242
do_something(item)
246243

247244

248-
Old classes
249-
-----------
250-
251-
These classes, present in earlier versions of the :mod:`cgi` module, are still
252-
supported for backward compatibility. New applications should use the
253-
:class:`FieldStorage` class.
254-
255-
:class:`SvFormContentDict` stores single value form content as dictionary; it
256-
assumes each field name occurs in the form only once.
257-
258-
:class:`FormContentDict` stores multiple value form content as a dictionary (the
259-
form items are lists of values). Useful if your form contains multiple fields
260-
with the same name.
261-
262-
Other classes (:class:`FormContent`, :class:`InterpFormContentDict`) are present
263-
for backwards compatibility with really old applications only. If you still use
264-
these and would be inconvenienced when they disappeared from a next version of
265-
this module, drop me a note.
266-
267-
268245
.. _functions-in-cgi-module:
269246

270247
Functions

Lib/cgi.py

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
written in Python.
1616
"""
1717

18-
# XXX Perhaps there should be a slimmed version that doesn't contain
19-
# all those backwards compatible and debugging classes and functions?
20-
2118
# History
2219
# -------
2320
#
@@ -43,8 +40,7 @@
4340
import collections
4441
from io import StringIO
4542

46-
__all__ = ["MiniFieldStorage", "FieldStorage", "FormContentDict",
47-
"SvFormContentDict", "InterpFormContentDict", "FormContent",
43+
__all__ = ["MiniFieldStorage", "FieldStorage",
4844
"parse", "parse_qs", "parse_qsl", "parse_multipart",
4945
"parse_header", "print_exception", "print_environ",
5046
"print_form", "print_directory", "print_arguments",
@@ -777,124 +773,6 @@ def make_file(self):
777773
return tempfile.TemporaryFile("w+", encoding="utf-8", newline="\n")
778774

779775

780-
781-
# Backwards Compatibility Classes
782-
# ===============================
783-
784-
class FormContentDict(collections.Mapping):
785-
"""Form content as dictionary with a list of values per field.
786-
787-
form = FormContentDict()
788-
789-
form[key] -> [value, value, ...]
790-
key in form -> Boolean
791-
form.keys() -> [key, key, ...]
792-
form.values() -> [[val, val, ...], [val, val, ...], ...]
793-
form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...]
794-
form.dict == {key: [val, val, ...], ...}
795-
796-
"""
797-
def __init__(self, environ=os.environ, keep_blank_values=0, strict_parsing=0):
798-
self.dict = self.data = parse(environ=environ,
799-
keep_blank_values=keep_blank_values,
800-
strict_parsing=strict_parsing)
801-
self.query_string = environ['QUERY_STRING']
802-
803-
def __len__(self):
804-
return len(self.dict)
805-
806-
def __iter__(self):
807-
return iter(self.dict)
808-
809-
def __getitem__(self, key):
810-
return self.dict[key]
811-
812-
813-
class SvFormContentDict(FormContentDict):
814-
"""Form content as dictionary expecting a single value per field.
815-
816-
If you only expect a single value for each field, then form[key]
817-
will return that single value. It will raise an IndexError if
818-
that expectation is not true. If you expect a field to have
819-
possible multiple values, than you can use form.getlist(key) to
820-
get all of the values. values() and items() are a compromise:
821-
they return single strings where there is a single value, and
822-
lists of strings otherwise.
823-
824-
"""
825-
def __getitem__(self, key):
826-
if len(self.dict[key]) > 1:
827-
raise IndexError('expecting a single value')
828-
return self.dict[key][0]
829-
def getlist(self, key):
830-
return self.dict[key]
831-
def values(self):
832-
result = []
833-
for value in self.dict.values():
834-
if len(value) == 1:
835-
result.append(value[0])
836-
else: result.append(value)
837-
return result
838-
def items(self):
839-
result = []
840-
for key, value in self.dict.items():
841-
if len(value) == 1:
842-
result.append((key, value[0]))
843-
else: result.append((key, value))
844-
return result
845-
846-
847-
class InterpFormContentDict(SvFormContentDict):
848-
"""This class is present for backwards compatibility only."""
849-
def __getitem__(self, key):
850-
v = SvFormContentDict.__getitem__(self, key)
851-
if v[0] in '0123456789+-.':
852-
try: return int(v)
853-
except ValueError:
854-
try: return float(v)
855-
except ValueError: pass
856-
return v.strip()
857-
def values(self):
858-
result = []
859-
for key in self.keys():
860-
try:
861-
result.append(self[key])
862-
except IndexError:
863-
result.append(self.dict[key])
864-
return result
865-
def items(self):
866-
result = []
867-
for key in self.keys():
868-
try:
869-
result.append((key, self[key]))
870-
except IndexError:
871-
result.append((key, self.dict[key]))
872-
return result
873-
874-
875-
class FormContent(FormContentDict):
876-
"""This class is present for backwards compatibility only."""
877-
def values(self, key):
878-
if key in self.dict :return self.dict[key]
879-
else: return None
880-
def indexed_value(self, key, location):
881-
if key in self.dict:
882-
if len(self.dict[key]) > location:
883-
return self.dict[key][location]
884-
else: return None
885-
else: return None
886-
def value(self, key):
887-
if key in self.dict: return self.dict[key][0]
888-
else: return None
889-
def length(self, key):
890-
return len(self.dict[key])
891-
def stripped(self, key):
892-
if key in self.dict: return self.dict[key][0].strip()
893-
else: return None
894-
def pars(self):
895-
return self.dict
896-
897-
898776
# Test/debug code
899777
# ===============
900778

Lib/test/test_cgi.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -143,56 +143,22 @@ def test_strict(self):
143143
self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
144144

145145
env = {'QUERY_STRING': orig}
146-
fcd = cgi.FormContentDict(env)
147-
sd = cgi.SvFormContentDict(env)
148146
fs = cgi.FieldStorage(environ=env)
149147
if type(expect) == type({}):
150148
# test dict interface
151-
self.assertEqual(len(expect), len(fcd))
152-
self.assertEqual(norm(expect.keys()), norm(fcd.keys()))
153-
self.assertEqual(norm(expect.values()), norm(fcd.values()))
154-
self.assertEqual(norm(expect.items()), norm(fcd.items()))
155-
self.assertEqual(fcd.get("nonexistent field", "default"), "default")
156-
self.assertEqual(len(sd), len(fs))
157-
self.assertEqual(norm(sd.keys()), norm(fs.keys()))
149+
self.assertEqual(len(expect), len(fs))
150+
self.assertEqual(norm(expect.keys()), norm(fs.keys()))
151+
##self.assertEqual(norm(expect.values()), norm(fs.values()))
152+
##self.assertEqual(norm(expect.items()), norm(fs.items()))
158153
self.assertEqual(fs.getvalue("nonexistent field", "default"), "default")
159154
# test individual fields
160155
for key in expect.keys():
161156
expect_val = expect[key]
162-
self.assert_(key in fcd)
163-
self.assertEqual(norm(fcd[key]), norm(expect[key]))
164-
self.assertEqual(fcd.get(key, "default"), fcd[key])
165157
self.assert_(key in fs)
166158
if len(expect_val) > 1:
167-
single_value = 0
168-
else:
169-
single_value = 1
170-
try:
171-
val = sd[key]
172-
except IndexError:
173-
self.failIf(single_value)
174159
self.assertEqual(fs.getvalue(key), expect_val)
175160
else:
176-
self.assert_(single_value)
177-
self.assertEqual(val, expect_val[0])
178161
self.assertEqual(fs.getvalue(key), expect_val[0])
179-
self.assertEqual(norm(sd.getlist(key)), norm(expect_val))
180-
if single_value:
181-
self.assertEqual(norm(sd.values()),
182-
first_elts(norm(expect.values())))
183-
self.assertEqual(norm(sd.items()),
184-
first_second_elts(norm(expect.items())))
185-
186-
def test_weird_formcontentdict(self):
187-
# Test the weird FormContentDict classes
188-
env = {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"}
189-
expect = {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'}
190-
d = cgi.InterpFormContentDict(env)
191-
for k, v in expect.items():
192-
self.assertEqual(d[k], v)
193-
for k, v in d.items():
194-
self.assertEqual(expect[k], v)
195-
self.assertEqual(norm(expect.values()), norm(d.values()))
196162

197163
def test_log(self):
198164
cgi.log("Testing")

0 commit comments

Comments
 (0)