3131# Imports
3232# =======
3333
34- from operator import attrgetter
3534from io import StringIO
3635import sys
3736import os
3837import urllib .parse
3938import email .parser
4039from warnings import warn
40+ import html
4141
4242__all__ = ["MiniFieldStorage" , "FieldStorage" ,
4343 "parse" , "parse_qs" , "parse_qsl" , "parse_multipart" ,
@@ -800,8 +800,8 @@ def print_exception(type=None, value=None, tb=None, limit=None):
800800 list = traceback .format_tb (tb , limit ) + \
801801 traceback .format_exception_only (type , value )
802802 print ("<PRE>%s<B>%s</B></PRE>" % (
803- escape ("" .join (list [:- 1 ])),
804- escape (list [- 1 ]),
803+ html . escape ("" .join (list [:- 1 ])),
804+ html . escape (list [- 1 ]),
805805 ))
806806 del tb
807807
@@ -812,7 +812,7 @@ def print_environ(environ=os.environ):
812812 print ("<H3>Shell Environment:</H3>" )
813813 print ("<DL>" )
814814 for key in keys :
815- print ("<DT>" , escape (key ), "<DD>" , escape (environ [key ]))
815+ print ("<DT>" , html . escape (key ), "<DD>" , html . escape (environ [key ]))
816816 print ("</DL>" )
817817 print ()
818818
@@ -825,10 +825,10 @@ def print_form(form):
825825 print ("<P>No form fields." )
826826 print ("<DL>" )
827827 for key in keys :
828- print ("<DT>" + escape (key ) + ":" , end = ' ' )
828+ print ("<DT>" + html . escape (key ) + ":" , end = ' ' )
829829 value = form [key ]
830- print ("<i>" + escape (repr (type (value ))) + "</i>" )
831- print ("<DD>" + escape (repr (value )))
830+ print ("<i>" + html . escape (repr (type (value ))) + "</i>" )
831+ print ("<DD>" + html . escape (repr (value )))
832832 print ("</DL>" )
833833 print ()
834834
@@ -839,9 +839,9 @@ def print_directory():
839839 try :
840840 pwd = os .getcwd ()
841841 except os .error as msg :
842- print ("os.error:" , escape (str (msg )))
842+ print ("os.error:" , html . escape (str (msg )))
843843 else :
844- print (escape (pwd ))
844+ print (html . escape (pwd ))
845845 print ()
846846
847847def print_arguments ():
@@ -899,16 +899,17 @@ def print_environ_usage():
899899# =========
900900
901901def escape (s , quote = None ):
902- '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
903- If the optional flag quote is true, the quotation mark character (")
904- is also translated.'''
902+ """Deprecated API."""
903+ warn ( "cgi.escape is deprecated, use html.escape instead" ,
904+ PendingDeprecationWarning , stacklevel = 2 )
905905 s = s .replace ("&" , "&" ) # Must be done first!
906906 s = s .replace ("<" , "<" )
907907 s = s .replace (">" , ">" )
908908 if quote :
909909 s = s .replace ('"' , """ )
910910 return s
911911
912+
912913def valid_boundary (s , _vb_pattern = "^[ -~]{0,200}[!-~]$" ):
913914 import re
914915 return re .match (_vb_pattern , s )
0 commit comments