[pypy-svn] r74821 - in pypy/branch/sys-prefix/pypy/translator/goal: . test2

antocuni at codespeak.net antocuni at codespeak.net
Thu May 27 18:36:05 CEST 2010


Author: antocuni
Date: Thu May 27 18:36:03 2010
New Revision: 74821

Modified:
   pypy/branch/sys-prefix/pypy/translator/goal/app_main.py
   pypy/branch/sys-prefix/pypy/translator/goal/test2/test_app_main.py
Log:
try to factor out get_library_path from setup_initial_paths, and write a test
for it as there were none


Modified: pypy/branch/sys-prefix/pypy/translator/goal/app_main.py
==============================================================================
--- pypy/branch/sys-prefix/pypy/translator/goal/app_main.py	(original)
+++ pypy/branch/sys-prefix/pypy/translator/goal/app_main.py	Thu May 27 18:36:03 2010
@@ -182,12 +182,32 @@
                                    option)
         return argv[i], i
 
+def get_library_path(executable):
+    AUTOSUBPATH = 'share' + os.sep + 'pypy-%d.%d'
+    # set up a sys.path that depends on the local machine
+    autosubpath = AUTOSUBPATH % sys.pypy_version_info[:2]
+    search = executable
+    while 1:
+        dirname = resolvedirof(search)
+        if dirname == search:
+            # not found!  let's hope that the compiled-in path is ok
+            print >> sys.stderr, ('debug: WARNING: library path not found, '
+                                  'using compiled-in sys.path')
+            newpath = sys.path[:]
+            break
+        newpath = sys.pypy_initial_path(dirname)
+        if newpath is None:
+            newpath = sys.pypy_initial_path(os.path.join(dirname, autosubpath))
+            if newpath is None:
+                search = dirname    # walk to the parent directory
+                continue
+        break      # found!
+    return newpath
 
 def setup_initial_paths(executable, nanos):
     # a substituted os if we are translated
     global os
     os = nanos
-    AUTOSUBPATH = 'share' + os.sep + 'pypy-%d.%d'
     # find the full path to the executable, assuming that if there is no '/'
     # in the provided one then we must look along the $PATH
     if we_are_translated() and IS_WINDOWS and not executable.lower().endswith('.exe'):
@@ -204,24 +224,7 @@
                     break
     sys.executable = os.path.abspath(executable)
 
-    # set up a sys.path that depends on the local machine
-    autosubpath = AUTOSUBPATH % sys.pypy_version_info[:2]
-    search = executable
-    while 1:
-        dirname = resolvedirof(search)
-        if dirname == search:
-            # not found!  let's hope that the compiled-in path is ok
-            print >> sys.stderr, ('debug: WARNING: library path not found, '
-                                  'using compiled-in sys.path')
-            newpath = sys.path[:]
-            break
-        newpath = sys.pypy_initial_path(dirname)
-        if newpath is None:
-            newpath = sys.pypy_initial_path(os.path.join(dirname, autosubpath))
-            if newpath is None:
-                search = dirname    # walk to the parent directory
-                continue
-        break      # found!
+    newpath = get_library_path(executable)
     path = os.getenv('PYTHONPATH')
     if path:
         newpath = path.split(os.pathsep) + newpath

Modified: pypy/branch/sys-prefix/pypy/translator/goal/test2/test_app_main.py
==============================================================================
--- pypy/branch/sys-prefix/pypy/translator/goal/test2/test_app_main.py	(original)
+++ pypy/branch/sys-prefix/pypy/translator/goal/test2/test_app_main.py	Thu May 27 18:36:03 2010
@@ -460,3 +460,43 @@
         data = child_out_err.read(11)
         assert data == '\x00(STDOUT)\n\x00'    # from stdout
         child_out_err.close()
+
+
+class AppTestAppMain:
+
+    def setup_class(self):
+        # ------------------------------------
+        # setup code for test_get_library_path
+        # ------------------------------------
+        from pypy.module.sys.version import CPYTHON_VERSION, PYPY_VERSION
+        libroot = 'share/pypy-%d.%d' % PYPY_VERSION[:2]
+        cpy_ver = '%d.%d.%d' % CPYTHON_VERSION[:3]
+        
+        goal_dir = os.path.dirname(app_main)
+        # build a directory hierarchy like which contains both bin/pypy-c and
+        # share/pypy-1.2/*
+        prefix = udir.join('pathtest')
+        fake_exe = prefix.join('bin/pypy-c').ensure(file=1)
+        share_pypy = prefix.join(libroot).ensure(dir=1)
+        expected_path = [str(share_pypy.join(subdir).ensure(dir=1))
+                         for subdir in ('pypy/lib',
+                                        'lib-python/modified-%s' % cpy_ver,
+                                        'lib-python/%s' % cpy_ver)]
+        
+        self.w_goal_dir = self.space.wrap(goal_dir)
+        self.w_fake_exe = self.space.wrap(str(fake_exe))
+        self.w_expected_path = self.space.wrap(expected_path)
+
+    def test_get_library_path(self):
+        import sys
+        import os
+        sys.path.append(self.goal_dir)
+        try:
+            import app_main
+            app_main.os = os
+            newpath = app_main.get_library_path('/tmp/pypy-c') # stdlib not found
+            assert newpath == sys.path
+            newpath = app_main.get_library_path(self.fake_exe)
+            assert newpath == self.expected_path
+        finally:
+            sys.path.pop()



More information about the Pypy-commit mailing list