Skip to content

Add the encoding='utf-8' to ensure that it won't cause Chinese disorderly code during the template files are read or wrote #400

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

Closed
wants to merge 4 commits into from

Conversation

liuming-dev
Copy link
Contributor

Today I found a problem when use web.py to develop a web app. The Exception is like this:

  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\application.py", line 257, in process
    return self.handle()
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\application.py", line 248, in handle
    return self._delegate(fn, self.fvars, args)
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\application.py", line 488, in _delegate
    return handle_class(cls)
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\application.py", line 466, in handle_class
    return tocall(*args)
  File "F:\liuming\PyWorkspace\WeChat\QuickStart.py", line 56, in GET
    return render.form(name)
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 1034, in __getattr__
    t = self._template(name)
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 1031, in _template
    return self._load_template(name)
  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 1016, in _load_template
    return Template(open(path).read(), filename=path, **self._keywords)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbd in position 139: illegal multibyte sequence

And this exception happens because the following code:

  File "E:\软件安装\Python\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 1016, in _load_template
    return Template(open(path).read(), filename=path, **self._keywords)

So, I change return Template(open(path).read(), filename=path, **self._keywords) to return Template(open(path,encoding='utf-8').read(), filename=path, **self._keywords), then the problem was sovled.
And the Chinese disorderly code problem was also solved:
Before>>
Chinese disorderly code
After>>

…erly code during the template files are read or wrote
@anandology
Copy link
Member

anandology commented Feb 15, 2017 via email

…e disorderly code during the template files are read or wrote"

This reverts commit 8135512.

revert
…erly code during the template files are read or wrote
@liuming-dev
Copy link
Contributor Author

@anandology Sure, now I have removed VCS.xml file.

@jzellman
Copy link
Contributor

jzellman commented Mar 3, 2017

@anandology thoughts on .gitignore change? It is separate commit... otherwise LGTM

@anandology
Copy link
Member

Doesn't work with python 2.

$ python2.7
>>> open('/tmp/a.txt', encoding='utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'encoding' is an invalid keyword argument for this function

To make it work with Python 2, we need to import the open function from io module.

>>> from io import open
>>> open('/tmp/a.txt', encoding='utf-8')
<_io.TextIOWrapper name='/tmp/a.txt' encoding='utf-8'>

@I2IT could you please make that change and remove the change the .gitignore?

liuming-dev added a commit to liuming-dev/webpy that referenced this pull request Sep 15, 2017
@anandology
Copy link
Member

Merged in ffb0b6a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants