AttributeError in jinja2.ext.babel_extract with Python 3 #158

Closed
Zortaniac opened this Issue Oct 17, 2012 · 3 comments

Comments

Projects
None yet
3 participants

If you use Python 3, the babel_extract method fails, maybe because of the no longer existing decode method on string objects. (I use the python 3 compatible babel implementation from https://bitbucket.org/vinay.sajip/babel3 )

File "venv/lib/python3.2/site-packages/jinja2/ext.py", line 593, in babel_extract
source = fileobj.read().decode(options.get('encoding', 'utf-8'))
AttributeError: 'str' object has no attribute 'decode'

Doing a little research on how this can be fixed in py3. It looks like python 3 needs to specify the encoding when you open the file (http://getpython3.com/diveintopython3/files.html):

a_file = open('examples/chinese.txt', encoding='utf-8')

But the issue is that in the file specified in this bug, the fileobj is passed into the babel extract function. It would be awesome to get a trace back or even better, a code block that causes the error, or even more better, a unit test. I don't know enough about this toolchain to reproduce this bug with the provided info.

The error causes in the extract_messages command of setup.py in my pyramid project. I have work around the error by commenting out the decode function after read(), but I think its required for python 2 and only correct as long as the decoding is utf-8.
I hope this traceback helps:

extracting messages from backend/templates/dashboard.jinja2 (encoding="utf-8")
Traceback (most recent call last):
File "setup.py", line 45, in
('**.py', 'chameleon_python', None ),
File "/usr/lib/python3.2/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.2/distutils/dist.py", line 917, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.2/distutils/dist.py", line 936, in run_command
cmd_obj.run()
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/frontend.py", line 314, in run
for filename, lineno, message, comments, context in extracted:
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/extract.py", line 171, in extract_from_dir
strip_comment_tags):
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/extract.py", line 201, in extract_from_file
strip_comment_tags))
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/extract.py", line 281, in extract
for lineno, funcname, messages, comments in results:
File "/venv/lib/python3.2/site-packages/jinja2/ext.py", line 593, in babel_extract
source = fileobj.read().decode(options.get('encoding', 'utf-8'))
AttributeError: 'str' object has no attribute 'decode'

Owner

mitsuhiko commented May 20, 2013

The file object comes from babel. I would expect it to give me one in bytes. I would file that bug against babel and not Jinja.

@mitsuhiko mitsuhiko closed this May 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment