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

Drop python2 specific code #486

Merged
merged 7 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
python-version: ['3.7', '3.8', '3.9', '3.10']
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions rope/base/fscommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ def read_str_coding(source):


def _find_coding(text):
if isinstance(text, pycompat.str):
if isinstance(text, str):
text = text.encode("utf-8")
coding = b"coding"
to_chr = chr if pycompat.PY3 else lambda x: x
to_chr = chr
try:
start = text.index(coding) + len(coding)
if text[start] not in b"=:":
Expand Down
4 changes: 2 additions & 2 deletions rope/base/oi/runmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _get_persisted_class(self, object_):
return ("unknown",)

def _get_persisted_builtin(self, object_):
if isinstance(object_, pycompat.string_types):
if isinstance(object_, str):
return ("builtin", "str")
if isinstance(object_, list):
holding = None
Expand Down Expand Up @@ -194,7 +194,7 @@ def _object_to_persisted_form(self, object_):
return self._get_persisted_code(object_.__func__.__code__)
if isinstance(object_, types.ModuleType):
return self._get_persisted_module(object_)
if isinstance(object_, pycompat.string_types + (list, dict, tuple, set)):
if isinstance(object_, (str, list, dict, tuple, set)):
return self._get_persisted_builtin(object_)
if isinstance(object_, type):
return self._get_persisted_class(object_)
Expand Down
11 changes: 2 additions & 9 deletions rope/base/oi/type_hinting/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ def __init__(self, lexer):

def parse(self, program):
generator = self.lexer.tokenize(program)
try:
self.next = generator.__next__ # PY3
except AttributeError:
self.next = generator.next
self.next = generator.__next__
self.token = self.next()
return self.expression()

Expand Down Expand Up @@ -354,11 +351,7 @@ def __call__(self, program, pyobject):
:type program: str or rope.base.oi.type_hinting.evaluate.SymbolBase
:rtype: rope.base.pyobjects.PyDefinedObject | rope.base.pyobjects.PyObject or None
"""
ast = (
self.compile(program)
if isinstance(program, pycompat.string_types)
else program
)
ast = self.compile(program) if isinstance(program, str) else program
return ast.evaluate(pyobject)


Expand Down
12 changes: 0 additions & 12 deletions rope/base/oi/type_hinting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ class ParametrizeType:
"collections.abc.Iterable": "rope.base.builtins.get_iterator", # Python3.3
"collections.abc.Iterator": "rope.base.builtins.get_iterator", # Python3.3
}
if pycompat.PY2:
_supported_mapping = dict(
(
(
k.replace("builtins.", "__builtin__.").replace(
"_collections_abc.", "_abcoll."
),
v,
)
for k, v in _supported_mapping.items()
)
)

def __call__(self, pyobject, *args, **kwargs):
"""
Expand Down
2 changes: 1 addition & 1 deletion rope/base/pyobjectsdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def _AsyncWith(self, node):
return self._With(node)

def _excepthandler(self, node):
node_name_type = str if pycompat.PY3 else ast.Name
node_name_type = str
if node.name is not None and isinstance(node.name, node_name_type):
type_node = node.type
if isinstance(node.type, ast.Tuple) and type_node.elts:
Expand Down
11 changes: 2 additions & 9 deletions rope/base/stdmods.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import inspect
import os
import re
import sys

from rope.base import utils
from rope.base.utils import pycompat


def _stdlib_path():
if pycompat.PY2:
from distutils import sysconfig

return sysconfig.get_python_lib(standard_lib=True, plat_specific=True)
elif pycompat.PY3:
import inspect

return os.path.dirname(inspect.getsourcefile(inspect))
return os.path.dirname(inspect.getsourcefile(inspect))


@utils.cached(1)
Expand Down
4 changes: 1 addition & 3 deletions rope/base/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def __call__(self, *args, **kwds):

def resolve(str_or_obj):
"""Returns object from string"""
from rope.base.utils.pycompat import string_types

if not isinstance(str_or_obj, string_types):
if not isinstance(str_or_obj, str):
return str_or_obj
if "." not in str_or_obj:
str_or_obj += "."
Expand Down
53 changes: 12 additions & 41 deletions rope/base/utils/pycompat.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
import sys
import ast
import builtins

# from rope.base import ast

PY2 = sys.version_info[0] == 2
PY27 = sys.version_info[0:2] >= (2, 7)
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)
PY36 = sys.version_info[0:2] >= (3, 6)
ast_arg_type = ast.arg

try:
str = unicode
except NameError: # PY3

str = str
string_types = (str,)
import builtins
def execfile(fn, global_vars=None, local_vars=None):
with open(fn) as f:
code = compile(f.read(), fn, "exec")
exec(code, global_vars or {}, local_vars)

ast_arg_type = ast.arg

def execfile(fn, global_vars=None, local_vars=None):
with open(fn) as f:
code = compile(f.read(), fn, "exec")
exec(code, global_vars or {}, local_vars)
def get_ast_arg_arg(node):
if isinstance(node, str): # TODO: G21: Understand the Algorithm (Where it's used?)
return node
return node.arg

def get_ast_arg_arg(node):
if isinstance(
node, string_types
): # TODO: G21: Understand the Algorithm (Where it's used?)
return node
return node.arg

def get_ast_with_items(node):
return node.items

else: # PY2

string_types = (basestring,)
builtins = __import__("__builtin__")
ast_arg_type = ast.Name
execfile = execfile

def get_ast_arg_arg(node):
if isinstance(node, string_types): # Python2 arguments.vararg, arguments.kwarg
return node
return node.id

def get_ast_with_items(node):
return [node]
def get_ast_with_items(node):
return node.items
2 changes: 1 addition & 1 deletion rope/refactor/occurrences.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def _re_search(self, source):
for match in self.pattern.finditer(source):
if match.groupdict()["occurrence"]:
yield match.start("occurrence")
elif utils.pycompat.PY36 and match.groupdict()["fstring"]:
elif match.groupdict()["fstring"]:
f_string = match.groupdict()["fstring"]
for occurrence_node in self._search_in_f_string(f_string):
yield match.start("fstring") + occurrence_node.col_offset
Expand Down
57 changes: 15 additions & 42 deletions rope/refactor/patchedast.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,30 +746,11 @@ def _Print(self, node):
self._handle(node, children)

def _Raise(self, node):
def get_python3_raise_children(node):
children = ["raise"]
if node.exc:
children.append(node.exc)
if node.cause:
children.append(node.cause)
return children

def get_python2_raise_children(node):
children = ["raise"]
if node.type:
children.append(node.type)
if node.inst:
children.append(",")
children.append(node.inst)
if node.tback:
children.append(",")
children.append(node.tback)
return children

if pycompat.PY2:
children = get_python2_raise_children(node)
else:
children = get_python3_raise_children(node)
children = ["raise"]
if node.exc:
children.append(node.exc)
if node.cause:
children.append(node.cause)
self._handle(node, children)

def _Return(self, node):
Expand Down Expand Up @@ -810,23 +791,18 @@ def _TryFinally(self, node):
is_there_except_handler = False
not_empty_body = True
if len(node.finalbody) == 1:
if pycompat.PY2:
is_there_except_handler = isinstance(node.body[0], ast.TryExcept)
not_empty_body = not bool(len(node.body))
elif pycompat.PY3:
try:
is_there_except_handler = isinstance(
node.handlers[0], ast.ExceptHandler
)
not_empty_body = True
except IndexError:
pass
try:
is_there_except_handler = isinstance(
node.handlers[0], ast.ExceptHandler
)
not_empty_body = True
except IndexError:
pass
children = []
if not_empty_body or not is_there_except_handler:
children.extend(["try", ":"])
children.extend(node.body)
if pycompat.PY3:
children.extend(node.handlers)
children.extend(node.handlers)
children.extend(["finally", ":"])
children.extend(node.finalbody)
self._handle(node, children)
Expand Down Expand Up @@ -906,11 +882,8 @@ def _handle_with_node(self, node, is_async):
children.extend([self.with_or_comma_context_manager, item.context_expr])
if item.optional_vars:
children.extend(["as", item.optional_vars])
if pycompat.PY2 and COMMA_IN_WITH_PATTERN.search(self.source.source):
children.append(node.body[0])
else:
children.append(":")
children.extend(node.body)
children.append(":")
children.extend(node.body)
self._handle(node, children)

def _With(self, node):
Expand Down
17 changes: 7 additions & 10 deletions rope/refactor/suites.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,14 @@ def _Match(self, node):
def _TryFinally(self, node):
proceed_to_except_handler = False
if len(node.finalbody) == 1:
if pycompat.PY2:
proceed_to_except_handler = isinstance(node.body[0], ast.TryExcept)
elif pycompat.PY3:
try:
proceed_to_except_handler = isinstance(
node.handlers[0], ast.ExceptHandler
)
except IndexError:
pass
try:
proceed_to_except_handler = isinstance(
node.handlers[0], ast.ExceptHandler
)
except IndexError:
pass
if proceed_to_except_handler:
self._TryExcept(node if pycompat.PY3 else node.body[0])
self._TryExcept(node)
else:
self.suites.append(Suite(node.body, node.lineno, self.suite))
self.suites.append(Suite(node.finalbody, node.lineno, self.suite))
Expand Down
11 changes: 1 addition & 10 deletions ropetest/advanced_oi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,13 @@ def a_func(arg):

def test_dict_keys_and_dynamicoi(self):
mod = testutils.create_module(self.project, "mod")
if pycompat.PY3:
code = dedent("""\
code = dedent("""\
class C(object):
pass
def a_func(arg):
return eval("arg")
a_var = list(a_func({C(): 1}))[0]
""")
else:
code = dedent("""\
class C(object):
pass
def a_func(arg):
return eval("arg")
a_var = a_func({C(): 1}).keys()[0]
""")
mod.write(code)
self.pycore.run_module(mod).wait_process()
pymod = self.project.get_pymodule(mod)
Expand Down