"Fossies" - the Fresh Open Source Software Archive

Member "Cheetah3-3.2.6.post1/Cheetah/Tests/ImportHooks.py" (22 Feb 2021, 3028 Bytes) of package /linux/www/Cheetah3-3.2.6.post1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "ImportHooks.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3-3.2.6_vs_3-3.2.6.post1.

    1 from glob import glob
    2 import os
    3 import shutil
    4 import sys
    5 import unittest
    6 import Cheetah.ImportHooks
    7 from Cheetah.compat import PY2
    8 
    9 
   10 ImportHooksTemplatesDir = os.path.join(
   11     os.path.dirname(__file__), 'ImportHooksTemplates')
   12 
   13 
   14 def setUpModule():
   15     sys.path.append(ImportHooksTemplatesDir)
   16 
   17 
   18 def tearDownModule():
   19     assert sys.path[-1] == ImportHooksTemplatesDir
   20     del sys.path[-1]
   21 
   22 
   23 def _cleanup():
   24     py_files = os.path.join(ImportHooksTemplatesDir, '*.py')
   25     pyc_files = py_files + 'c'
   26     for fname in glob(py_files) + glob(pyc_files):
   27         os.remove(fname)
   28     __pycache__ = os.path.join(ImportHooksTemplatesDir, '__pycache__')
   29     if os.path.isdir(__pycache__):
   30         shutil.rmtree(__pycache__)
   31 
   32     for modname in list(sys.modules.keys()):
   33         if '.ImportHooksTemplates.' in modname \
   34                 or modname.endswith('.ImportHooksTemplates'):
   35             del sys.modules[modname]
   36 
   37     for modname in 'index', 'layout':
   38         if modname in sys.modules:
   39             del sys.modules[modname]
   40 
   41     Cheetah.ImportHooks.uninstall()
   42 
   43 
   44 def _exec(code, _dict):
   45     exec(code, _dict)
   46 
   47 
   48 class ImportHooksTest(unittest.TestCase):
   49     def setUp(self):
   50         _cleanup()
   51 
   52     def tearDown(self):
   53         _cleanup()
   54 
   55     def test_CheetahDirOwner(self):
   56         templates = list(sorted(os.listdir(ImportHooksTemplatesDir)))
   57         self.assertListEqual(templates, ['index.tmpl', 'layout.tmpl'])
   58 
   59         cdo = Cheetah.ImportHooks.CheetahDirOwner(ImportHooksTemplatesDir)
   60         index_mod = cdo.getmod('index')
   61         templates = os.listdir(ImportHooksTemplatesDir)
   62         self.assertIn('index.py', templates)
   63         self.assertNotIn('layout.py', templates)
   64 
   65         index_co = index_mod.__co__
   66         del index_mod.__co__
   67         self.assertRaises(ImportError, _exec, index_co, index_mod.__dict__)
   68 
   69         cdo.getmod('layout')  # Compiled to layout.py and .pyc
   70         self.assertIn('layout.py', os.listdir(ImportHooksTemplatesDir))
   71 
   72     def test_ImportHooks(self):
   73         templates = os.listdir(ImportHooksTemplatesDir)
   74         self.assertNotIn('index.py', templates)
   75         self.assertNotIn('layout.py', templates)
   76         Cheetah.ImportHooks.install()
   77         from index import index  # noqa
   78         templates = os.listdir(ImportHooksTemplatesDir)
   79         self.assertIn('index.py', templates)
   80         self.assertIn('layout.py', templates)
   81 
   82     def test_import_builtin(self):
   83         Cheetah.ImportHooks.install()
   84         for nm in sys.builtin_module_names:
   85             if nm not in sys.modules:
   86                 __import__(nm)
   87                 return
   88         raise self.fail("All builtin modules are imported")
   89 
   90     # _bootlocale was removed in Python 3.10:
   91     # https://bugs.python.org/issue42208
   92     if not PY2 and sys.version_info < (3, 10):
   93         def test_import_bootlocale(self):
   94             if '_bootlocale' in sys.modules:
   95                 del sys.modules['_bootlocale']
   96             Cheetah.ImportHooks.install()
   97             import _bootlocale  # noqa: F401 '_bootlocale' imported but unused