-
Notifications
You must be signed in to change notification settings - Fork 27
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
Improve friendliness with Python 3 #175
Changes from all commits
c3d6639
3036884
f9aca6f
5d04d84
7f64734
22be9bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,7 @@ def _log(self, level, msg, args): | |
|
||
RELOAD_VSC_MODS = False | ||
|
||
VERSION = '0.17.1' | ||
VERSION = '0.17.2' | ||
|
||
log.info('This is (based on) vsc.install.shared_setup %s' % VERSION) | ||
log.info('(using setuptools version %s located at %s)' % (setuptools.__version__, setuptools.__file__)) | ||
|
@@ -263,12 +263,24 @@ def _fvs(msg=None): | |
|
||
|
||
def _read(source, read_lines=False): | ||
"""read a file, either in full or as a list (read_lines=True)""" | ||
with open(source) as file_handle: | ||
"""read a file, either in full or as a list (read_lines=True) and decode UTF-8 files into ASCII""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you don't know that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK there is no other way that a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but can you properly read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it might be safer to directly decode the file in ASCII and handle the errors. Using
but it generates new errors down the line. There is a write operation in UTF-8 which does not allow surrogates
I could hunt all those new errors, but I think that we can just use |
||
|
||
def file_read(file_handle, read_lines): | ||
"""raw read action on file handle, either in full or as a list (read_lines=True)""" | ||
if read_lines: | ||
txt = file_handle.readlines() | ||
else: | ||
txt = file_handle.read() | ||
return txt | ||
|
||
try: | ||
with open(source) as file_handle: | ||
txt = file_read(file_handle, read_lines) | ||
except UnicodeDecodeError: | ||
# file contains unicode characters, try again backslashing them | ||
with open(source, encoding='ascii', errors="backslashreplace") as file_handle: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not only using this open instead of the try/except? also, is this py2 compat? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python 2 |
||
txt = file_read(file_handle, read_lines) | ||
|
||
return txt | ||
|
||
|
||
|
@@ -662,7 +674,10 @@ def make_release_tree(self, base_dir, files): | |
if pyshebang_reg.search(first_line): | ||
log.info("going to adapt shebang for script %s" % fn) | ||
dest, code = self._recopy(base_dir, fn) | ||
code = pyshebang_reg.sub(SHEBANG_STRIPPED_ENV_PYTHON, code) | ||
pyshebang_line = first_line.split() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this doing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of replacing the full line with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't you fix the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would require changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or alternatively, create a new global |
||
pyshebang_line[0] = SHEBANG_STRIPPED_ENV_PYTHON | ||
pyshebang_line = ' '.join(pyshebang_line) | ||
code = pyshebang_reg.sub(pyshebang_line, code) | ||
self._write(dest, code) | ||
else: | ||
log.info("no scripts to check for shebang") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm trying to understand what this is fixing, but i can't get my had wrapped around it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, nvm. i'm not a big fan of this tbh. i don't like the fact that i can eat arguments, making it a bad wrapper.
better solutions however...
in what kind of an environment do you run into this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not really depend on any specific environment. The problem is that
vsc-install
is breaking any scripts that can only be executed in Python 3. Scripts with a shebang such as#!/usr/bin/env python3
are replaced with#!/usr/bin/python-stripped-env
which is hardcoded to/usr/bin/python
. Therefore, if the Python 3 interpreter is calledpython3
, as it usually is, things break.I propose to solve it by allowing to pass an argument to
python-stripped-env
defining the target interpreter. In a similar way to how/usr/bin/env
works. This PR does not change the default behaviour ofpython-stripped-env
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7f64734 tightens the names considered as possible python interpreters to reduce the chances of eating arguments by mistake. Now it only accepts the basic
python
,python2
,python3
and those followed by any dot version.