Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.9 #30

Open
yohanboniface opened this issue Feb 22, 2021 · 1 comment · May be fixed by #32
Open

Python 3.9 #30

yohanboniface opened this issue Feb 22, 2021 · 1 comment · May be fixed by #32
Assignees
Labels

Comments

@yohanboniface
Copy link

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
Copy link
Member

amcgregor commented Feb 22, 2021

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
# encoding: cinje

: def page content="", result=""
<!DOCTYPE html>
<title>Demonstration Interface</title>

<style>
html, body { height: 100%; padding: 0; margin: 0; font-size: 14px; font-family: sans-serif; line-height: 1; }
…
</style>

<textarea id="origin" placeholder="Enter your source markup here.">${content}</textarea>
<main id="target">${result}</main>
<button id="sanitize">Sanitize</button>
web/app/sanitize/template.py as seen by Python
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(('<!DOCTYPE html>\n',
		'<title>Demonstration Interface</title>\n',
		'\n',
		'<style>\n',
		'html, body { height: 100%; padding: 0; margin: 0; font-size: 14px; font-family: sans-serif; line-height: 1; }\n',
		'…',
		'</style>\n',
		'\n',
		'<textarea id="origin" placeholder="Enter your source markup here.">',
		_escape(content),
		'</textarea>\n',
		'<main id="target">',
		_escape(result),
		'</main>\n',
		'<button id="sanitize">Sanitize</button>\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
python3.9 -m venv sandbox
cd ./sandbox; . bin/activate
pip3 install cinje
minimal.py

"This page intentionally left blank."

# encoding: cinje
Invocation Results

python -m cinje source minimal.py

Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/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/[email protected]/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 <module>
    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
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.

@amcgregor amcgregor self-assigned this Feb 22, 2021
@amcgregor amcgregor changed the title Python 3.9 ? Python 3.9 Mar 2, 2021
amcgregor referenced this issue Mar 10, 2021
As a module-level list named `__mapping__`, and as a compressed delta
encoding for production use as ``__gzmapping__``.

The compressed version is the base-84 representation of a gzipped
stream of packed integer bytes defining the difference in line number
from line to line.
@amcgregor amcgregor linked a pull request May 28, 2024 that will close this issue
@amcgregor amcgregor linked a pull request May 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants