You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
11 KiB

36 years ago
36 years ago
36 years ago
Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r67154 | hirokazu.yamamoto | 2008-11-07 21:46:17 -0600 (Fri, 07 Nov 2008) | 1 line Issue #4071: ntpath.abspath returned an empty string for long unicode path. ........ r67157 | georg.brandl | 2008-11-08 05:47:44 -0600 (Sat, 08 Nov 2008) | 2 lines Don't use "HOWTO" as the title for all howto .tex files. ........ r67158 | georg.brandl | 2008-11-08 05:48:20 -0600 (Sat, 08 Nov 2008) | 2 lines Update "Documenting" a bit. Concentrate on Python-specifics. ........ r67159 | georg.brandl | 2008-11-08 06:52:25 -0600 (Sat, 08 Nov 2008) | 2 lines Fix warning. ........ r67175 | benjamin.peterson | 2008-11-08 19:44:32 -0600 (Sat, 08 Nov 2008) | 1 line update link ........ r67176 | benjamin.peterson | 2008-11-08 19:52:32 -0600 (Sat, 08 Nov 2008) | 1 line fix comment ........ r67189 | benjamin.peterson | 2008-11-11 15:56:06 -0600 (Tue, 11 Nov 2008) | 1 line use correct name ........ r67224 | georg.brandl | 2008-11-15 02:10:04 -0600 (Sat, 15 Nov 2008) | 2 lines #4324: fix getlocale() argument. ........ r67225 | brett.cannon | 2008-11-15 16:33:25 -0600 (Sat, 15 Nov 2008) | 1 line Clarify the docs for the 'strict' argument to httplib.HTTPConnection. ........ r67226 | brett.cannon | 2008-11-15 16:40:44 -0600 (Sat, 15 Nov 2008) | 4 lines The docs for httplib.HTTPConnection.putheader() have claimed for quite a while that their could be an arbitrary number of values passed in. Turns out the code did not match that. The code now matches the docs. ........ r67227 | georg.brandl | 2008-11-16 02:00:17 -0600 (Sun, 16 Nov 2008) | 2 lines #4316: fix configure.in markup problem. ........ r67234 | benjamin.peterson | 2008-11-16 11:54:55 -0600 (Sun, 16 Nov 2008) | 1 line run autoconf ........
17 years ago
16 years ago
16 years ago
  1. """A collection of string constants.
  2. Public module variables:
  3. whitespace -- a string containing all ASCII whitespace
  4. ascii_lowercase -- a string containing all ASCII lowercase letters
  5. ascii_uppercase -- a string containing all ASCII uppercase letters
  6. ascii_letters -- a string containing all ASCII letters
  7. digits -- a string containing all ASCII decimal digits
  8. hexdigits -- a string containing all ASCII hexadecimal digits
  9. octdigits -- a string containing all ASCII octal digits
  10. punctuation -- a string containing all ASCII punctuation characters
  11. printable -- a string containing all ASCII characters considered printable
  12. """
  13. __all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords",
  14. "digits", "hexdigits", "octdigits", "printable", "punctuation",
  15. "whitespace", "Formatter", "Template"]
  16. import _string
  17. # Some strings for ctype-style character classification
  18. whitespace = ' \t\n\r\v\f'
  19. ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
  20. ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  21. ascii_letters = ascii_lowercase + ascii_uppercase
  22. digits = '0123456789'
  23. hexdigits = digits + 'abcdef' + 'ABCDEF'
  24. octdigits = '01234567'
  25. punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
  26. printable = digits + ascii_letters + punctuation + whitespace
  27. # Functions which aren't available as string methods.
  28. # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
  29. def capwords(s, sep=None):
  30. """capwords(s [,sep]) -> string
  31. Split the argument into words using split, capitalize each
  32. word using capitalize, and join the capitalized words using
  33. join. If the optional second argument sep is absent or None,
  34. runs of whitespace characters are replaced by a single space
  35. and leading and trailing whitespace are removed, otherwise
  36. sep is used to split and join the words.
  37. """
  38. return (sep or ' ').join(x.capitalize() for x in s.split(sep))
  39. ####################################################################
  40. import re as _re
  41. from collections import ChainMap as _ChainMap
  42. class _TemplateMetaclass(type):
  43. pattern = r"""
  44. %(delim)s(?:
  45. (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
  46. (?P<named>%(id)s) | # delimiter and a Python identifier
  47. {(?P<braced>%(id)s)} | # delimiter and a braced identifier
  48. (?P<invalid>) # Other ill-formed delimiter exprs
  49. )
  50. """
  51. def __init__(cls, name, bases, dct):
  52. super(_TemplateMetaclass, cls).__init__(name, bases, dct)
  53. if 'pattern' in dct:
  54. pattern = cls.pattern
  55. else:
  56. pattern = _TemplateMetaclass.pattern % {
  57. 'delim' : _re.escape(cls.delimiter),
  58. 'id' : cls.idpattern,
  59. }
  60. cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
  61. class Template(metaclass=_TemplateMetaclass):
  62. """A string class for supporting $-substitutions."""
  63. delimiter = '$'
  64. idpattern = r'[_a-z][_a-z0-9]*'
  65. flags = _re.IGNORECASE
  66. def __init__(self, template):
  67. self.template = template
  68. # Search for $$, $identifier, ${identifier}, and any bare $'s
  69. def _invalid(self, mo):
  70. i = mo.start('invalid')
  71. lines = self.template[:i].splitlines(keepends=True)
  72. if not lines:
  73. colno = 1
  74. lineno = 1
  75. else:
  76. colno = i - len(''.join(lines[:-1]))
  77. lineno = len(lines)
  78. raise ValueError('Invalid placeholder in string: line %d, col %d' %
  79. (lineno, colno))
  80. def substitute(*args, **kws):
  81. if not args:
  82. raise TypeError("descriptor 'substitute' of 'Template' object "
  83. "needs an argument")
  84. self, *args = args # allow the "self" keyword be passed
  85. if len(args) > 1:
  86. raise TypeError('Too many positional arguments')
  87. if not args:
  88. mapping = kws
  89. elif kws:
  90. mapping = _ChainMap(kws, args[0])
  91. else:
  92. mapping = args[0]
  93. # Helper function for .sub()
  94. def convert(mo):
  95. # Check the most common path first.
  96. named = mo.group('named') or mo.group('braced')
  97. if named is not None:
  98. return str(mapping[named])
  99. if mo.group('escaped') is not None:
  100. return self.delimiter
  101. if mo.group('invalid') is not None:
  102. self._invalid(mo)
  103. raise ValueError('Unrecognized named group in pattern',
  104. self.pattern)
  105. return self.pattern.sub(convert, self.template)
  106. def safe_substitute(*args, **kws):
  107. if not args:
  108. raise TypeError("descriptor 'safe_substitute' of 'Template' object "
  109. "needs an argument")
  110. self, *args = args # allow the "self" keyword be passed
  111. if len(args) > 1:
  112. raise TypeError('Too many positional arguments')
  113. if not args:
  114. mapping = kws
  115. elif kws:
  116. mapping = _ChainMap(kws, args[0])
  117. else:
  118. mapping = args[0]
  119. # Helper function for .sub()
  120. def convert(mo):
  121. named = mo.group('named') or mo.group('braced')
  122. if named is not None:
  123. try:
  124. return str(mapping[named])
  125. except KeyError:
  126. return mo.group()
  127. if mo.group('escaped') is not None:
  128. return self.delimiter
  129. if mo.group('invalid') is not None:
  130. return mo.group()
  131. raise ValueError('Unrecognized named group in pattern',
  132. self.pattern)
  133. return self.pattern.sub(convert, self.template)
  134. ########################################################################
  135. # the Formatter class
  136. # see PEP 3101 for details and purpose of this class
  137. # The hard parts are reused from the C implementation. They're exposed as "_"
  138. # prefixed methods of str.
  139. # The overall parser is implemented in _string.formatter_parser.
  140. # The field name parser is implemented in _string.formatter_field_name_split
  141. class Formatter:
  142. def format(*args, **kwargs):
  143. if not args:
  144. raise TypeError("descriptor 'format' of 'Formatter' object "
  145. "needs an argument")
  146. self, *args = args # allow the "self" keyword be passed
  147. try:
  148. format_string, *args = args # allow the "format_string" keyword be passed
  149. except ValueError:
  150. if 'format_string' in kwargs:
  151. format_string = kwargs.pop('format_string')
  152. import warnings
  153. warnings.warn("Passing 'format_string' as keyword argument is "
  154. "deprecated", DeprecationWarning, stacklevel=2)
  155. else:
  156. raise TypeError("format() missing 1 required positional "
  157. "argument: 'format_string'") from None
  158. return self.vformat(format_string, args, kwargs)
  159. def vformat(self, format_string, args, kwargs):
  160. used_args = set()
  161. result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
  162. self.check_unused_args(used_args, args, kwargs)
  163. return result
  164. def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
  165. auto_arg_index=0):
  166. if recursion_depth < 0:
  167. raise ValueError('Max string recursion exceeded')
  168. result = []
  169. for literal_text, field_name, format_spec, conversion in \
  170. self.parse(format_string):
  171. # output the literal text
  172. if literal_text:
  173. result.append(literal_text)
  174. # if there's a field, output it
  175. if field_name is not None:
  176. # this is some markup, find the object and do
  177. # the formatting
  178. # handle arg indexing when empty field_names are given.
  179. if field_name == '':
  180. if auto_arg_index is False:
  181. raise ValueError('cannot switch from manual field '
  182. 'specification to automatic field '
  183. 'numbering')
  184. field_name = str(auto_arg_index)
  185. auto_arg_index += 1
  186. elif field_name.isdigit():
  187. if auto_arg_index:
  188. raise ValueError('cannot switch from manual field '
  189. 'specification to automatic field '
  190. 'numbering')
  191. # disable auto arg incrementing, if it gets
  192. # used later on, then an exception will be raised
  193. auto_arg_index = False
  194. # given the field_name, find the object it references
  195. # and the argument it came from
  196. obj, arg_used = self.get_field(field_name, args, kwargs)
  197. used_args.add(arg_used)
  198. # do any conversion on the resulting object
  199. obj = self.convert_field(obj, conversion)
  200. # expand the format spec, if needed
  201. format_spec, auto_arg_index = self._vformat(
  202. format_spec, args, kwargs,
  203. used_args, recursion_depth-1,
  204. auto_arg_index=auto_arg_index)
  205. # format the object and append to the result
  206. result.append(self.format_field(obj, format_spec))
  207. return ''.join(result), auto_arg_index
  208. def get_value(self, key, args, kwargs):
  209. if isinstance(key, int):
  210. return args[key]
  211. else:
  212. return kwargs[key]
  213. def check_unused_args(self, used_args, args, kwargs):
  214. pass
  215. def format_field(self, value, format_spec):
  216. return format(value, format_spec)
  217. def convert_field(self, value, conversion):
  218. # do any conversion on the resulting object
  219. if conversion is None:
  220. return value
  221. elif conversion == 's':
  222. return str(value)
  223. elif conversion == 'r':
  224. return repr(value)
  225. elif conversion == 'a':
  226. return ascii(value)
  227. raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
  228. # returns an iterable that contains tuples of the form:
  229. # (literal_text, field_name, format_spec, conversion)
  230. # literal_text can be zero length
  231. # field_name can be None, in which case there's no
  232. # object to format and output
  233. # if field_name is not None, it is looked up, formatted
  234. # with format_spec and conversion and then used
  235. def parse(self, format_string):
  236. return _string.formatter_parser(format_string)
  237. # given a field_name, find the object it references.
  238. # field_name: the field being looked up, e.g. "0.name"
  239. # or "lookup[3]"
  240. # used_args: a set of which args have been used
  241. # args, kwargs: as passed in to vformat
  242. def get_field(self, field_name, args, kwargs):
  243. first, rest = _string.formatter_field_name_split(field_name)
  244. obj = self.get_value(first, args, kwargs)
  245. # loop through the rest of the field_name, doing
  246. # getattr or getitem as needed
  247. for is_attr, i in rest:
  248. if is_attr:
  249. obj = getattr(obj, i)
  250. else:
  251. obj = obj[i]
  252. return obj, first