From 3ff3ad23aa4846c2bf370711245f0b9142df78ad Mon Sep 17 00:00:00 2001 From: Federico Mon Date: Thu, 28 Nov 2013 19:36:45 +0100 Subject: [PATCH] python 3 compliance --- pylibs/pymode/queue.py | 2 +- pylibs/rope/base/ast.py | 2 +- pylibs/rope/base/builtins.py | 32 +++++++++++++++---------------- pylibs/rope/base/change.py | 2 +- pylibs/rope/base/codeanalyze.py | 6 +++--- pylibs/rope/base/oi/doa.py | 4 ++-- pylibs/rope/base/oi/objectdb.py | 9 +++++---- pylibs/rope/base/project.py | 2 +- pylibs/rope/base/pyobjectsdef.py | 4 ++-- pylibs/rope/base/resources.py | 2 +- pylibs/rope/contrib/codeassist.py | 2 +- pylibs/rope/contrib/fixsyntax.py | 2 +- pylibs/ropemode/decorators.py | 6 +++--- pylibs/ropevim.py | 6 +++--- 14 files changed, 41 insertions(+), 40 deletions(-) diff --git a/pylibs/pymode/queue.py b/pylibs/pymode/queue.py index e43de9c8..fb7e13db 100644 --- a/pylibs/pymode/queue.py +++ b/pylibs/pymode/queue.py @@ -1,7 +1,7 @@ from __future__ import absolute_import import threading -from Queue import Queue, Empty +from queue import Queue, Empty from .interface import show_message diff --git a/pylibs/rope/base/ast.py b/pylibs/rope/base/ast.py index f6a9d88d..50070fd3 100644 --- a/pylibs/rope/base/ast.py +++ b/pylibs/rope/base/ast.py @@ -14,7 +14,7 @@ def parse(source, filename=''): source += '\n' try: return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST) - except (TypeError, ValueError), e: + except (TypeError, ValueError) as e: error = SyntaxError() error.lineno = 1 error.filename = filename diff --git a/pylibs/rope/base/builtins.py b/pylibs/rope/base/builtins.py index 78e7afb0..6e791391 100644 --- a/pylibs/rope/base/builtins.py +++ b/pylibs/rope/base/builtins.py @@ -263,7 +263,7 @@ def __init__(self, holding=None): # Getting methods collector('__getitem__', function=self._list_get) collector('pop', function=self._list_get) - collector('__getslice__', function=self._self_get) + #collector('__getslice__', function=self._self_get) super(List, self).__init__(list, collector.attributes) @@ -483,8 +483,8 @@ def __init__(self): collector = _AttributeCollector(str) collector('__iter__', get_iterator(self_object), check_existence=False) - self_methods = ['__getitem__', '__getslice__', 'capitalize', 'center', - 'decode', 'encode', 'expandtabs', 'join', 'ljust', + self_methods = ['__getitem__', 'capitalize', 'center', + 'encode', 'expandtabs', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rjust', 'rstrip', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] for method in self_methods: @@ -560,18 +560,18 @@ def __init__(self): str_object = get_str() str_list = get_list(get_str()) attributes = {} - def add(name, returned=None, function=None): - builtin = getattr(file, name, None) - attributes[name] = BuiltinName( - BuiltinFunction(returned=returned, function=function, - builtin=builtin)) - add('__iter__', get_iterator(str_object)) - for method in ['next', 'read', 'readline', 'readlines']: - add(method, str_list) - for method in ['close', 'flush', 'lineno', 'isatty', 'seek', 'tell', - 'truncate', 'write', 'writelines']: - add(method) - super(File, self).__init__(file, attributes) + #def add(name, returned=None, function=None): + # builtin = getattr(file, name, None) + # attributes[name] = BuiltinName( + # BuiltinFunction(returned=returned, function=function, + # builtin=builtin)) + #add('__iter__', get_iterator(str_object)) + #for method in ['next', 'read', 'readline', 'readlines']: + # add(method, str_list) + #for method in ['close', 'flush', 'lineno', 'isatty', 'seek', 'tell', + # 'truncate', 'write', 'writelines']: + # add(method) + #super(File, self).__init__(file, attributes) get_file = _create_builtin_getter(File) @@ -761,7 +761,7 @@ def _input_function(args): 'object': BuiltinName(BuiltinObject()), 'type': BuiltinName(BuiltinType()), 'iter': BuiltinName(BuiltinFunction(function=_iter_function, builtin=iter)), - 'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input)), + #'raw_input': BuiltinName(BuiltinFunction(function=_input_function, builtin=raw_input)), } builtins = BuiltinModule('__builtin__', initial=_initial_builtins) diff --git a/pylibs/rope/base/change.py b/pylibs/rope/base/change.py index 8d19aac1..3b0d8a14 100644 --- a/pylibs/rope/base/change.py +++ b/pylibs/rope/base/change.py @@ -367,7 +367,7 @@ def _create_resource(self, file_name, kind='file'): fscommands.create_file(resource_path) else: fscommands.create_folder(resource_path) - except IOError, e: + except IOError as e: raise exceptions.RopeError(e) diff --git a/pylibs/rope/base/codeanalyze.py b/pylibs/rope/base/codeanalyze.py index 3d2a2a45..14e60bbd 100644 --- a/pylibs/rope/base/codeanalyze.py +++ b/pylibs/rope/base/codeanalyze.py @@ -174,7 +174,7 @@ def logical_line_in(self, line_number): block_start = get_block_start(self.lines, line_number, indents) try: return self._block_logical_line(block_start, line_number) - except IndentationError, e: + except IndentationError as e: tries += 1 if tries == 5: raise e @@ -199,7 +199,7 @@ def generate_regions(self, start_line=1, end_line=None): real_end = end + block_start - 1 if real_start >= start_line: yield (real_start, real_end) - except tokenize.TokenError, e: + except tokenize.TokenError as e: pass def _block_logical_line(self, block_start, line_number): @@ -220,7 +220,7 @@ def _calculate_logical(self, readline, line_number): if line_number <= end: return (start, end) last_end = end + 1 - except tokenize.TokenError, e: + except tokenize.TokenError as e: current = e.args[1][0] return (last_end, max(last_end, current - 1)) return (last_end, None) diff --git a/pylibs/rope/base/oi/doa.py b/pylibs/rope/base/oi/doa.py index 12f50553..10fbba96 100644 --- a/pylibs/rope/base/oi/doa.py +++ b/pylibs/rope/base/oi/doa.py @@ -1,4 +1,4 @@ -import cPickle as pickle +import pickle as pickle import marshal import os import socket @@ -114,7 +114,7 @@ def __init__(self): try: self.server_socket.bind(('', self.data_port)) break - except socket.error, e: + except socket.error as e: self.data_port += 1 self.server_socket.listen(1) diff --git a/pylibs/rope/base/oi/objectdb.py b/pylibs/rope/base/oi/objectdb.py index 6f988add..ad5f9bd1 100644 --- a/pylibs/rope/base/oi/objectdb.py +++ b/pylibs/rope/base/oi/objectdb.py @@ -1,4 +1,5 @@ -import UserDict +from collections import UserDict +from collections import MutableMapping class ObjectDB(object): @@ -78,7 +79,7 @@ def _get_scope_info(self, path, key, readonly=True): self.files[path].create_scope(key) result = self.files[path][key] if isinstance(result, dict): - print self.files, self.files[path], self.files[path][key] + print(self.files, self.files[path], self.files[path][key]) return result def _file_removed(self, path): @@ -120,13 +121,13 @@ def add_call(self, parameters, returned): raise NotImplementedError() -class FileInfo(UserDict.DictMixin): +class FileInfo(MutableMapping): def create_scope(self, key): pass -class FileDict(UserDict.DictMixin): +class FileDict(MutableMapping): def create(self, key): pass diff --git a/pylibs/rope/base/project.py b/pylibs/rope/base/project.py index 90b8fd17..4ab21cbb 100644 --- a/pylibs/rope/base/project.py +++ b/pylibs/rope/base/project.py @@ -1,4 +1,4 @@ -import cPickle as pickle +import pickle as pickle import os import shutil import sys diff --git a/pylibs/rope/base/pyobjectsdef.py b/pylibs/rope/base/pyobjectsdef.py index 50b24360..eef05601 100644 --- a/pylibs/rope/base/pyobjectsdef.py +++ b/pylibs/rope/base/pyobjectsdef.py @@ -178,9 +178,9 @@ def _init_source(self, pycore, source_code, resource): else: source_bytes = source_code ast_node = ast.parse(source_bytes, filename=filename) - except SyntaxError, e: + except SyntaxError as e: raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg) - except UnicodeDecodeError, e: + except UnicodeDecodeError as e: raise exceptions.ModuleSyntaxError(filename, 1, '%s' % (e.reason)) return source_code, ast_node diff --git a/pylibs/rope/base/resources.py b/pylibs/rope/base/resources.py index 46beadb0..e5640743 100644 --- a/pylibs/rope/base/resources.py +++ b/pylibs/rope/base/resources.py @@ -81,7 +81,7 @@ def read(self): data = self.read_bytes() try: return rope.base.fscommands.file_data_to_unicode(data) - except UnicodeDecodeError, e: + except UnicodeDecodeError as e: raise exceptions.ModuleDecodeError(self.path, e.reason) def read_bytes(self): diff --git a/pylibs/rope/contrib/codeassist.py b/pylibs/rope/contrib/codeassist.py index 37433c2a..19b949f9 100644 --- a/pylibs/rope/contrib/codeassist.py +++ b/pylibs/rope/contrib/codeassist.py @@ -423,7 +423,7 @@ def _keyword_parameters(self, pymodule, scope): try: function_pyname = rope.base.evaluate.\ eval_str(scope, primary) - except exceptions.BadIdentifierError, e: + except exceptions.BadIdentifierError as e: return {} if function_pyname is not None: pyobject = function_pyname.get_object() diff --git a/pylibs/rope/contrib/fixsyntax.py b/pylibs/rope/contrib/fixsyntax.py index fc62a060..bbdb8b76 100644 --- a/pylibs/rope/contrib/fixsyntax.py +++ b/pylibs/rope/contrib/fixsyntax.py @@ -26,7 +26,7 @@ def get_pymodule(self): force_errors=True) return self.pycore.get_string_module( code, resource=self.resource, force_errors=True) - except exceptions.ModuleSyntaxError, e: + except exceptions.ModuleSyntaxError as e: if tries < self.maxfixes: tries += 1 self.commenter.comment(e.lineno) diff --git a/pylibs/ropemode/decorators.py b/pylibs/ropemode/decorators.py index 6a610728..652ed3d8 100644 --- a/pylibs/ropemode/decorators.py +++ b/pylibs/ropemode/decorators.py @@ -16,7 +16,7 @@ def __call__(self, message, short=None): def _show(self, message): if self.message is None: - print message + print(message) else: self.message(message) @@ -27,7 +27,7 @@ def lisphook(func): def newfunc(*args, **kwds): try: func(*args, **kwds) - except Exception, e: + except Exception as e: trace = str(traceback.format_exc()) short = 'Ignored an exception in ropemode hook: %s' % \ _exception_message(e) @@ -51,7 +51,7 @@ def _exception_handler(func): def newfunc(*args, **kwds): try: return func(*args, **kwds) - except exceptions.RopeError, e: + except exceptions.RopeError as e: short = None if isinstance(e, input_exceptions): short = _exception_message(e) diff --git a/pylibs/ropevim.py b/pylibs/ropevim.py index 9692c828..6b9e654c 100644 --- a/pylibs/ropevim.py +++ b/pylibs/ropevim.py @@ -301,7 +301,7 @@ def _add_command(self, name, callback, key, prefix, prekey): _vim_name(name), _vim_name(name) ) ) - if key is not None: + if key is not None and prekey: key = prekey + key.replace(' ', '') vim.command( 'noremap {0} :call {1}()'.format(key, _vim_name(name)) @@ -380,7 +380,7 @@ def done(self): def echo(message): message = message.encode(VimUtils._get_encoding()) - print message + print(message) def status(message): @@ -399,7 +399,7 @@ class _ValueCompleter(object): def __init__(self): self.values = [] - vim.command('python import vim') + vim.command('py3 import vim') vim.command('function! RopeValueCompleter(A, L, P)\n' 'python args = [vim.eval("a:" + p) for p in "ALP"]\n' 'python ropevim._completer(*args)\n'