Patchwork [5,of,5,foldmap-in-C,V2] encoding: use parsers.asciiupper when available

login
register
mail settings
Submitter Siddharth Agarwal
Date April 1, 2015, 9 p.m.
Message ID <050fbd52f265778a3a07.1427922003@devbig136.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8428/
State Accepted
Commit ac08de78de7fe6c9710d428cb251f40e899657e3
Headers show

Comments

Siddharth Agarwal - April 1, 2015, 9 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1427840529 25200
#      Tue Mar 31 15:22:09 2015 -0700
# Node ID 050fbd52f265778a3a07803ff1e1227aa6710a08
# Parent  492a4a1a47563b318db7b63bc6b6008004dd6d26
encoding: use parsers.asciiupper when available

This is used on Windows and Cygwin, and the gains from this are expected to be
similar to what was seen in 80f2b63dd83a.
Matt Mackall - April 1, 2015, 11:56 p.m.
On Wed, 2015-04-01 at 14:00 -0700, Siddharth Agarwal wrote:
> # HG changeset patch
> # User Siddharth Agarwal <sid0@fb.com>
> # Date 1427840529 25200
> #      Tue Mar 31 15:22:09 2015 -0700
> # Node ID 050fbd52f265778a3a07803ff1e1227aa6710a08
> # Parent  492a4a1a47563b318db7b63bc6b6008004dd6d26
> encoding: use parsers.asciiupper when available

These are queued for default, thanks.

Patch

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -296,6 +296,22 @@ 
     asciilower = impl
     return impl(s)
 
+def _asciiupper(s):
+    '''convert a string to uppercase if ASCII
+
+    Raises UnicodeDecodeError if non-ASCII characters are found.'''
+    s.decode('ascii')
+    return s.upper()
+
+def asciiupper(s):
+    # delay importing avoids cyclic dependency around "parsers" in
+    # pure Python build (util => i18n => encoding => parsers => util)
+    import parsers
+    impl = getattr(parsers, 'asciiupper', _asciiupper)
+    global asciiupper
+    asciiupper = impl
+    return impl(s)
+
 def lower(s):
     "best-effort encoding-aware case-folding of local string s"
     try:
@@ -320,8 +336,7 @@ 
 def upper(s):
     "best-effort encoding-aware case-folding of local string s"
     try:
-        s.decode('ascii') # throw exception for non-ASCII character
-        return s.upper()
+        return asciiupper(s)
     except UnicodeDecodeError:
         pass
     try: