marrow / cinje

A Pythonic and ultra fast template engine DSL.
MIT License
33 stars 0 forks source link

Python 3.9 #30

Open yohanboniface opened 3 years ago

yohanboniface commented 3 years ago

Hey there,

Seems not to run on python 3.9:

$ python benchmark.py
Traceback (most recent call last):
  File "/home/ybon/Code/py/cinje/example/benchmark.py", line 445, in <module>
    import bigtable
  File "/home/ybon/Code/py/cinje/example/bigtable.py", line 106
    __gzmapping__ = b"eJxjYGJkAANGMEBmMDAxMTAjCWETAwsRlmBgAAAPKwBL"
                                                                  ^
SyntaxError: unexpected EOF while parsing

or am I missing something ?

Thanks :)

amcgregor commented 3 years ago

Unfortunately, Python doesn't always emit the most helpful error messages when it comes to syntax errors such as unterminated constants, as that's just pointing at the end of the generated file.

To get a clearer picture of how Python sees your template, run: python3 -m cinje source source.py

E.g. from one of my own API service projects: python3 -m cinje source web/app/sanitize/template.py

If you have Pygments installed, the output will be pretty and syntax-colored when emitting to a terminal.

web/app/sanitize/template.py raw source ```py # encoding: cinje : def page content="", result="" Demonstration Interface
${result}
```
web/app/sanitize/template.py as seen by Python ```py import cinje from cinje.helpers import escape as _escape, bless as _bless, iterate, xmlargs as _args, _interrupt, _json __tmpl__ = [] # Exported template functions. def page(content="", result="", *, _escape=_escape, _bless=_bless, _args=_args): _buffer = [] __w, __ws = _buffer.extend, _buffer.append __w(('\n', 'Demonstration Interface\n', '\n', '\n', '\n', '\n', '
', _escape(result), '
\n', '\n')) yield "".join(_buffer) __tmpl__.extend(["page"]) __mapping__ = [0,2,2,2,2,2,2,2,3,3,3,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,23,23,24,24,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,42,42,42] __gzmapping__ = b"eJxjYGIAA0YQxgLAMtiEGQAEsQAr" ```

If you could Gist your source template, that'd greatly assist with debugging. 🙂 The likely culprit is an unterminated string much earlier in your code.

Updated to include raw source of sample template. Additionally, I have been able to reproduce locally and am investigating myself.

Minimal Test Case

Environment Preparation ```sh python3.9 -m venv sandbox cd ./sandbox; . bin/activate pip3 install cinje ```
minimal.py "This page intentionally left blank." ```py # encoding: cinje ```
Invocation Results python -m cinje source minimal.py ```pytb Traceback (most recent call last): File "/usr/local/Cellar/python@3.9/3.9.2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/local/Cellar/python@3.9/3.9.2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/Users/amcgregor/Projects/sandbox/lib/python3.9/site-packages/cinje/__main__.py", line 92, in interface.go(*interface.args, **interface.kwargs) File "/Users/amcgregor/Projects/sandbox/lib/python3.9/site-packages/cinje/__main__.py", line 26, in go result = action(*args, **data) File "/Users/amcgregor/Projects/sandbox/lib/python3.9/site-packages/cinje/__main__.py", line 60, in source result = self._source_reference(file) File "/Users/amcgregor/Projects/sandbox/lib/python3.9/site-packages/cinje/__main__.py", line 76, in _source_reference root = __import__(reference) File "/Users/amcgregor/Projects/sandbox/minimal.py", line 11 __gzmapping__ = b"eJxjYGJkAAMAACAABA==" ^ SyntaxError: unexpected EOF while parsing ```

However, if the "empty" source file is read in, and run through the Unicode decoder, correctly translated source is produced:

minimal-translated.py ```py import cinje from cinje.helpers import escape as _escape, bless as _bless, iterate, xmlargs as _args, _interrupt, _json __tmpl__ = [] # Exported template functions. __mapping__ = [0,2,3,3,3,3,3,3,3] __gzmapping__ = b"eJxjYGJkAAMAACAABA==" ```

Written to a distinct file, this module imports perfectly fine.