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

Fix bug in get encoding from "coding" comment line #3023

Merged
merged 3 commits into from
Mar 5, 2016

Conversation

eendebakpt
Copy link
Contributor

The get_coding function can return an encoding that is not valid. This caused spyder to crash during startup. This pull request makes sure only valid encodings are returned.

An example text which returns an invalid encoding:

# Try decoding as utf-8 bytestring return __builtins__.unicode(msg, encoding="utf-8") except: pass try: # Try guessing system default encoding and decode as such import locale return __builtins__.unicode(msg, encoding=locale.getpreferredencoding()) except: pass try: # Attempt using filesystem encoding import sys return __builtins__.unicode(msg, encoding=sys.getfilesystemencoding()) except:

@Nodd
Copy link
Contributor

Nodd commented Mar 2, 2016

The code looks fine but please replace c with a meaningful name like codec.

Also I don't understand your example, are there missing newlines ? Or to test I can just copy-paste in an empty file in spyder ?

@eendebakpt
Copy link
Contributor Author

The variable name was changed. A working example:

import spyderlib
txt='return __builtins__.unicode(msg, encoding=sys.getfilesystemencoding())    except: '
coding = spyderlib.utils.encoding.get_coding(txt)
print(coding)

This returns a string which is not a valid coding.

if codec in CODECS:
return codec
else:
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually if the codec is not valid, the loop should continue to the second line. It's a detail but just removing the else: block achieves that and simplifies the code.

@eendebakpt
Copy link
Contributor Author

Ok, changed code as suggested

@ccordoba12 ccordoba12 changed the title fix bug in get_coding Fix bug in get_coding Mar 5, 2016
@ccordoba12
Copy link
Member

@eendebakpt, thanks for your contribution!

@ccordoba12 ccordoba12 changed the title Fix bug in get_coding Fix bug in get encoding from "coding" comment line Mar 5, 2016
ccordoba12 added a commit that referenced this pull request Mar 5, 2016
Fix bug in get encoding from "coding" comment line
@ccordoba12 ccordoba12 merged commit 4c1f648 into spyder-ide:master Mar 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants