Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-103977: compile re expressions in platform.py only if required #103981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 30, 2023
Next Next commit
compile re expressions only if required
  • Loading branch information
eendebakpt committed Apr 21, 2023
commit c8f51fd8df42a4ad2b81fdc3144d5fa8c3f50bfa
65 changes: 34 additions & 31 deletions 65 Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ def _comparable_version(version):

### Platform specific APIs

_libc_search = re.compile(b'(__libc_init)'
b'|'
b'(GLIBC_([0-9.]+))'
b'|'
br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)

def libc_ver(executable=None, lib='', version='', chunksize=16384):

Expand Down Expand Up @@ -190,6 +185,12 @@ def libc_ver(executable=None, lib='', version='', chunksize=16384):
# sys.executable is not set.
return lib, version

_libc_search = re.compile(b'(__libc_init)'
b'|'
b'(GLIBC_([0-9.]+))'
b'|'
br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)

V = _comparable_version
# We use os.path.realpath()
# here to work around problems with Cygwin not being
Expand Down Expand Up @@ -247,9 +248,6 @@ def _norm_version(version, build=''):
version = '.'.join(strings[:3])
return version

_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
r'.*'
r'\[.* ([\d.]+)\])')

# Examples of VER command output:
#
Expand Down Expand Up @@ -295,6 +293,10 @@ def _syscmd_ver(system='', release='', version='',
else:
return system, release, version

_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
r'.*'
r'\[.* ([\d.]+)\])')

# Parse the output
info = info.strip()
m = _ver_output.match(info)
Expand Down Expand Up @@ -1033,18 +1035,6 @@ def processor():

### Various APIs for extracting information from sys.version

_sys_version_parser = re.compile(
r'([\w.+]+)\s*' # "version<space>"
r'\(#?([^,]+)' # "(#buildno"
r'(?:,\s*([\w ]*)' # ", builddate"
r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>"
r'\[([^\]]+)\]?', re.ASCII) # "[compiler]"

_pypy_sys_version_parser = re.compile(
r'([\w.+]+)\s*'
r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
r'\[PyPy [^\]]+\]?')

_sys_version_cache = {}

def _sys_version(sys_version=None):
Expand Down Expand Up @@ -1076,6 +1066,13 @@ def _sys_version(sys_version=None):
if result is not None:
return result

_sys_version_parser = re.compile(
r'([\w.+]+)\s*' # "version<space>"
r'\(#?([^,]+)' # "(#buildno"
r'(?:,\s*([\w ]*)' # ", builddate"
r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>"
r'\[([^\]]+)\]?', re.ASCII) # "[compiler]"

if sys.platform.startswith('java'):
# Jython
name = 'Jython'
Expand All @@ -1091,6 +1088,11 @@ def _sys_version(sys_version=None):

elif "PyPy" in sys_version:
# PyPy
_pypy_sys_version_parser = re.compile(
r'([\w.+]+)\s*'
r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
r'\[PyPy [^\]]+\]?')

name = "PyPy"
match = _pypy_sys_version_parser.match(sys_version)
if match is None:
Expand Down Expand Up @@ -1290,17 +1292,6 @@ def platform(aliased=False, terse=False):
### freedesktop.org os-release standard
# https://www.freedesktop.org/software/systemd/man/os-release.html

# NAME=value with optional quotes (' or "). The regular expression is less
# strict than shell lexer, but that's ok.
_os_release_line = re.compile(
"^(?P<name>[a-zA-Z0-9_]+)=(?P<quote>[\"\']?)(?P<value>.*)(?P=quote)$"
)
# unescape five special characters mentioned in the standard
_os_release_unescape = re.compile(r"\\([\\\$\"\'`])")
# /etc takes precedence over /usr/lib
_os_release_candidates = ("/etc/os-release", "/usr/lib/os-release")
_os_release_cache = None


def _parse_os_release(lines):
# These fields are mandatory fields with well-known defaults
Expand All @@ -1311,6 +1302,14 @@ def _parse_os_release(lines):
"PRETTY_NAME": "Linux",
}

# NAME=value with optional quotes (' or "). The regular expression is less
# strict than shell lexer, but that's ok.
_os_release_line = re.compile(
"^(?P<name>[a-zA-Z0-9_]+)=(?P<quote>[\"\']?)(?P<value>.*)(?P=quote)$"
)
# unescape five special characters mentioned in the standard
_os_release_unescape = re.compile(r"\\([\\\$\"\'`])")

for line in lines:
mo = _os_release_line.match(line)
if mo is not None:
Expand All @@ -1326,6 +1325,10 @@ def freedesktop_os_release():
"""
global _os_release_cache

# /etc takes precedence over /usr/lib
_os_release_candidates = ("/etc/os-release", "/usr/lib/os-release")
_os_release_cache = None

if _os_release_cache is None:
errno = None
for candidate in _os_release_candidates:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.