HamlPy icon indicating copy to clipboard operation
HamlPy copied to clipboard

HamlPy cannot run from library.zip

Open a1s opened this issue 11 years ago • 3 comments

HamlPy fails to load when an application is run from a packed Python library, such as one produced by py2exe:

Traceback (most recent call last):
  File "django\core\handlers\base.pyo", line 140, in get_response
  File "django\template\response.pyo", line 105, in render
  File "django\template\response.pyo", line 80, in rendered_content
  File "django\template\response.pyo", line 58, in resolve_template
  File "django\template\loader.pyo", line 146, in get_template
  File "django\template\loader.pyo", line 129, in find_template
  File "django\template\loader.pyo", line 96, in find_template_loader
  File "django\utils\importlib.pyo", line 35, in import_module
  File "hamlpy\template\__init__.pyo", line 1, in <module>
  File "hamlpy\template\loaders.pyo", line 64, in <module>
  File "hamlpy\template\utils.pyo", line 17, in get_django_template_loaders
  File "hamlpy\template\utils.pyo", line 22, in get_submodules
  File "hamlpy\template\utils.pyo", line 29, in package_contents
WindowsError: [Error 3] The system cannot find the path specified: 'X:\\path\\build\\dist.win32\\bin\\library.zip\\django\\template\\loaders/*.*'

a1s avatar Oct 02 '13 08:10 a1s

This patch effectively disables wrapping for all Django template loaders at HamlPy module load time when Django modules do not reside on file system:

=== modified file 'hamlpy/template/utils.py'
--- hamlpy/template/utils.py    2013-05-09 01:59:39 +0000
+++ hamlpy/template/utils.py    2013-10-02 08:18:29 +0000
@@ -1,6 +1,6 @@
 import imp
 from os import listdir
-from os.path import dirname, splitext
+from os.path import dirname, isdir, splitext

 try:
   from django.template import loaders
@@ -25,7 +25,10 @@

 def package_contents(package):
     package_path = dirname(loaders.__file__)
-    contents = set([splitext(module)[0]
-            for module in listdir(package_path)
-            if module.endswith(MODULE_EXTENSIONS)])
+    if isdir(package_path):
+        contents = set([splitext(module)[0]
+                for module in listdir(package_path)
+                if module.endswith(MODULE_EXTENSIONS)])
+    else:
+        contents = set()
     return contents

With this change, one cannot import an arbitrary Django loader from hamlpy.template.

Loaders hamlpy.template.loaders.HamlPyFilesystemLoader and hamlpy.template.loaders.HamlPyAppDirectoriesLoader are not affected by this patch and work as expected as soon as module loading error is fixed.

a1s avatar Oct 02 '13 08:10 a1s

By the way, I think more Pythonic approach would be to provide a way for explicit wrapping of needed loaders (in a way similar to what Django cached loader does) and not automatically create wrappers for everything we can find on a file system.

a1s avatar Oct 02 '13 09:10 a1s

This is fixed in 84abeed87310daa9cbb4b704dccc149a2dd53be5 (courtesy of @KACAH).

a1s avatar Mar 10 '15 11:03 a1s