issue #16: initial smorgasbord of 3.x fixes.

pull/193/head
David Wilson 8 years ago
parent c4bef102fe
commit 4c8ec131f9

@ -26,8 +26,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
import cPickle
import cStringIO
import collections import collections
import errno import errno
import fcntl import fcntl
@ -40,7 +38,6 @@ import signal
import socket import socket
import struct import struct
import sys import sys
import thread
import threading import threading
import time import time
import traceback import traceback
@ -48,6 +45,16 @@ import warnings
import weakref import weakref
import zlib import zlib
try:
import cPickle
except ImportError:
import pickle as cPickle
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
# TODO: usage of 'import' after setting __name__, but before fixing up # TODO: usage of 'import' after setting __name__, but before fixing up
# sys.modules generates a warning. This happens when profiling = True. # sys.modules generates a warning. This happens when profiling = True.
warnings.filterwarnings('ignore', warnings.filterwarnings('ignore',
@ -69,6 +76,12 @@ ALLOCATE_ID = 105
SHUTDOWN = 106 SHUTDOWN = 106
LOAD_MODULE = 107 LOAD_MODULE = 107
PY3 = sys.version_info > (3,)
if PY3:
b = lambda s: s.encode('latin-1')
else:
b = str
CHUNK_SIZE = 131072 CHUNK_SIZE = 131072
_tls = threading.local() _tls = threading.local()
@ -344,9 +357,12 @@ class Message(object):
_vv and IOLOG.debug('%r.unpickle()', self) _vv and IOLOG.debug('%r.unpickle()', self)
obj = self._unpickled obj = self._unpickled
if obj is Message._unpickled: if obj is Message._unpickled:
fp = cStringIO.StringIO(self.data) fp = BytesIO(self.data)
unpickler = cPickle.Unpickler(fp) unpickler = cPickle.Unpickler(fp)
unpickler.find_global = self._find_global try:
unpickler.find_global = self._find_global
except AttributeError:
unpickler.find_class = self._find_global
try: try:
# Must occur off the broker thread. # Must occur off the broker thread.
@ -679,7 +695,10 @@ class Importer(object):
flags = 0x4000 flags = 0x4000
source = self.get_source(fullname) source = self.get_source(fullname)
code = compile(source, mod.__file__, 'exec', flags, True) code = compile(source, mod.__file__, 'exec', flags, True)
exec code in vars(mod) if PY3:
exec(code, vars(mod))
else:
exec('exec code in vars(mod)')
return mod return mod
def get_filename(self, fullname): def get_filename(self, fullname):
@ -1154,7 +1173,7 @@ class Waker(BasicStream):
self._broker.shutdown() self._broker.shutdown()
def defer(self, func, *args, **kwargs): def defer(self, func, *args, **kwargs):
if thread.get_ident() == self.broker_ident: if threading.currentThread().ident == self.broker_ident:
_vv and IOLOG.debug('%r.defer() [immediate]', self) _vv and IOLOG.debug('%r.defer() [immediate]', self)
return func(*args, **kwargs) return func(*args, **kwargs)
@ -1169,7 +1188,7 @@ class Waker(BasicStream):
# of tearing itself down, the waker fd may already have been closed, so # of tearing itself down, the waker fd may already have been closed, so
# ignore EBADF here. # ignore EBADF here.
try: try:
self.transmit_side.write(' ') self.transmit_side.write(b(' '))
except OSError: except OSError:
e = sys.exc_info()[1] e = sys.exc_info()[1]
if e[0] != errno.EBADF: if e[0] != errno.EBADF:

@ -26,7 +26,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
import cStringIO
import fcntl import fcntl
import getpass import getpass
import inspect import inspect
@ -43,6 +42,11 @@ import time
import types import types
import zlib import zlib
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
if sys.version_info < (2, 7, 11): if sys.version_info < (2, 7, 11):
from mitogen.compat import tokenize from mitogen.compat import tokenize
else: else:
@ -97,7 +101,7 @@ def is_immediate_child(msg, stream):
def minimize_source(source): def minimize_source(source):
"""Remove most comments and docstrings from Python source code. """Remove most comments and docstrings from Python source code.
""" """
tokens = tokenize.generate_tokens(cStringIO.StringIO(source).readline) tokens = tokenize.generate_tokens(BytesIO(source).readline)
tokens = strip_comments(tokens) tokens = strip_comments(tokens)
tokens = strip_docstrings(tokens) tokens = strip_docstrings(tokens)
tokens = reindent(tokens) tokens = reindent(tokens)

@ -30,10 +30,14 @@
Functionality to allow establishing new slave contexts over an SSH connection. Functionality to allow establishing new slave contexts over an SSH connection.
""" """
import commands
import logging import logging
import time import time
try:
from shlex import quote as shlex_quote
except ImportError:
from pipes import quote as shlex_quote
import mitogen.parent import mitogen.parent
@ -108,7 +112,7 @@ class Stream(mitogen.parent.Stream):
bits += self.ssh_args bits += self.ssh_args
bits.append(self.hostname) bits.append(self.hostname)
base = super(Stream, self).get_boot_command() base = super(Stream, self).get_boot_command()
return bits + [commands.mkarg(s).strip() for s in base] return bits + [shlex_quote(s).strip() for s in base]
def connect(self): def connect(self):
super(Stream, self).connect() super(Stream, self).connect()

Loading…
Cancel
Save