mailr22802 - /trunk/test_suite/verification_tests/library.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on April 17, 2014 - 13:52:
Author: bugman
Date: Thu Apr 17 13:52:50 2014
New Revision: 22802

URL: http://svn.gna.org/viewcvs/relax?rev=22802&view=rev
Log:
Python 2.5 and 2.6 compatibility for the Library.test_library_independence 
verification test.

The importlib package is not available in these Python versions, but the code 
in the Python 2.7
library file importlib/__init__.py is compatible all the way back to Python 
2.3.  Therefore the
importlib functions have been copied directly into the system test script and 
the importlib
dependency removed.


Modified:
    trunk/test_suite/verification_tests/library.py

Modified: trunk/test_suite/verification_tests/library.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/test_suite/verification_tests/library.py?rev=22802&r1=22801&r2=22802&view=diff
==============================================================================
--- trunk/test_suite/verification_tests/library.py      (original)
+++ trunk/test_suite/verification_tests/library.py      Thu Apr 17 13:52:50 
2014
@@ -66,9 +66,44 @@
         lines = [
             "",
             "# Python module imports.",
-            "import importlib",
             "import pkgutil",
             "import sys",
+            "",
+            "# Direct copy of the Python 2.7 importlib function.",
+            "def _resolve_name(name, package, level):",
+            "    \"\"\"Return the absolute name of the module to be 
imported.\"\"\"",
+            "    if not hasattr(package, 'rindex'):",
+            "        raise ValueError(\"'package' not set to a string\")",
+            "    dot = len(package)",
+            "    for x in xrange(level, 1, -1):",
+            "        try:",
+            "            dot = package.rindex('.', 0, dot)",
+            "        except ValueError:",
+            "            raise ValueError(\"attempted relative import beyond 
top-level package\")",
+            "    return \"%s.%s\" % (package[:dot], name)",
+            "",
+            "",
+            "# Direct copy of the Python 2.7 importlib function.",
+            "def import_module(name, package=None):",
+            "    \"\"\"Import a module.",
+            "",
+            "    The 'package' argument is required when performing a 
relative import. It",
+            "    specifies the package to use as the anchor point from which 
to resolve the",
+            "    relative import to an absolute import.",
+            "",
+            "    \"\"\"",
+            "    if name.startswith('.'):",
+            "        if not package:",
+            "            raise TypeError(\"relative imports require the 
'package' argument\")",
+            "        level = 0",
+            "        for character in name:",
+            "            if character != '.':",
+            "                break",
+            "            level += 1",
+            "        name = _resolve_name(name[level:], package, level)",
+            "    __import__(name)",
+            "    return sys.modules[name]",
+            "",
             "",
             "# Initialise a structure for later reporting of failed 
imports.",
             "failed = []",
@@ -88,7 +123,7 @@
             "    # Import it.",
             "    module = None",
             "    try:",
-            "        module = importlib.import_module(full_name)",
+            "        module = import_module(full_name)",
             "    except:",
             "        message = sys.exc_info()[1]",
             "        failed.append([full_name, message])",
@@ -111,7 +146,7 @@
             "        # Import it.",
             "        module2 = None",
             "        try:",
-            "            module2 = importlib.import_module(full_name2)",
+            "            module2 = import_module(full_name2)",
             "        except:",
             "            message = sys.exc_info()[1]",
             "            failed.append([full_name2, message])",
@@ -134,7 +169,7 @@
             "",
             "            # Import it.",
             "            try:",
-            "                module3 = importlib.import_module(full_name3)",
+            "                module3 = import_module(full_name3)",
             "            except:",
             "                message = sys.exc_info()[1]",
             "                failed.append([full_name3, message])",




Related Messages


Powered by MHonArc, Updated Thu Apr 17 14:20:03 2014