diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2018-01-24 04:57:00 +0100 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2018-01-24 05:01:20 +0100 |
| commit | b82ea0a0189c21941fc6889d63257d9c26ae02a9 (patch) | |
| tree | 5b0e766564fe550c96f1b261550a7c5a1f5a31ed /kconfiglib.py | |
| parent | 1a07bf708f001611e12ef86df969aa446446a18a (diff) | |
Enable universal newlines mode for Python 2
Use the "U" flag to open() rather than io.open() to avoid a ~14% parsing
performance hit. See comment.
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 0deda87..441a8c0 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -1110,12 +1110,12 @@ class Kconfig(object): was set when the configuration was loaded. """ try: - return open(filename) + return open(filename, _UNIVERSAL_NEWLINES_MODE) except IOError as e: if not os.path.isabs(filename) and self.srctree is not None: filename = os.path.join(self.srctree, filename) try: - return open(filename) + return open(filename, _UNIVERSAL_NEWLINES_MODE) except IOError as e2: # This is needed for Python 3, because e2 is deleted after # the try block: @@ -4410,3 +4410,31 @@ _REL_TO_STR = { LESS_EQUAL: "<=", UNEQUAL: "!=", } + +# Enable universal newlines mode on Python 2 to ease interoperability between +# Linux and Windows. It's already the default on Python 3. +# +# The "U" flag would currently work for both Python 2 and 3, but it's +# deprecated on Python 3, so play it future-safe. +# +# A simpler solution would be to use io.open(), which defaults to universal +# newlines on both Python 2 and 3 (and is an alias for open() on Python 3), but +# it's appreciably slower on Python 2: +# +# Parsing x86 Kconfigs on Python 2 +# +# with open(..., "rU"): +# +# real 0m0.930s +# user 0m0.905s +# sys 0m0.025s +# +# with io.open(): +# +# real 0m1.069s +# user 0m1.040s +# sys 0m0.029s +# +# There's no appreciable performance difference between "r" and "rU" for +# parsing performance on Python 2. +_UNIVERSAL_NEWLINES_MODE = "rU" if sys.version_info[0] < 3 else "r" |
