Commit 2b123309 authored by Marc-André Lemburg's avatar Marc-André Lemburg

Add parameter sys_version to _sys_version().

Change the cache for _sys_version() to take the parameter into account.

Add support for parsing the IronPython 1.0.1 sys.version value - even
though it still returns '1.0.0'; the version string no longer includes
the patch level.
parent 5ad4d2ca
...@@ -1201,9 +1201,10 @@ _ironpython_sys_version_parser = re.compile( ...@@ -1201,9 +1201,10 @@ _ironpython_sys_version_parser = re.compile(
'([\d\.]+)' '([\d\.]+)'
'(?: \(([\d\.]+)\))?' '(?: \(([\d\.]+)\))?'
' on (.NET [\d\.]+)') ' on (.NET [\d\.]+)')
_sys_version_cache = None
def _sys_version(): _sys_version_cache = {}
def _sys_version(sys_version=None):
""" Returns a parsed version of Python's sys.version as tuple """ Returns a parsed version of Python's sys.version as tuple
(name, version, branch, revision, buildno, builddate, compiler) (name, version, branch, revision, buildno, builddate, compiler)
...@@ -1218,21 +1219,30 @@ def _sys_version(): ...@@ -1218,21 +1219,30 @@ def _sys_version():
The function returns empty strings for tuple entries that The function returns empty strings for tuple entries that
cannot be determined. cannot be determined.
sys_version may be given to parse an alternative version
string, e.g. if the version was read from a different Python
interpreter.
""" """
global _sys_version_cache # Get the Python version
if sys_version is None:
sys_version = sys.version
if _sys_version_cache is not None: # Try the cache first
return _sys_version_cache result = _sys_version_cache.get(sys_version, None)
if result is not None:
return result
if sys.version[:10] == 'IronPython': # Parse it
if sys_version[:10] == 'IronPython':
# IronPython # IronPython
name = 'IronPython' name = 'IronPython'
match = _ironpython_sys_version_parser.match(sys.version) match = _ironpython_sys_version_parser.match(sys_version)
if match is None: if match is None:
raise ValueError( raise ValueError(
'failed to parse IronPython sys.version: %s' % 'failed to parse IronPython sys.version: %s' %
repr(sys.version)) repr(sys_version))
version, compiler = match.groups() version, alt_version, compiler = match.groups()
branch = '' branch = ''
revision = '' revision = ''
buildno = '' buildno = ''
...@@ -1241,11 +1251,11 @@ def _sys_version(): ...@@ -1241,11 +1251,11 @@ def _sys_version():
elif sys.platform[:4] == 'java': elif sys.platform[:4] == 'java':
# Jython # Jython
name = 'Jython' name = 'Jython'
match = _jython_sys_version_parser.match(sys.version) match = _jython_sys_version_parser.match(sys_version)
if match is None: if match is None:
raise ValueError( raise ValueError(
'failed to parse Jython sys.version: %s' % 'failed to parse Jython sys.version: %s' %
repr(sys.version)) repr(sys_version))
version, = match.groups() version, = match.groups()
branch = '' branch = ''
revision = '' revision = ''
...@@ -1255,27 +1265,47 @@ def _sys_version(): ...@@ -1255,27 +1265,47 @@ def _sys_version():
else: else:
# CPython # CPython
match = _sys_version_parser.match(sys.version) match = _sys_version_parser.match(sys_version)
if match is None: if match is None:
raise ValueError( raise ValueError(
'failed to parse CPython sys.version: %s' % 'failed to parse CPython sys.version: %s' %
repr(sys.version)) repr(sys_version))
version, buildno, builddate, buildtime, compiler = \ version, buildno, builddate, buildtime, compiler = \
match.groups() match.groups()
if hasattr(sys, 'subversion'): if hasattr(sys, 'subversion'):
# sys.subversion was added in Python 2.5
name, branch, revision = sys.subversion name, branch, revision = sys.subversion
else: else:
name = 'CPython' name = 'CPython'
branch = '' branch = ''
revision = '' revision = ''
builddate = builddate + ' ' + buildtime builddate = builddate + ' ' + buildtime
# Add the patchlevel version if missing
l = string.split(version, '.') l = string.split(version, '.')
if len(l) == 2: if len(l) == 2:
l.append('0') l.append('0')
version = string.join(l, '.') version = string.join(l, '.')
_sys_version_cache = (name, version, branch, revision, buildno,
builddate, compiler) # Build and cache the result
return _sys_version_cache result = (name, version, branch, revision, buildno, builddate, compiler)
_sys_version_cache[sys_version] = result
return result
def _test_sys_version():
_sys_version_cache.clear()
for input, output in (
('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
('IronPython 1.0.60816 on .NET 2.0.50727.42',
('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
):
parsed = _sys_version(input)
if parsed != output:
print (input, parsed)
def python_implementation(): def python_implementation():
...@@ -1325,6 +1355,7 @@ def python_branch(): ...@@ -1325,6 +1355,7 @@ def python_branch():
If not available, an empty string is returned. If not available, an empty string is returned.
""" """
return _sys_version()[2] return _sys_version()[2]
def python_revision(): def python_revision():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment