-
Notifications
You must be signed in to change notification settings - Fork 148
Remove Python 2 compatibility code for Python 3.8+ #255
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
Open
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/update-beaker-to-python-3-8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,169 +1,82 @@ | ||
| from __future__ import absolute_import | ||
| import sys | ||
| """Compatibility module for Python 3.8+. | ||
|
|
||
| This module provides compatibility aliases and utility functions | ||
| that were previously used to support both Python 2 and Python 3. | ||
| It is now simplified for Python 3.8+ only. | ||
| """ | ||
| import pickle | ||
| import http.cookies as http_cookies | ||
| from base64 import b64decode as _b64decode, b64encode as _b64encode | ||
| from urllib.parse import urlencode as url_encode | ||
| from urllib.parse import quote as url_quote | ||
| from urllib.parse import unquote as url_unquote | ||
| from urllib.parse import urlparse as url_parse | ||
| from urllib.request import url2pathname | ||
| from inspect import signature as func_signature | ||
|
|
||
| # True if we are running on Python 2. | ||
| PY2 = sys.version_info[0] == 2 | ||
| PYVER = sys.version_info[:2] | ||
| JYTHON = sys.platform.startswith('java') | ||
|
|
||
| if PY2 and not JYTHON: # pragma: no cover | ||
| import cPickle as pickle | ||
| else: # pragma: no cover | ||
| import pickle | ||
|
|
||
|
|
||
| if not PY2: # pragma: no cover | ||
| xrange_ = range | ||
| NoneType = type(None) | ||
|
|
||
| string_type = str | ||
| unicode_text = str | ||
| byte_string = bytes | ||
|
|
||
| from urllib.parse import urlencode as url_encode | ||
| from urllib.parse import quote as url_quote | ||
| from urllib.parse import unquote as url_unquote | ||
| from urllib.parse import urlparse as url_parse | ||
| from urllib.request import url2pathname | ||
| import http.cookies as http_cookies | ||
| from base64 import b64decode as _b64decode, b64encode as _b64encode | ||
|
|
||
| try: | ||
| import dbm.gnu as anydbm | ||
| except ImportError: | ||
| import dbm.dumb as anydbm | ||
|
|
||
| def b64decode(b): | ||
| return _b64decode(b.encode('ascii')) | ||
| try: | ||
| import dbm.gnu as anydbm | ||
| except ImportError: | ||
| import dbm.dumb as anydbm | ||
|
|
||
| def b64encode(s): | ||
| return _b64encode(s).decode('ascii') | ||
| # Type aliases for backwards compatibility | ||
| NoneType = type(None) | ||
| string_type = str | ||
| unicode_text = str | ||
| byte_string = bytes | ||
|
|
||
| def u_(s): | ||
| return str(s) | ||
|
|
||
| def bytes_(s): | ||
| if isinstance(s, byte_string): | ||
| return s | ||
| return str(s).encode('ascii', 'strict') | ||
| def b64decode(b): | ||
| """Base64 decode a string, returning bytes.""" | ||
| return _b64decode(b.encode('ascii')) | ||
|
|
||
| def dictkeyslist(d): | ||
| return list(d.keys()) | ||
|
|
||
| else: | ||
| xrange_ = xrange | ||
| from types import NoneType | ||
| def b64encode(s): | ||
| """Base64 encode bytes, returning a string.""" | ||
| return _b64encode(s).decode('ascii') | ||
|
|
||
| string_type = basestring | ||
| unicode_text = unicode | ||
| byte_string = str | ||
|
|
||
| from urllib import urlencode as url_encode | ||
| from urllib import quote as url_quote | ||
| from urllib import unquote as url_unquote | ||
| from urlparse import urlparse as url_parse | ||
| from urllib import url2pathname | ||
| import Cookie as http_cookies | ||
| from base64 import b64decode, b64encode | ||
| import anydbm | ||
| def u_(s): | ||
| """Convert to string (unicode).""" | ||
| return str(s) | ||
amol- marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def u_(s): | ||
| if isinstance(s, unicode_text): | ||
| return s | ||
|
|
||
| if not isinstance(s, byte_string): | ||
| s = str(s) | ||
| return unicode(s, 'utf-8') | ||
| def bytes_(s): | ||
| """Convert to bytes.""" | ||
| if isinstance(s, bytes): | ||
| return s | ||
| return str(s).encode('ascii', 'strict') | ||
|
|
||
| def bytes_(s): | ||
| if isinstance(s, byte_string): | ||
| return s | ||
| return str(s) | ||
|
|
||
| def dictkeyslist(d): | ||
| return d.keys() | ||
| def dictkeyslist(d): | ||
| """Return dictionary keys as a list.""" | ||
| return list(d.keys()) | ||
|
|
||
|
|
||
| def im_func(f): | ||
| if not PY2: # pragma: no cover | ||
| return getattr(f, '__func__', None) | ||
| else: | ||
| return getattr(f, 'im_func', None) | ||
| """Get the function from a bound method.""" | ||
| return getattr(f, '__func__', None) | ||
|
|
||
|
|
||
| def default_im_func(f): | ||
| if not PY2: # pragma: no cover | ||
| return getattr(f, '__func__', f) | ||
| else: | ||
| return getattr(f, 'im_func', f) | ||
| """Get the function from a bound method, or return the function itself.""" | ||
| return getattr(f, '__func__', f) | ||
|
|
||
|
|
||
| def im_self(f): | ||
| if not PY2: # pragma: no cover | ||
| return getattr(f, '__self__', None) | ||
| else: | ||
| return getattr(f, 'im_self', None) | ||
| """Get the instance from a bound method.""" | ||
| return getattr(f, '__self__', None) | ||
|
|
||
|
|
||
| def im_class(f): | ||
| if not PY2: # pragma: no cover | ||
| self = im_self(f) | ||
| if self is not None: | ||
| return self.__class__ | ||
| else: | ||
| return None | ||
| else: | ||
| return getattr(f, 'im_class', None) | ||
|
|
||
|
|
||
| def add_metaclass(metaclass): | ||
| """Class decorator for creating a class with a metaclass.""" | ||
| def wrapper(cls): | ||
| orig_vars = cls.__dict__.copy() | ||
| slots = orig_vars.get('__slots__') | ||
| if slots is not None: | ||
| if isinstance(slots, str): | ||
| slots = [slots] | ||
| for slots_var in slots: | ||
| orig_vars.pop(slots_var) | ||
| orig_vars.pop('__dict__', None) | ||
| orig_vars.pop('__weakref__', None) | ||
| return metaclass(cls.__name__, cls.__bases__, orig_vars) | ||
| return wrapper | ||
|
|
||
|
|
||
| if not PY2: # pragma: no cover | ||
| import builtins | ||
| exec_ = getattr(builtins, "exec") | ||
|
|
||
| def reraise(tp, value, tb=None): | ||
| if value.__traceback__ is not tb: | ||
| raise value.with_traceback(tb) | ||
| raise value | ||
| else: # pragma: no cover | ||
| def exec_(code, globs=None, locs=None): | ||
| """Execute code in a namespace.""" | ||
| if globs is None: | ||
| frame = sys._getframe(1) | ||
| globs = frame.f_globals | ||
| if locs is None: | ||
| locs = frame.f_locals | ||
| del frame | ||
| elif locs is None: | ||
| locs = globs | ||
| exec("""exec code in globs, locs""") | ||
|
|
||
| exec_("""def reraise(tp, value, tb=None): | ||
| raise tp, value, tb | ||
| """) | ||
|
|
||
|
|
||
| try: | ||
| from inspect import signature as func_signature | ||
| except ImportError: | ||
| from funcsigs import signature as func_signature | ||
| """Get the class from a bound method.""" | ||
| self = im_self(f) | ||
| if self is not None: | ||
| return self.__class__ | ||
| return None | ||
|
|
||
|
|
||
| def bindfuncargs(arginfo, args, kwargs): | ||
| """Bind function arguments to their parameters.""" | ||
| boundargs = arginfo.bind(*args, **kwargs) | ||
| return boundargs.args, boundargs.kwargs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.