]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #22410: Module level functions in the re module now cache compiled
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 30 Oct 2014 22:56:45 +0000 (00:56 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 30 Oct 2014 22:56:45 +0000 (00:56 +0200)
locale-dependent regular expressions taking into account the locale.

1  2 
Lib/re.py
Lib/test/test_re.py
Misc/NEWS

diff --cc Lib/re.py
index 8f5b3995a6a5fcdce6cf0c8c4c0e2091474e357b,46cea2bd691f09b49520d07253e2854c8aea4c08..e731a69bca31921f0ac0ffbc9ced37c4403aeeea
+++ b/Lib/re.py
@@@ -122,15 -122,13 +122,16 @@@ This module also defines an exception '
  import sys
  import sre_compile
  import sre_parse
+ import _locale
  
  # public symbols
 -__all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", "findall",
 -    "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X",
 -    "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
 -    "UNICODE", "error" ]
 +__all__ = [
 +    "match", "fullmatch", "search", "sub", "subn", "split",
 +    "findall", "finditer", "compile", "purge", "template", "escape",
 +    "error", "A", "I", "L", "M", "S", "X", "U",
 +    "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
 +    "UNICODE",
 +]
  
  __version__ = "2.2.1"
  
@@@ -273,10 -273,14 +274,12 @@@ _pattern_type = type(sre_compile.compil
  _MAXCACHE = 512
  def _compile(pattern, flags):
      # internal: compile pattern
 -    bypass_cache = flags & DEBUG
 -    if not bypass_cache:
 -        try:
 -            p, loc = _cache[type(pattern), pattern, flags]
 -            if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
 -                return p
 -        except KeyError:
 -            pass
 +    try:
-         return _cache[type(pattern), pattern, flags]
++        p, loc = _cache[type(pattern), pattern, flags]
++        if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
++            return p
 +    except KeyError:
 +        pass
      if isinstance(pattern, _pattern_type):
          if flags:
              raise ValueError(
      if not sre_compile.isstring(pattern):
          raise TypeError("first argument must be string or compiled pattern")
      p = sre_compile.compile(pattern, flags)
 -    if not bypass_cache:
 +    if not (flags & DEBUG):
          if len(_cache) >= _MAXCACHE:
              _cache.clear()
-         _cache[type(pattern), pattern, flags] = p
+         if p.flags & LOCALE:
+             loc = _locale.setlocale(_locale.LC_CTYPE)
+         else:
+             loc = None
+         _cache[type(pattern), pattern, flags] = p, loc
      return p
  
  def _compile_repl(repl, pattern):
Simple merge
diff --cc Misc/NEWS
index 76fdd8ddc7e8488e50ea8d5cc948c2f35488c3e0,52bbcf1702af376268c0c3401046f7a4622aaa9b..dd2aeb6f4d8226c108c4074e6e4f567f11e02904
+++ b/Misc/NEWS
@@@ -180,9 -33,9 +180,12 @@@ Core and Builtin
  Library
  -------
  
+ - Issue #22410: Module level functions in the re module now cache compiled
+   locale-dependent regular expressions taking into account the locale.
 +- Issue #22759: Query methods on pathlib.Path() (exists(), is_dir(), etc.)
 +  now return False when the underlying stat call raises NotADirectoryError.
 +
  - Issue #8876: distutils now falls back to copying files when hard linking
    doesn't work.  This allows use with special filesystems such as VirtualBox
    shared folders.