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

Executing scripts with non ascii characters results in UnicodeDecodeError #625

Closed
fzlee opened this issue Jul 27, 2016 · 4 comments
Closed

Comments

@fzlee
Copy link

@fzlee fzlee commented Jul 27, 2016

Hi team: bpython could be the best interpreter I have ever tried, which is really cool.

But I got some problems with it recently. I can reproduce this issue with the following conditions:

OS: OSX EI Capitan/Ubuntu 3.13.0-81-generic 64bit
bpython version 0.15 on top of Python 2.7.11 or 2.7.11

if you have a script like:

#!/usr/bin/env python2
# coding: utf-8
"""
    你好 # I put some non-ascii characters here  
    test.py
    ~~~~~~~~~~

"""

if __name__ == "__main__":
    pass

run bpython -i test.py and you will be able to see the following exceptions:

Traceback (most recent call last):
  File "/Users/xxx/.virtualenvs/virtualenv1/bin/bpython", line 11, in <module>
    sys.exit(main())
  File "/Users/xxx/.virtualenvs/virtualenv1/lib/python2.7/site-packages/bpython/curtsies.py", line 170, in main
    bpargs.exec_code(interp, exec_args)
  File "/Users/xxx/.virtualenvs/virtualenv1/lib/python2.7/site-packages/bpython/args.py", line 122, in exec_code
    interpreter.runsource(source, args[0], 'exec')
  File "/Users/xxx/.virtualenvs/virtualenv1/lib/python2.7/site-packages/bpython/repl.py", line 111, in runsource
    source = u'# coding: %s\n\n%s' % (self.encoding, source)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 330: ordinal not in range(128)

Removing non ascii characters from script and we can get the issue resolved, but this doesn't seem to be a good idea, since we sometimes really need those non ascii characters. Besides running this script with python works.

Seems bpython will append coding declaration automatically prior to code execution, and that could be the root cause:

# bpython/repl.py, line 108
source = u'# coding: %s\n\n%s' % (self.encoding, source)

Just wondering do we have any solutions regarding to this issue? Thank you in advance!

@thomasballinger
Copy link
Member

@thomasballinger thomasballinger commented Jul 29, 2016

I've tried to write a patch for this, I'll write more tests for it soon.

@fzlee
Copy link
Author

@fzlee fzlee commented Jul 29, 2016

Thank you, thomasballinger. this bug fix should help me a lot!

@thomasballinger
Copy link
Member

@thomasballinger thomasballinger commented Aug 5, 2016

pending merging "Fix 625" #627

@thomasballinger
Copy link
Member

@thomasballinger thomasballinger commented Jul 5, 2020

#627 is merged, assuming this is fixed

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.