Author: Philip Jenvey <[email protected]>
Branch:
Changeset: r70500:c54befdbeb4b
Date: 2014-04-09 11:30 -0700
http://bitbucket.org/pypy/pypy/changeset/c54befdbeb4b/
Log: minor cleanup/pep8
diff --git a/pypy/module/sys/initpath.py b/pypy/module/sys/initpath.py
--- a/pypy/module/sys/initpath.py
+++ b/pypy/module/sys/initpath.py
@@ -2,27 +2,31 @@
Logic to find sys.executable and the initial sys.path containing the stdlib
"""
-import sys
+import errno
import os
import stat
-import errno
+import sys
+
from rpython.rlib import rpath
from rpython.rlib.objectmodel import we_are_translated
+
from pypy.interpreter.gateway import unwrap_spec
from pypy.module.sys.state import get as get_state
-platform = sys.platform
IS_WINDOWS = sys.platform == 'win32'
+
def find_executable(executable):
"""
- Return the absolute path of the executable, by looking into PATH and the
- current directory. If it cannot be found, return ''.
+ Return the absolute path of the executable, by looking into PATH and
+ the current directory. If it cannot be found, return ''.
"""
- if we_are_translated() and IS_WINDOWS and not
executable.lower().endswith('.exe'):
+ if (we_are_translated() and IS_WINDOWS and
+ not executable.lower().endswith('.exe')):
executable += '.exe'
if os.sep in executable or (IS_WINDOWS and ':' in executable):
- pass # the path is already more than just an executable name
+ # the path is already more than just an executable name
+ pass
else:
path = os.environ.get('PATH')
if path:
@@ -35,15 +39,15 @@
# 'sys.executable' should not end up being an non-existing file;
# just use '' in this case. (CPython issue #7774)
- if not os.path.isfile(executable):
- executable = ''
- return executable
+ return executable if os.path.isfile(executable) else ''
+
def _readlink_maybe(filename):
if not IS_WINDOWS:
return os.readlink(filename)
raise NotImplementedError
+
def resolvedirof(filename):
filename = rpath.rabspath(filename)
dirname = rpath.rabspath(os.path.join(filename, '..'))
@@ -56,36 +60,37 @@
return resolvedirof(os.path.join(dirname, link))
return dirname
+
def find_stdlib(state, executable):
"""
Find and compute the stdlib path, starting from the directory where
- ``executable`` is and going one level up until we find it. Return a tuple
- (path, prefix), where ``prefix`` is the root directory which contains the
- stdlib.
- If it cannot be found, return (None, None).
+ ``executable`` is and going one level up until we find it. Return a
+ tuple (path, prefix), where ``prefix`` is the root directory which
+ contains the stdlib. If it cannot be found, return (None, None).
"""
- if executable == '':
- executable = 'pypy-c'
- search = executable
+ search = 'pypy-c' if executable == '' else executable
while True:
dirname = resolvedirof(search)
if dirname == search:
- return None, None # not found :-(
+ return None, None # not found :-(
newpath = compute_stdlib_path_maybe(state, dirname)
if newpath is not None:
return newpath, dirname
search = dirname # walk to the parent directory
+
def _checkdir(path):
st = os.stat(path)
if not stat.S_ISDIR(st[0]):
raise OSError(errno.ENOTDIR, path)
+
def compute_stdlib_path(state, prefix):
"""
- Compute the paths for the stdlib rooted at ``prefix``. ``prefix`` must at
- least contain a directory called ``lib-python/X.Y`` and another one called
- ``lib_pypy``. If they cannot be found, it raises OSError.
+ Compute the paths for the stdlib rooted at ``prefix``. ``prefix``
+ must at least contain a directory called ``lib-python/X.Y`` and
+ another one called ``lib_pypy``. If they cannot be found, it raises
+ OSError.
"""
from pypy.module.sys.version import CPYTHON_VERSION
dirname = '%d.%d' % (CPYTHON_VERSION[0],
@@ -111,41 +116,42 @@
importlist.append(lib_tk)
# List here the extra platform-specific paths.
- if platform != 'win32':
- importlist.append(os.path.join(python_std_lib, 'plat-'+platform))
- if platform == 'darwin':
+ if not IS_WINDOWS:
+ importlist.append(os.path.join(python_std_lib, 'plat-' + sys.platform))
+ if sys.platform == 'darwin':
platmac = os.path.join(python_std_lib, 'plat-mac')
importlist.append(platmac)
importlist.append(os.path.join(platmac, 'lib-scriptpackages'))
return importlist
+
def compute_stdlib_path_maybe(state, prefix):
- """
- Return the stdlib path rooted at ``prefix``, or None if it cannot be
- found.
+ """Return the stdlib path rooted at ``prefix``, or None if it cannot
+ be found.
"""
try:
return compute_stdlib_path(state, prefix)
except OSError:
return None
+
@unwrap_spec(executable='str0')
def pypy_find_executable(space, executable):
return space.wrap(find_executable(executable))
+
@unwrap_spec(filename='str0')
def pypy_resolvedirof(space, filename):
return space.wrap(resolvedirof(filename))
+
@unwrap_spec(executable='str0')
def pypy_find_stdlib(space, executable):
path, prefix = find_stdlib(get_state(space), executable)
if path is None:
return space.w_None
- else:
- space.setitem(space.sys.w_dict, space.wrap('prefix'),
- space.wrap(prefix))
- space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
- space.wrap(prefix))
- return space.newlist([space.wrap(p) for p in path])
+ space.setitem(space.sys.w_dict, space.wrap('prefix'), space.wrap(prefix))
+ space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
+ space.wrap(prefix))
+ return space.newlist([space.wrap(p) for p in path])
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit