1 #! /usr/local/bin/python
3 # NOTE: the above "/usr/local/bin/python" is NOT a mistake. It is
4 # intentionally NOT "/usr/bin/env python". On many systems
5 # (e.g. Solaris), /usr/local/bin is not in $PATH as passed to CGI
6 # scripts, and /usr/local/bin is the default directory where Python is
7 # installed, so /usr/bin/env would be unable to find python. Granted,
8 # binary installations by Linux vendors often install Python in
9 # /usr/bin. So let those vendors patch cgi.py to match their choice
12 """Support module for CGI (Common Gateway Interface) scripts.
14 This module defines a number of utilities for use by CGI scripts
18 # XXX Perhaps there should be a slimmed version that doesn't contain
19 # all those backwards compatible and debugging classes and functions?
24 # Michael McLay started this module. Steve Majewski changed the
25 # interface to SvFormContentDict and FormContentDict. The multipart
26 # parsing was inspired by code submitted by Andreas Paepcke. Guido van
27 # Rossum rewrote, reformatted and documented the module and is currently
28 # responsible for its maintenance.
37 from operator
import attrgetter
45 from cStringIO
import StringIO
47 from StringIO
import StringIO
49 __all__
= ["MiniFieldStorage", "FieldStorage", "FormContentDict",
50 "SvFormContentDict", "InterpFormContentDict", "FormContent",
51 "parse", "parse_qs", "parse_qsl", "parse_multipart",
52 "parse_header", "print_exception", "print_environ",
53 "print_form", "print_directory", "print_arguments",
54 "print_environ_usage", "escape"]
59 logfile
= "" # Filename to log to, if not empty
60 logfp
= None # File object to log to, if not None
62 def initlog(*allargs
):
63 """Write a log message, if there is a log file.
65 Even though this function is called initlog(), you should always
66 use log(); log is a variable that is set either to initlog
67 (initially), to dolog (once the log file has been opened), or to
68 nolog (when logging is disabled).
70 The first argument is a format string; the remaining arguments (if
71 any) are arguments to the % operator, so e.g.
72 log("%s: %s", "a", "b")
73 will write "a: b" to the log file, followed by a newline.
75 If the global logfp is not None, it should be a file object to
76 which log data is written.
78 If the global logfp is None, the global logfile may be a string
79 giving a filename to open, in append mode. This file should be
80 world writable!!! If the file can't be opened, logging is
81 silently disabled (since there is no safe place where we could
82 send an error message).
86 if logfile
and not logfp
:
88 logfp
= open(logfile
, "a")
97 def dolog(fmt
, *args
):
98 """Write a log message to the log file. See initlog() for docs."""
99 logfp
.write(fmt
%args
+ "\n")
102 """Dummy function, assigned to log when logging is disabled."""
105 log
= initlog
# The current logging function
111 # Maximum input we will accept when REQUEST_METHOD is POST
112 # 0 ==> unlimited input
115 def parse(fp
=None, environ
=os
.environ
, keep_blank_values
=0, strict_parsing
=0):
116 """Parse a query in the environment or from a file (default stdin)
118 Arguments, all optional:
120 fp : file pointer; default: sys.stdin
122 environ : environment dictionary; default: os.environ
124 keep_blank_values: flag indicating whether blank values in
125 URL encoded forms should be treated as blank strings.
126 A true value indicates that blanks should be retained as
127 blank strings. The default false value indicates that
128 blank values are to be ignored and treated as if they were
131 strict_parsing: flag indicating what to do with parsing errors.
132 If false (the default), errors are silently ignored.
133 If true, errors raise a ValueError exception.
137 if not 'REQUEST_METHOD' in environ
:
138 environ
['REQUEST_METHOD'] = 'GET' # For testing stand-alone
139 if environ
['REQUEST_METHOD'] == 'POST':
140 ctype
, pdict
= parse_header(environ
['CONTENT_TYPE'])
141 if ctype
== 'multipart/form-data':
142 return parse_multipart(fp
, pdict
)
143 elif ctype
== 'application/x-www-form-urlencoded':
144 clength
= int(environ
['CONTENT_LENGTH'])
145 if maxlen
and clength
> maxlen
:
146 raise ValueError, 'Maximum content length exceeded'
147 qs
= fp
.read(clength
)
149 qs
= '' # Unknown content-type
150 if 'QUERY_STRING' in environ
:
152 qs
= qs
+ environ
['QUERY_STRING']
155 qs
= qs
+ sys
.argv
[1]
156 environ
['QUERY_STRING'] = qs
# XXX Shouldn't, really
157 elif 'QUERY_STRING' in environ
:
158 qs
= environ
['QUERY_STRING']
164 environ
['QUERY_STRING'] = qs
# XXX Shouldn't, really
165 return parse_qs(qs
, keep_blank_values
, strict_parsing
)
168 def parse_qs(qs
, keep_blank_values
=0, strict_parsing
=0):
169 """Parse a query given as a string argument.
173 qs: URL-encoded query string to be parsed
175 keep_blank_values: flag indicating whether blank values in
176 URL encoded queries should be treated as blank strings.
177 A true value indicates that blanks should be retained as
178 blank strings. The default false value indicates that
179 blank values are to be ignored and treated as if they were
182 strict_parsing: flag indicating what to do with parsing errors.
183 If false (the default), errors are silently ignored.
184 If true, errors raise a ValueError exception.
187 for name
, value
in parse_qsl(qs
, keep_blank_values
, strict_parsing
):
189 dict[name
].append(value
)
194 def parse_qsl(qs
, keep_blank_values
=0, strict_parsing
=0):
195 """Parse a query given as a string argument.
199 qs: URL-encoded query string to be parsed
201 keep_blank_values: flag indicating whether blank values in
202 URL encoded queries should be treated as blank strings. A
203 true value indicates that blanks should be retained as blank
204 strings. The default false value indicates that blank values
205 are to be ignored and treated as if they were not included.
207 strict_parsing: flag indicating what to do with parsing errors. If
208 false (the default), errors are silently ignored. If true,
209 errors raise a ValueError exception.
211 Returns a list, as G-d intended.
213 pairs
= [s2
for s1
in qs
.split('&') for s2
in s1
.split(';')]
215 for name_value
in pairs
:
216 if not name_value
and not strict_parsing
:
218 nv
= name_value
.split('=', 1)
221 raise ValueError, "bad query field: %r" % (name_value
,)
222 # Handle case of a control-name with no equal sign
223 if keep_blank_values
:
227 if len(nv
[1]) or keep_blank_values
:
228 name
= urllib
.unquote(nv
[0].replace('+', ' '))
229 value
= urllib
.unquote(nv
[1].replace('+', ' '))
230 r
.append((name
, value
))
235 def parse_multipart(fp
, pdict
):
236 """Parse multipart input.
240 pdict: dictionary containing other parameters of content-type header
242 Returns a dictionary just like parse_qs(): keys are the field names, each
243 value is a list of values for that field. This is easy to use but not
244 much good if you are expecting megabytes to be uploaded -- in that case,
245 use the FieldStorage class instead which is much more flexible. Note
246 that content-type is the raw, unparsed contents of the content-type
249 XXX This does not parse nested multipart parts -- use FieldStorage for
252 XXX This should really be subsumed by FieldStorage altogether -- no
253 point in having two implementations of the same parsing algorithm.
254 Also, FieldStorage protects itself better against certain DoS attacks
255 by limiting the size of the data read in one chunk. The API here
256 does not support that kind of protection. This also affects parse()
257 since it can call parse_multipart().
261 if 'boundary' in pdict
:
262 boundary
= pdict
['boundary']
263 if not valid_boundary(boundary
):
264 raise ValueError, ('Invalid boundary in multipart form: %r'
267 nextpart
= "--" + boundary
268 lastpart
= "--" + boundary
+ "--"
272 while terminator
!= lastpart
:
276 # At start of next part. Read headers first.
277 headers
= mimetools
.Message(fp
)
278 clength
= headers
.getheader('content-length')
285 if maxlen
and bytes
> maxlen
:
286 raise ValueError, 'Maximum content length exceeded'
287 data
= fp
.read(bytes
)
290 # Read lines until end of part.
295 terminator
= lastpart
# End outer loop
298 terminator
= line
.strip()
299 if terminator
in (nextpart
, lastpart
):
307 # Strip final line terminator
309 if line
[-2:] == "\r\n":
311 elif line
[-1:] == "\n":
314 data
= "".join(lines
)
315 line
= headers
['content-disposition']
318 key
, params
= parse_header(line
)
319 if key
!= 'form-data':
322 name
= params
['name']
326 partdict
[name
].append(data
)
328 partdict
[name
] = [data
]
333 def parse_header(line
):
334 """Parse a Content-type like header.
336 Return the main content-type and a dictionary of options.
339 plist
= [x
.strip() for x
in line
.split(';')]
340 key
= plist
.pop(0).lower()
345 name
= p
[:i
].strip().lower()
346 value
= p
[i
+1:].strip()
347 if len(value
) >= 2 and value
[0] == value
[-1] == '"':
349 value
= value
.replace('\\\\', '\\').replace('\\"', '"')
354 # Classes for field storage
355 # =========================
357 class MiniFieldStorage
:
359 """Like FieldStorage, for use when no file uploads are possible."""
368 disposition_options
= {}
371 def __init__(self
, name
, value
):
372 """Constructor from field name and value."""
375 # self.file = StringIO(value)
378 """Return printable representation."""
379 return "MiniFieldStorage(%r, %r)" % (self
.name
, self
.value
)
384 """Store a sequence of fields, reading multipart/form-data.
386 This class provides naming, typing, files stored on disk, and
387 more. At the top level, it is accessible like a dictionary, whose
388 keys are the field names. (Note: None can occur as a field name.)
389 The items are either a Python list (if there's multiple values) or
390 another FieldStorage or MiniFieldStorage object. If it's a single
391 object, it has the following attributes:
393 name: the field name, if specified; otherwise None
395 filename: the filename, if specified; otherwise None; this is the
396 client side filename, *not* the file name on which it is
397 stored (that's a temporary file you don't deal with)
399 value: the value as a *string*; for file uploads, this
400 transparently reads the file every time you request the value
402 file: the file(-like) object from which you can read the data;
403 None if the data is stored a simple string
405 type: the content-type, or None if not specified
407 type_options: dictionary of options specified on the content-type
410 disposition: content-disposition, or None if not specified
412 disposition_options: dictionary of corresponding options
414 headers: a dictionary(-like) object (sometimes rfc822.Message or a
415 subclass thereof) containing *all* headers
417 The class is subclassable, mostly for the purpose of overriding
418 the make_file() method, which is called internally to come up with
419 a file open for reading and writing. This makes it possible to
420 override the default choice of storing all files in a temporary
421 directory and unlinking them as soon as they have been opened.
425 def __init__(self
, fp
=None, headers
=None, outerboundary
="",
426 environ
=os
.environ
, keep_blank_values
=0, strict_parsing
=0):
427 """Constructor. Read multipart/* until last part.
429 Arguments, all optional:
431 fp : file pointer; default: sys.stdin
432 (not used when the request method is GET)
434 headers : header dictionary-like object; default:
435 taken from environ as per CGI spec
437 outerboundary : terminating multipart boundary
438 (for internal use only)
440 environ : environment dictionary; default: os.environ
442 keep_blank_values: flag indicating whether blank values in
443 URL encoded forms should be treated as blank strings.
444 A true value indicates that blanks should be retained as
445 blank strings. The default false value indicates that
446 blank values are to be ignored and treated as if they were
449 strict_parsing: flag indicating what to do with parsing errors.
450 If false (the default), errors are silently ignored.
451 If true, errors raise a ValueError exception.
455 self
.keep_blank_values
= keep_blank_values
456 self
.strict_parsing
= strict_parsing
457 if 'REQUEST_METHOD' in environ
:
458 method
= environ
['REQUEST_METHOD'].upper()
459 if method
== 'GET' or method
== 'HEAD':
460 if 'QUERY_STRING' in environ
:
461 qs
= environ
['QUERY_STRING']
468 headers
= {'content-type':
469 "application/x-www-form-urlencoded"}
473 # Set default content-type for POST to what's traditional
474 headers
['content-type'] = "application/x-www-form-urlencoded"
475 if 'CONTENT_TYPE' in environ
:
476 headers
['content-type'] = environ
['CONTENT_TYPE']
477 if 'CONTENT_LENGTH' in environ
:
478 headers
['content-length'] = environ
['CONTENT_LENGTH']
479 self
.fp
= fp
or sys
.stdin
480 self
.headers
= headers
481 self
.outerboundary
= outerboundary
483 # Process content-disposition header
484 cdisp
, pdict
= "", {}
485 if 'content-disposition' in self
.headers
:
486 cdisp
, pdict
= parse_header(self
.headers
['content-disposition'])
487 self
.disposition
= cdisp
488 self
.disposition_options
= pdict
491 self
.name
= pdict
['name']
493 if 'filename' in pdict
:
494 self
.filename
= pdict
['filename']
496 # Process content-type header
498 # Honor any existing content-type header. But if there is no
499 # content-type header, use some sensible defaults. Assume
500 # outerboundary is "" at the outer level, but something non-false
501 # inside a multi-part. The default for an inner part is text/plain,
502 # but for an outer part it should be urlencoded. This should catch
503 # bogus clients which erroneously forget to include a content-type
506 # See below for what we do if there does exist a content-type header,
507 # but it happens to be something we don't understand.
508 if 'content-type' in self
.headers
:
509 ctype
, pdict
= parse_header(self
.headers
['content-type'])
510 elif self
.outerboundary
or method
!= 'POST':
511 ctype
, pdict
= "text/plain", {}
513 ctype
, pdict
= 'application/x-www-form-urlencoded', {}
515 self
.type_options
= pdict
516 self
.innerboundary
= ""
517 if 'boundary' in pdict
:
518 self
.innerboundary
= pdict
['boundary']
520 if 'content-length' in self
.headers
:
522 clen
= int(self
.headers
['content-length'])
525 if maxlen
and clen
> maxlen
:
526 raise ValueError, 'Maximum content length exceeded'
529 self
.list = self
.file = None
531 if ctype
== 'application/x-www-form-urlencoded':
532 self
.read_urlencoded()
533 elif ctype
[:10] == 'multipart/':
534 self
.read_multi(environ
, keep_blank_values
, strict_parsing
)
539 """Return a printable representation."""
540 return "FieldStorage(%r, %r, %r)" % (
541 self
.name
, self
.filename
, self
.value
)
544 return iter(self
.keys())
546 def __getattr__(self
, name
):
548 raise AttributeError, name
551 value
= self
.file.read()
553 elif self
.list is not None:
559 def __getitem__(self
, key
):
560 """Dictionary style indexing."""
561 if self
.list is None:
562 raise TypeError, "not indexable"
564 for item
in self
.list:
565 if item
.name
== key
: found
.append(item
)
573 def getvalue(self
, key
, default
=None):
574 """Dictionary style get() method, including 'value' lookup."""
577 if type(value
) is type([]):
578 return map(attrgetter('value'), value
)
584 def getfirst(self
, key
, default
=None):
585 """ Return the first value received."""
588 if type(value
) is type([]):
589 return value
[0].value
595 def getlist(self
, key
):
596 """ Return list of received values."""
599 if type(value
) is type([]):
600 return map(attrgetter('value'), value
)
607 """Dictionary style keys() method."""
608 if self
.list is None:
609 raise TypeError, "not indexable"
610 return list(set(item
.name
for item
in self
.list))
612 def has_key(self
, key
):
613 """Dictionary style has_key() method."""
614 if self
.list is None:
615 raise TypeError, "not indexable"
616 return any(item
.name
== key
for item
in self
.list)
618 def __contains__(self
, key
):
619 """Dictionary style __contains__ method."""
620 if self
.list is None:
621 raise TypeError, "not indexable"
622 return any(item
.name
== key
for item
in self
.list)
625 """Dictionary style len(x) support."""
626 return len(self
.keys())
628 def __nonzero__(self
):
629 return bool(self
.list)
631 def read_urlencoded(self
):
632 """Internal: read data in query string format."""
633 qs
= self
.fp
.read(self
.length
)
634 self
.list = list = []
635 for key
, value
in parse_qsl(qs
, self
.keep_blank_values
,
636 self
.strict_parsing
):
637 list.append(MiniFieldStorage(key
, value
))
640 FieldStorageClass
= None
642 def read_multi(self
, environ
, keep_blank_values
, strict_parsing
):
643 """Internal: read a part that is itself multipart."""
644 ib
= self
.innerboundary
645 if not valid_boundary(ib
):
646 raise ValueError, 'Invalid boundary in multipart form: %r' % (ib
,)
648 klass
= self
.FieldStorageClass
or self
.__class
__
649 part
= klass(self
.fp
, {}, ib
,
650 environ
, keep_blank_values
, strict_parsing
)
651 # Throw first part away
653 headers
= rfc822
.Message(self
.fp
)
654 part
= klass(self
.fp
, headers
, ib
,
655 environ
, keep_blank_values
, strict_parsing
)
656 self
.list.append(part
)
659 def read_single(self
):
660 """Internal: read an atomic part."""
668 bufsize
= 8*1024 # I/O buffering size for copy to file
670 def read_binary(self
):
671 """Internal: read binary data."""
672 self
.file = self
.make_file('b')
676 data
= self
.fp
.read(min(todo
, self
.bufsize
))
680 self
.file.write(data
)
681 todo
= todo
- len(data
)
683 def read_lines(self
):
684 """Internal: read lines until EOF or outerboundary."""
685 self
.file = self
.__file
= StringIO()
686 if self
.outerboundary
:
687 self
.read_lines_to_outerboundary()
689 self
.read_lines_to_eof()
691 def __write(self
, line
):
692 if self
.__file
is not None:
693 if self
.__file
.tell() + len(line
) > 1000:
694 self
.file = self
.make_file('')
695 self
.file.write(self
.__file
.getvalue())
697 self
.file.write(line
)
699 def read_lines_to_eof(self
):
700 """Internal: read lines until EOF."""
702 line
= self
.fp
.readline(1<<16)
708 def read_lines_to_outerboundary(self
):
709 """Internal: read lines until outerboundary."""
710 next
= "--" + self
.outerboundary
713 last_line_lfend
= True
715 line
= self
.fp
.readline(1<<16)
719 if line
[:2] == "--" and last_line_lfend
:
720 strippedline
= line
.strip()
721 if strippedline
== next
:
723 if strippedline
== last
:
727 if line
[-2:] == "\r\n":
730 last_line_lfend
= True
731 elif line
[-1] == "\n":
734 last_line_lfend
= True
737 last_line_lfend
= False
738 self
.__write
(odelim
+ line
)
740 def skip_lines(self
):
741 """Internal: skip lines until outer boundary if defined."""
742 if not self
.outerboundary
or self
.done
:
744 next
= "--" + self
.outerboundary
746 last_line_lfend
= True
748 line
= self
.fp
.readline(1<<16)
752 if line
[:2] == "--" and last_line_lfend
:
753 strippedline
= line
.strip()
754 if strippedline
== next
:
756 if strippedline
== last
:
759 last_line_lfend
= line
.endswith('\n')
761 def make_file(self
, binary
=None):
762 """Overridable: return a readable & writable file.
764 The file will be used as follows:
765 - data is written to it
767 - data is read from it
769 The 'binary' argument is unused -- the file is always opened
772 This version opens a temporary file for reading and writing,
773 and immediately deletes (unlinks) it. The trick (on Unix!) is
774 that the file can still be used, but it can't be opened by
775 another process, and it will automatically be deleted when it
776 is closed or when the current process terminates.
778 If you want a more permanent file, you derive a class which
779 overrides this method. If you want a visible temporary file
780 that is nevertheless automatically deleted when the script
781 terminates, try defining a __del__ method in a derived class
782 which unlinks the temporary files you have created.
786 return tempfile
.TemporaryFile("w+b")
790 # Backwards Compatibility Classes
791 # ===============================
793 class FormContentDict(UserDict
.UserDict
):
794 """Form content as dictionary with a list of values per field.
796 form = FormContentDict()
798 form[key] -> [value, value, ...]
799 key in form -> Boolean
800 form.keys() -> [key, key, ...]
801 form.values() -> [[val, val, ...], [val, val, ...], ...]
802 form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...]
803 form.dict == {key: [val, val, ...], ...}
806 def __init__(self
, environ
=os
.environ
, keep_blank_values
=0, strict_parsing
=0):
807 self
.dict = self
.data
= parse(environ
=environ
,
808 keep_blank_values
=keep_blank_values
,
809 strict_parsing
=strict_parsing
)
810 self
.query_string
= environ
['QUERY_STRING']
813 class SvFormContentDict(FormContentDict
):
814 """Form content as dictionary expecting a single value per field.
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.
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
]
833 for value
in self
.dict.values():
835 result
.append(value
[0])
836 else: result
.append(value
)
840 for key
, value
in self
.dict.items():
842 result
.append((key
, value
[0]))
843 else: result
.append((key
, value
))
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+-.':
855 except ValueError: pass
859 for key
in self
.keys():
861 result
.append(self
[key
])
863 result
.append(self
.dict[key
])
867 for key
in self
.keys():
869 result
.append((key
, self
[key
]))
871 result
.append((key
, self
.dict[key
]))
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
]
880 def indexed_value(self
, key
, location
):
882 if len(self
.dict[key
]) > location
:
883 return self
.dict[key
][location
]
886 def value(self
, key
):
887 if key
in self
.dict: return self
.dict[key
][0]
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()
901 def test(environ
=os
.environ
):
902 """Robust test CGI script, usable as main program.
904 Write minimal HTTP headers and dump all information provided to
905 the script in HTML form.
908 print "Content-type: text/html"
910 sys
.stderr
= sys
.stdout
912 form
= FieldStorage() # Replace with other classes to test those
916 print_environ(environ
)
917 print_environ_usage()
919 exec "testing print_exception() -- <I>italics?</I>"
922 print "<H3>What follows is a test, not an actual exception:</H3>"
927 print "<H1>Second try with a small maxlen...</H1>"
932 form
= FieldStorage() # Replace with other classes to test those
936 print_environ(environ
)
940 def print_exception(type=None, value
=None, tb
=None, limit
=None):
942 type, value
, tb
= sys
.exc_info()
945 print "<H3>Traceback (most recent call last):</H3>"
946 list = traceback
.format_tb(tb
, limit
) + \
947 traceback
.format_exception_only(type, value
)
948 print "<PRE>%s<B>%s</B></PRE>" % (
949 escape("".join(list[:-1])),
954 def print_environ(environ
=os
.environ
):
955 """Dump the shell environment as HTML."""
956 keys
= environ
.keys()
959 print "<H3>Shell Environment:</H3>"
962 print "<DT>", escape(key
), "<DD>", escape(environ
[key
])
966 def print_form(form
):
967 """Dump the contents of a form as HTML."""
971 print "<H3>Form Contents:</H3>"
973 print "<P>No form fields."
976 print "<DT>" + escape(key
) + ":",
978 print "<i>" + escape(repr(type(value
))) + "</i>"
979 print "<DD>" + escape(repr(value
))
983 def print_directory():
984 """Dump the current directory as HTML."""
986 print "<H3>Current Working Directory:</H3>"
989 except os
.error
, msg
:
990 print "os.error:", escape(str(msg
))
995 def print_arguments():
997 print "<H3>Command Line Arguments:</H3>"
1002 def print_environ_usage():
1003 """Dump a list of environment variables used by CGI as HTML."""
1005 <H3>These environment variables could have been set:</H3>
1015 <LI>GATEWAY_INTERFACE
1033 In addition, HTTP headers sent by the server may be passed in the
1034 environment as well. Here are some common variable names:
1049 def escape(s
, quote
=None):
1050 '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
1051 If the optional flag quote is true, the quotation mark character (")
1052 is also translated.'''
1053 s
= s
.replace("&", "&") # Must be done first!
1054 s
= s
.replace("<", "<")
1055 s
= s
.replace(">", ">")
1057 s
= s
.replace('"', """)
1060 def valid_boundary(s
, _vb_pattern
="^[ -~]{0,200}[!-~]$"):
1062 return re
.match(_vb_pattern
, s
)
1067 # Call test() when this file is run as a script (not imported as a module)
1068 if __name__
== '__main__':