-
Notifications
You must be signed in to change notification settings - Fork 5
Sweep the tree for everything conditional on python 2. #425
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
base: next
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
|
|
@@ -736,23 +736,13 @@ | |
| https://docs.python.org/3/library/sys.html#sys.argv | ||
| for further details. | ||
| """ | ||
| if sys.version_info[0] >= 3: | ||
| return [os.fsencode(arg) for arg in argv] | ||
| else: | ||
| return argv | ||
| return [os.fsencode(arg) for arg in argv] | ||
|
|
||
|
|
||
| def NativeStringIO(initial_value=''): | ||
|
Contributor
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. See note below about |
||
| """Mock StringIO pseudo-class to create a StringIO matching the native | ||
| string coding form. That is a BytesIO with utf8 on python 2 and unicode | ||
| StringIO otherwise. Optional string helpers are automatically converted | ||
| accordingly. | ||
| """Pseudo-class wrapper to return a StringIO. This is a unicode StringIO. | ||
| """ | ||
| if sys.version_info[0] >= 3: | ||
| return io.StringIO(initial_value) | ||
| else: | ||
| from StringIO import StringIO | ||
| return StringIO(initial_value) | ||
| return io.StringIO(initial_value) | ||
|
|
||
|
|
||
| def DefaultStringIO(initial_value=''): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # --- BEGIN_HEADER --- | ||
|
|
@@ -36,29 +36,10 @@ | |
| import codecs | ||
| import io | ||
| import sys | ||
| # NOTE: StringIO is only available in python2 | ||
| try: | ||
| import StringIO | ||
| except ImportError: | ||
| StringIO = None | ||
|
|
||
| PY2 = sys.version_info[0] < 3 | ||
| _TYPE_UNICODE = type(u"") | ||
|
|
||
|
|
||
| if PY2: | ||
| class SimpleNamespace(dict): | ||
| """Bare minimum SimpleNamespace for Python 2.""" | ||
|
|
||
| def __getattribute__(self, name): | ||
| if name == '__dict__': | ||
| return dict(**self) | ||
|
|
||
| return self[name] | ||
| else: | ||
| from types import SimpleNamespace | ||
|
|
||
|
|
||
| def _is_unicode(val): | ||
| """Return boolean indicating if the value is a unicode string. | ||
|
|
||
|
|
@@ -72,34 +53,9 @@ | |
| """Given a supplied input which can be either a string or bytes | ||
| return a representation providing string operations while ensuring that | ||
| its contents represent a valid series of textual characters. | ||
|
|
||
| Arrange identical operation across python 2 and 3 - specifically, | ||
| the presence of invalid UTF-8 bytes (thus the input not being a | ||
| valid textual string) will trigger a UnicodeDecodeError on PY3. | ||
| Force the same to occur on PY2. | ||
| """ | ||
| if PY2: | ||
| # Simulate decoding done by PY3 to trigger identical exceptions | ||
| # note the use of a forced "utf8" encoding value: this function | ||
| # is generally used to wrap, for example, substitutions of values | ||
| # into strings that are defined in the source code. In Python 3 | ||
| # these are mandated to be UTF-8, and thus decoding as "utf8" is | ||
| # what will be attempted on supplied input. Match it. | ||
| textual_output = codecs.encode(string_or_bytes, 'utf8') | ||
| elif not _is_unicode(string_or_bytes): | ||
| if not _is_unicode(string_or_bytes): | ||
| textual_output = str(string_or_bytes, 'utf8') | ||
| else: | ||
| textual_output = string_or_bytes | ||
| return textual_output | ||
|
|
||
|
|
||
| def NativeStringIO(initial_value=''): | ||
|
Contributor
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. We have only two modules using
|
||
| """Mock StringIO pseudo-class to create a StringIO matching the native | ||
| string coding form. That is a BytesIO with utf8 on python 2 and unicode | ||
| StringIO otherwise. Optional string helpers are automatically converted | ||
| accordingly. | ||
| """ | ||
| if PY2 and StringIO is not None: | ||
| return StringIO.StringIO(initial_value) | ||
| else: | ||
| return io.StringIO(initial_value) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
|
|
@@ -37,17 +37,8 @@ | |
| MIG_BASE = os.path.realpath(os.path.join(os.path.dirname(__file__), '../..')) | ||
| MIG_ENV = os.getenv('MIG_ENV', 'default') | ||
|
|
||
| # NOTE: python3 switched strings to use unicode by default in contrast to bytes | ||
|
Contributor
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. I think we'll want to preserve a short note about the point of these variables and their use in |
||
| # in python2. File systems remain with utf8 however so we need to | ||
| # carefully handle a lot of cases of either encoding to utf8 or decoding | ||
| # to unicode depending on the python used. | ||
| # Please refer to the helpers in shared.base for actual handling of it. | ||
| if sys.version_info[0] >= 3: | ||
| default_str_coding = 'unicode' | ||
| default_fs_coding = 'utf8' | ||
| else: | ||
| default_str_coding = 'utf8' | ||
| default_fs_coding = 'utf8' | ||
| default_str_coding = 'unicode' | ||
| default_fs_coding = 'utf8' | ||
|
|
||
| CODING_KINDS = (STR_KIND, FS_KIND) = ('__STR__', '__FS__') | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
|
|
@@ -44,9 +44,8 @@ | |
| # Paramiko not available - imported fom griddaemons so fail gracefully | ||
| paramiko = None | ||
|
|
||
| from mig.shared.base import client_id_dir, force_utf8 | ||
| from mig.shared.base import client_id_dir, force_utf8, NativeStringIO | ||
| from mig.shared.conf import get_resource_exe, get_configuration_object | ||
| from mig.shared.compat import NativeStringIO | ||
|
Contributor
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. See note about NativeStringIO above |
||
| from mig.shared.defaults import ssh_conf_dir | ||
| from mig.shared.safeeval import subprocess_popen, subprocess_pipe | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
|
|
@@ -42,20 +42,9 @@ | |
| import base64 | ||
| import os | ||
| import sys | ||
|
|
||
| # NOTE: moved to urllib.parse in python3 and are re-exposed with future. | ||
| # Other modules should import helpers from here for consistency. | ||
| # TODO: handle the unicode returned by python3 and future versions! | ||
|
Contributor
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. Hmm, I suppose this is resolved or just works now but it would be nice to either comment about specifically in the PR / commit log or add a regression test for instead of just silently dropping the TODO. |
||
| # Perhaps switch to suggested "easiest option" from | ||
| # http://python-future.org/compatible_idioms.html#urllib-module | ||
| # once we have unicode/bytecode mix-up sorted out. | ||
| if sys.version_info[0] >= 3: | ||
| from urllib.parse import quote, unquote, urlencode, parse_qs, parse_qsl, \ | ||
| urlsplit, urlparse, urljoin | ||
| from urllib.request import urlopen | ||
| else: | ||
| from urllib import quote, unquote, urlencode, urlopen | ||
| from urlparse import parse_qs, parse_qsl, urlsplit, urlparse, urljoin | ||
| from urllib.parse import quote, unquote, urlencode, parse_qs, parse_qsl, \ | ||
| urlsplit, urlparse, urljoin | ||
| from urllib.request import urlopen | ||
|
|
||
| try: | ||
| from mig.shared.base import force_utf8, force_native_str | ||
|
|
||
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.
These are obsolete with the merge of #424