[Python-checkins] r67955 - python/trunk/Lib/textwrap.py

georg.brandl python-checkins at python.org
Sat Dec 27 19:27:53 CET 2008


Author: georg.brandl
Date: Sat Dec 27 19:27:53 2008
New Revision: 67955
Log:
Follow-up to r67746 in order to restore backwards-compatibility for
those who (monkey-)patch TextWrapper.wordsep_re with a custom RE.
Modified:
 python/trunk/Lib/textwrap.py
Modified: python/trunk/Lib/textwrap.py
==============================================================================
--- python/trunk/Lib/textwrap.py	(original)
+++ python/trunk/Lib/textwrap.py	Sat Dec 27 19:27:53 2008
@@ -84,7 +84,7 @@
 # splits into
 # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
 # (after stripping out empty strings).
- wordsep_re = (
+ wordsep_re = re.compile(
 r'(\s+|' # any whitespace
 r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words
 r'(?<=[\w\!\"\'\&\.,円\?])-{2,}(?=\w))') # em-dash
@@ -93,7 +93,7 @@
 # "Hello there -- you goof-ball, use the -b option!"
 # splits into
 # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
- wordsep_simple_re = r'(\s+)'
+ wordsep_simple_re = re.compile(r'(\s+)')
 
 # XXX this is not locale- or charset-aware -- string.lowercase
 # is US-ASCII only (and therefore English-only)
@@ -124,6 +124,13 @@
 self.drop_whitespace = drop_whitespace
 self.break_on_hyphens = break_on_hyphens
 
+ # recompile the regexes for Unicode mode -- done in this clumsy way for
+ # backwards compatibility because it's rather common to monkey-patch
+ # the TextWrapper class' wordsep_re attribute.
+ self.wordsep_re_uni = re.compile(self.wordsep_re.pattern, re.U)
+ self.wordsep_simple_re_uni = re.compile(
+ self.wordsep_simple_re.pattern, re.U)
+
 
 # -- Private methods -----------------------------------------------
 # (possibly useful for subclasses to override)
@@ -160,12 +167,17 @@
 'use', ' ', 'the', ' ', '-b', ' ', option!'
 otherwise.
 """
- flags = re.UNICODE if isinstance(text, unicode) else 0
- if self.break_on_hyphens:
- pat = self.wordsep_re
+ if isinstance(text, unicode):
+ if self.break_on_hyphens:
+ pat = self.wordsep_re_uni
+ else:
+ pat = self.wordsep_simple_re_uni
 else:
- pat = self.wordsep_simple_re
- chunks = re.compile(pat, flags).split(text)
+ if self.break_on_hyphens:
+ pat = self.wordsep_re
+ else:
+ pat = self.wordsep_simple_re
+ chunks = pat.split(text)
 chunks = filter(None, chunks) # remove empty chunks
 return chunks
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /