[pypy-commit] pypy app_main-refactor: move the logic to compute the stdlib path to its own file

antocuni noreply at buildbot.pypy.org
Fri Jun 8 17:33:13 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: app_main-refactor
Changeset: r55508:71e19f8cab3d
Date: 2012-06-08 15:21 +0200
http://bitbucket.org/pypy/pypy/changeset/71e19f8cab3d/

Log:	move the logic to compute the stdlib path to its own file

diff --git a/pypy/module/sys/__init__.py b/pypy/module/sys/__init__.py
--- a/pypy/module/sys/__init__.py
+++ b/pypy/module/sys/__init__.py
@@ -44,8 +44,8 @@
         'warnoptions'           : 'state.get(space).w_warnoptions', 
         'builtin_module_names'  : 'space.w_None',
         'pypy_getudir'          : 'state.pypy_getudir',    # not translated
-        'pypy_initial_path'     : 'state.pypy_initial_path',
-        'pypy_find_executable'  : 'state.pypy_find_executable',
+        'pypy_initial_path'     : 'initpath.pypy_initial_path',
+        'pypy_find_executable'  : 'initpath.pypy_find_executable',
 
         '_getframe'             : 'vm._getframe', 
         '_current_frames'       : 'currentframes._current_frames', 
diff --git a/pypy/module/sys/initpath.py b/pypy/module/sys/initpath.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/sys/initpath.py
@@ -0,0 +1,99 @@
+"""
+Logic to find sys.executable and the initial sys.path containing the stdlib
+"""
+
+import sys
+import os
+import stat
+import errno
+from pypy.rlib import rpath
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.interpreter.gateway import unwrap_spec
+
+platform = sys.platform
+IS_WINDOWS = sys.platform == 'win32'
+
+def find_executable(executable):
+    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
+    else:
+        path = os.environ.get('PATH')
+        if path:
+            for dir in path.split(os.pathsep):
+                fn = os.path.join(dir, executable)
+                if os.path.isfile(fn):
+                    executable = fn
+                    break
+    executable = rpath.rabspath(executable)
+    #
+    # '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
+
+
+
+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):
+    from pypy.module.sys.version import CPYTHON_VERSION
+    dirname = '%d.%d' % (CPYTHON_VERSION[0],
+                         CPYTHON_VERSION[1])
+    lib_python = os.path.join(prefix, 'lib-python')
+    python_std_lib = os.path.join(lib_python, dirname)
+    checkdir(python_std_lib)
+    
+    lib_pypy = os.path.join(prefix, 'lib_pypy')
+    checkdir(lib_pypy)
+
+    importlist = []
+    #
+    if state is not None:    # 'None' for testing only
+        lib_extensions = os.path.join(lib_pypy, '__extensions__')
+        state.w_lib_extensions = state.space.wrap(lib_extensions)
+        importlist.append(lib_extensions)
+    #
+    importlist.append(lib_pypy)
+    importlist.append(python_std_lib)
+    #
+    lib_tk = os.path.join(python_std_lib, 'lib-tk')
+    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':
+        platmac = os.path.join(python_std_lib, 'plat-mac')
+        importlist.append(platmac)
+        importlist.append(os.path.join(platmac, 'lib-scriptpackages'))
+    #
+    return importlist
+
+
+ at unwrap_spec(executable='str0')
+def pypy_find_executable(space, executable):
+    return space.wrap(find_executable(executable))
+
+
+ at unwrap_spec(srcdir='str0')
+def pypy_initial_path(space, srcdir):
+    try:
+        path = compute_stdlib_path(get(space), srcdir)
+    except OSError:
+        return space.w_None
+    else:
+        space.setitem(space.sys.w_dict, space.wrap('prefix'),
+                                        space.wrap(srcdir))
+        space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
+                                        space.wrap(srcdir))
+        return space.newlist([space.wrap(p) for p in path])
+
+
diff --git a/pypy/module/sys/state.py b/pypy/module/sys/state.py
--- a/pypy/module/sys/state.py
+++ b/pypy/module/sys/state.py
@@ -1,12 +1,9 @@
 """
 Implementation of interpreter-level 'sys' routines.
 """
+import os
 import pypy
-from pypy.rlib.objectmodel import we_are_translated
-from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import unwrap_spec
-
-import sys, os, stat, errno
+from pypy.module.sys.initpath import compute_stdlib_path
 
 # ____________________________________________________________
 #
@@ -25,63 +22,9 @@
         # Initialize the default path
         pypydir = os.path.dirname(os.path.abspath(pypy.__file__))
         srcdir = os.path.dirname(pypydir)
-        path = getinitialpath(self, srcdir)
+        path = compute_stdlib_path(self, srcdir)
         self.w_path = space.newlist([space.wrap(p) for p in path])
 
-def checkdir(path):
-    st = os.stat(path)
-    if not stat.S_ISDIR(st[0]):
-        raise OSError(errno.ENOTDIR, path)
-
-
-platform = sys.platform
-
-def getinitialpath(state, prefix):
-    from pypy.module.sys.version import CPYTHON_VERSION
-    dirname = '%d.%d' % (CPYTHON_VERSION[0],
-                         CPYTHON_VERSION[1])
-    lib_python = os.path.join(prefix, 'lib-python')
-    python_std_lib = os.path.join(lib_python, dirname)
-    checkdir(python_std_lib)
-    
-    lib_pypy = os.path.join(prefix, 'lib_pypy')
-    checkdir(lib_pypy)
-
-    importlist = []
-    #
-    if state is not None:    # 'None' for testing only
-        lib_extensions = os.path.join(lib_pypy, '__extensions__')
-        state.w_lib_extensions = state.space.wrap(lib_extensions)
-        importlist.append(lib_extensions)
-    #
-    importlist.append(lib_pypy)
-    importlist.append(python_std_lib)
-    #
-    lib_tk = os.path.join(python_std_lib, 'lib-tk')
-    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':
-        platmac = os.path.join(python_std_lib, 'plat-mac')
-        importlist.append(platmac)
-        importlist.append(os.path.join(platmac, 'lib-scriptpackages'))
-    #
-    return importlist
-
- at unwrap_spec(srcdir='str0')
-def pypy_initial_path(space, srcdir):
-    try:
-        path = getinitialpath(get(space), srcdir)
-    except OSError:
-        return space.w_None
-    else:
-        space.setitem(space.sys.w_dict, space.wrap('prefix'),
-                                        space.wrap(srcdir))
-        space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
-                                        space.wrap(srcdir))
-        return space.newlist([space.wrap(p) for p in path])
 
 def get(space):
     return space.fromcache(State)
@@ -117,31 +60,3 @@
     from pypy.tool.udir import udir
     return space.wrap(str(udir))
 
-
-IS_WINDOWS = 'nt' in sys.builtin_module_names
-
-def find_executable(executable):
-    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
-    else:
-        path = os.environ.get('PATH')
-        if path:
-            for dir in path.split(os.pathsep):
-                fn = os.path.join(dir, executable)
-                if os.path.isfile(fn):
-                    executable = fn
-                    break
-    executable = os.path.abspath(executable)
-    #
-    # '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
-
- at unwrap_spec(executable='str0')
-def pypy_find_executable(space, executable):
-    executable = find_executable(executable)
-    return space.wrap()
diff --git a/pypy/module/sys/test/test_initialpath.py b/pypy/module/sys/test/test_initpath.py
rename from pypy/module/sys/test/test_initialpath.py
rename to pypy/module/sys/test/test_initpath.py
--- a/pypy/module/sys/test/test_initialpath.py
+++ b/pypy/module/sys/test/test_initpath.py
@@ -1,6 +1,6 @@
 import py
 import os.path
-from pypy.module.sys.state import getinitialpath, find_executable
+from pypy.module.sys.initpath import compute_stdlib_path, find_executable
 from pypy.module.sys.version import PYPY_VERSION, CPYTHON_VERSION
 
 def build_hierarchy(prefix):
@@ -12,19 +12,19 @@
 
 def test_stdlib_in_prefix(tmpdir):
     dirs = build_hierarchy(tmpdir)
-    path = getinitialpath(None, str(tmpdir))
+    path = compute_stdlib_path(None, str(tmpdir))
     # we get at least 'dirs', and maybe more (e.g. plat-linux2)
     assert path[:len(dirs)] == map(str, dirs)
 
 def test_include_libtk(tmpdir):
     lib_pypy, lib_python = build_hierarchy(tmpdir)
     lib_tk = lib_python.join('lib-tk')
-    path = getinitialpath(None, str(tmpdir))
+    path = compute_stdlib_path(None, str(tmpdir))
     assert lib_tk in path
 
 
 def test_find_executable(tmpdir, monkeypatch):
-    from pypy.module.sys import state
+    from pypy.module.sys import initpath
     # /tmp/a/pypy
     # /tmp/b/pypy
     # /tmp/c
@@ -60,8 +60,8 @@
     c.join('pypy').ensure(dir=True)
     assert find_executable('pypy') == a.join('pypy')
     #
-    monkeypatch.setattr(state, 'we_are_translated', lambda: True)
-    monkeypatch.setattr(state, 'IS_WINDOWS', True)
+    monkeypatch.setattr(initpath, 'we_are_translated', lambda: True)
+    monkeypatch.setattr(initpath, 'IS_WINDOWS', True)
     monkeypatch.setenv('PATH', str(a))
     a.join('pypy.exe').ensure(file=True)
     assert find_executable('pypy') == a.join('pypy.exe')
diff --git a/pypy/rlib/rpath.py b/pypy/rlib/rpath.py
--- a/pypy/rlib/rpath.py
+++ b/pypy/rlib/rpath.py
@@ -3,7 +3,6 @@
 """
 
 import os.path
-from os import sep, pathsep
 from pypy.rlib import rposix
 
 if os.name == 'posix':


More information about the pypy-commit mailing list