Skip to content

Commit

Permalink
Update python3 support. Fix python-mode#437
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Jun 11, 2014
1 parent ca0078b commit 0c54d08
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 25 deletions.
5 changes: 1 addition & 4 deletions pymode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ class Options(object):

def get_documentation():
""" Search documentation and append to current buffer. """
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from ._compat import StringIO

sys.stdout, _ = StringIO(), sys.stdout
help(vim.eval('a:word'))
Expand Down
98 changes: 98 additions & 0 deletions pymode/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
""" Compatibility.
Some py2/py3 compatibility support based on a stripped down
version of six so we don't have to depend on a specific version
of it.
:copyright: (c) 2014 by Armin Ronacher.
:license: BSD
"""
import sys

PY2 = sys.version_info[0] == 2
_identity = lambda x: x


if not PY2:
text_type = str
string_types = (str,)
integer_types = (int, )

iterkeys = lambda d: iter(d.keys())
itervalues = lambda d: iter(d.values())
iteritems = lambda d: iter(d.items())

from io import StringIO
from queue import Queue # noqa

def reraise(tp, value, tb=None):
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value

implements_to_string = _identity

else:
text_type = unicode
string_types = (str, unicode)
integer_types = (int, long)

iterkeys = lambda d: d.iterkeys()
itervalues = lambda d: d.itervalues()
iteritems = lambda d: d.iteritems()

from cStringIO import StringIO
from Queue import Queue

exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')

def implements_to_string(cls):
cls.__unicode__ = cls.__str__
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
return cls


def with_metaclass(meta, *bases):
# This requires a bit of explanation: the basic idea is to make a
# dummy metaclass for one level of class instantiation that replaces
# itself with the actual metaclass. Because of internal type checks
# we also need to make sure that we downgrade the custom metaclass
# for one level to something closer to type (that's why __call__ and
# __init__ comes back from type etc.).
#
# This has the advantage over six.with_metaclass in that it does not
# introduce dummy classes into the final MRO.
class metaclass(meta):
__call__ = type.__call__
__init__ = type.__init__
def __new__(cls, name, this_bases, d):
if this_bases is None:
return type.__new__(cls, name, (), d)
return meta(name, bases, d)
return metaclass('temporary_class', None, {})


# Certain versions of pypy have a bug where clearing the exception stack
# breaks the __exit__ function in a very peculiar way. This is currently
# true for pypy 2.2.1 for instance. The second level of exception blocks
# is necessary because pypy seems to forget to check if an exception
# happend until the next bytecode instruction?
BROKEN_PYPY_CTXMGR_EXIT = False
if hasattr(sys, 'pypy_version_info'):
class _Mgr(object):
def __enter__(self):
return self
def __exit__(self, *args):
sys.exc_clear()
try:
try:
with _Mgr():
raise AssertionError()
except:
raise
except TypeError:
BROKEN_PYPY_CTXMGR_EXIT = True
except AssertionError:
pass

# pylama:skip=1
5 changes: 1 addition & 4 deletions pymode/async.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
""" Python-mode async support. """

try:
from Queue import Queue
except ImportError:
from queue import Queue # noqa
from ._compat import Queue


RESULTS = Queue()
2 changes: 1 addition & 1 deletion pymode/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time
import os.path

from .utils import PY2
from ._compat import PY2


class VimPymodeEnviroment(object):
Expand Down
3 changes: 1 addition & 2 deletions pymode/rope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import re
import site
import sys
import StringIO

from rope.base import project, libutils, exceptions, change, worder # noqa
from rope.base.fscommands import FileSystemCommands # noqa
from rope.base.taskhandle import TaskHandle # noqa
from rope.contrib import autoimport as rope_autoimport, codeassist, findit, generate # noqa
from rope.refactor import ModuleToPackage, ImportOrganizer, rename, extract, inline, usefunction, move, change_signature, importutils # noqa


from ._compat import StringIO
from .environment import env


Expand Down
9 changes: 2 additions & 7 deletions pymode/run.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
""" Code runnning support. """

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

import sys
from re import compile as re

from ._compat import StringIO
from .environment import env
from re import compile as re


encoding = re(r'#[^\w]+coding:\s+utf.*$')
Expand Down
8 changes: 1 addition & 7 deletions pymode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
from contextlib import contextmanager

import vim # noqa


try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from ._compat import StringIO, PY2


DEBUG = int(vim.eval('g:pymode_debug'))
PY2 = sys.version_info[0] == 2

warnings.filterwarnings('ignore')

Expand Down

0 comments on commit 0c54d08

Please sign in to comment.