/usr/lib/python3/dist-packages/pyzolib/interpreters/pythoninterpreter.py is in python3-pyzolib 0.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import os
import sys
import subprocess
from pyzolib.interpreters.inwinreg import register_interpreter
class PythonInterpreter:
""" Clas to represent a Python interpreter. It has properties
to get the path and version. Upon creation the version number is
acquired by calling the interpreter in a subprocess. If this fails,
the version becomes ''.
"""
def __init__(self, path):
if not isinstance(path, str):
raise ValueError('Path for PythonInterpreter is not a string: %r' % path)
if not os.path.isfile(path):
raise ValueError('Path for PythonInterpreter is invalid: %r' % path)
self._path = os.path.normpath(os.path.abspath(path))
self._normpath = os.path.normcase(self._path)
self._is_pyzo = False
self._problem = ''
self._version = None
def __repr__(self):
cls_name = self.__class__.__name__
return '<%s version %s at %s>' % (cls_name, self.version, self.path)
def __hash__(self):
return hash(self._normpath)
def __eq__(self, other):
return self._normpath == other._normpath
@property
def path(self):
""" The full path to the executable of the Python interpreter.
"""
return self._path
@property
def is_pyzo(self):
""" Whether this is a Pyzo interpreter.
"""
return self._is_pyzo
@property
def version(self):
""" The version number as a string, usually 3 numbers.
"""
if self._version is None:
self._version = self._getversion()
return self._version
@property
def version_info(self):
""" The version number as a tuple of integers. For comparing.
"""
return _versionStringToTuple(self.version)
def register(self):
""" Register this Python intepreter. On Windows this modifies
the CURRENT_USER. On All other OS's this is a no-op.
"""
if sys.platform.startswith('win'):
path = os.path.split(self.path)[0] # Remove "python.exe"
register_interpreter(self.version[:3], path)
def _getversion(self):
# Check if path is even a file
if not os.path.isfile(self._path):
self._problem = '%s is not a valid file.'
return ''
# Poll Python executable (--version does not work on 2.4)
# shell=True prevents loads of command windows popping up on Windows,
# but if used on Linux it would enter interpreter mode
cmd = [self._path, '-V']
try:
v = subprocess.check_output(cmd, stderr=subprocess.STDOUT,
shell=sys.platform.startswith('win'))
except (OSError, IOError, subprocess.CalledProcessError) as e:
self._problem = str(e)
return ''
# Extract the version, apply some defensive programming
v = v.decode('ascii','ignore').strip().lower()
if v.startswith('python'):
v = v.split(' ')[1]
v = v.split(' ')[0]
# Try turning it into version_info
try:
_versionStringToTuple(v)
except ValueError:
return ''
# Done
return v
class PyzoInterpreter(PythonInterpreter):
""" A Pyzo interpreter.
"""
def __init__(self, path):
PythonInterpreter.__init__(self, path)
self._is_pyzo = True
def _versionStringToTuple(version):
# Truncate version number to first occurance of non-numeric character
tversion = ''
for c in version:
if c in '0123456789.': tversion += c
# Split by dots, make each number an integer
tversion = tversion.strip('.')
return tuple( [int(a) for a in tversion.split('.') if a] )
|