Skip to content

Commit

Permalink
Deprecated py2 support by removing blocks executed if not is_py_3k; i…
Browse files Browse the repository at this point in the history
…mplementation/style may be improved elsewhere (e.g. try-blocks in compat, rpyc.core.NetrefMetaclass, module.builtin instead of module.builtins only, etc.)
  • Loading branch information
comrumino committed Nov 7, 2020
1 parent 9790cfd commit 7afaec2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 153 deletions.
51 changes: 9 additions & 42 deletions rpyc/core/brine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""*Brine* is a simple, fast and secure object serializer for **immutable** objects.
The following types are supported: ``int``, ``long``, ``bool``, ``str``, ``float``,
The following types are supported: ``int``, ``bool``, ``str``, ``float``,
``unicode``, ``bytes``, ``slice``, ``complex``, ``tuple`` (of simple types),
``frozenset`` (of simple types) as well as the following singletons: ``None``,
``NotImplemented``, and ``Ellipsis``.
Expand All @@ -17,7 +17,7 @@
>>> x == z
True
"""
from rpyc.lib.compat import Struct, BytesIO, is_py_3k, BYTES_LITERAL
from rpyc.lib.compat import Struct, BytesIO, BYTES_LITERAL


# singletons
Expand All @@ -30,7 +30,7 @@
TAG_ELLIPSIS = b"\x06"
# types
TAG_UNICODE = b"\x08"
TAG_LONG = b"\x09"
# deprecated w/ py2 support TAG_LONG = b"\x09"
TAG_STR1 = b"\x0a"
TAG_STR2 = b"\x0b"
TAG_STR3 = b"\x0c"
Expand All @@ -49,10 +49,7 @@
TAG_SLICE = b"\x19"
TAG_FSET = b"\x1a"
TAG_COMPLEX = b"\x1b"
if is_py_3k:
IMM_INTS = dict((i, bytes([i + 0x50])) for i in range(-0x30, 0xa0))
else:
IMM_INTS = dict((i, chr(i + 0x50)) for i in range(-0x30, 0xa0))
IMM_INTS = dict((i, bytes([i + 0x50])) for i in range(-0x30, 0xa0))

I1 = Struct("!B")
I4 = Struct("!L")
Expand Down Expand Up @@ -156,13 +153,6 @@ def _dump_str(obj, stream):
_dump_bytes(obj.encode("utf8"), stream)


if not is_py_3k:
@register(_dump_registry, long) # noqa: F821
def _dump_long(obj, stream):
stream.append(TAG_LONG)
_dump_int(obj, stream)


@register(_dump_registry, tuple)
def _dump_tuple(obj, stream):
lenobj = len(obj)
Expand Down Expand Up @@ -229,18 +219,6 @@ def _load_empty_str(stream):
return b""


if is_py_3k:
@register(_load_registry, TAG_LONG)
def _load_long(stream):
obj = _load(stream)
return int(obj)
else:
@register(_load_registry, TAG_LONG)
def _load_long(stream):
obj = _load(stream)
return long(obj) # noqa: F821


@register(_load_registry, TAG_FLOAT)
def _load_float(stream):
return F8.unpack(stream.read(8))[0]
Expand Down Expand Up @@ -316,16 +294,10 @@ def _load_tup_l1(stream):
return tuple(_load(stream) for i in range(l))


if is_py_3k:
@register(_load_registry, TAG_TUP_L4)
def _load_tup_l4(stream):
l, = I4.unpack(stream.read(4))
return tuple(_load(stream) for i in range(l))
else:
@register(_load_registry, TAG_TUP_L4)
def _load_tup_l4(stream):
l, = I4.unpack(stream.read(4))
return tuple(_load(stream) for i in xrange(l)) # noqa
@register(_load_registry, TAG_TUP_L4)
def _load_tup_l4(stream):
l, = I4.unpack(stream.read(4))
return tuple(_load(stream) for i in range(l))


@register(_load_registry, TAG_SLICE)
Expand Down Expand Up @@ -385,12 +357,7 @@ def load(data):
return _load(stream)


if is_py_3k:
simple_types = frozenset([type(None), int, bool, float, bytes, str, complex,
type(NotImplemented), type(Ellipsis)])
else:
simple_types = frozenset([type(None), int, long, bool, float, bytes, unicode, complex, # noqa: F821
type(NotImplemented), type(Ellipsis)])
simple_types = frozenset([type(None), int, bool, float, bytes, str, complex, type(NotImplemented), type(Ellipsis)])


def dumpable(obj):
Expand Down
29 changes: 7 additions & 22 deletions rpyc/core/netref.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import types
from rpyc.lib import get_methods, get_id_pack
from rpyc.lib.compat import pickle, is_py_3k, maxint, with_metaclass
from rpyc.lib.compat import pickle, maxint, with_metaclass
from rpyc.core import consts


Expand All @@ -16,6 +16,7 @@
'__array_struct__', '__array_interface__',
])

"""the set of attributes that are local to the netref object"""
LOCAL_ATTRS = frozenset([
'____conn__', '____id_pack__', '____refcount__', '__class__', '__cmp__', '__del__', '__delattr__',
'__dir__', '__doc__', '__getattr__', '__getattribute__', '__hash__', '__instancecheck__',
Expand All @@ -24,11 +25,13 @@
'__weakref__', '__dict__', '__methods__', '__exit__',
'__eq__', '__ne__', '__lt__', '__gt__', '__le__', '__ge__',
]) | DELETED_ATTRS
"""the set of attributes that are local to the netref object"""

"""a list of types considered built-in (shared between connections)
TODO: with the deprecation of py2 support, why not iterate the members of the builtins module?
"""
_builtin_types = [
type, object, bool, complex, dict, float, int, list, slice, str, tuple, set,
frozenset, Exception, type(None), types.BuiltinFunctionType, types.GeneratorType,
frozenset, BaseException, Exception, type(None), types.BuiltinFunctionType, types.GeneratorType,
types.MethodType, types.CodeType, types.FrameType, types.TracebackType,
types.ModuleType, types.FunctionType,

Expand All @@ -37,26 +40,8 @@
type(iter([])), # listiterator
type(iter(())), # tupleiterator
type(iter(set())), # setiterator
bytes, bytearray, type(iter(range(10))), memoryview
]
"""a list of types considered built-in (shared between connections)"""

try:
BaseException
except NameError:
pass
else:
_builtin_types.append(BaseException)

if is_py_3k:
_builtin_types.extend([
bytes, bytearray, type(iter(range(10))), memoryview,
])
xrange = range
else:
_builtin_types.extend([
basestring, unicode, long, xrange, type(iter(xrange(10))), file, # noqa
types.InstanceType, types.ClassType, types.DictProxyType,
])
_normalized_builtin_types = {}


Expand Down
15 changes: 5 additions & 10 deletions rpyc/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from threading import Lock, Condition
from rpyc.lib import spawn, Timeout, get_methods, get_id_pack
from rpyc.lib.compat import pickle, next, is_py_3k, maxint, select_error, acquire_lock # noqa: F401
from rpyc.lib.compat import pickle, next, maxint, select_error, acquire_lock # noqa: F401
from rpyc.lib.colls import WeakValueDict, RefCountingColl
from rpyc.core import consts, brine, vinegar, netref
from rpyc.core.async_ import AsyncResult
Expand Down Expand Up @@ -521,15 +521,10 @@ def _check_attr(self, obj, name, perm): # attribute access
raise AttributeError("cannot access %r" % (name,))

def _access_attr(self, obj, name, args, overrider, param, default): # attribute access
if is_py_3k:
if type(name) is bytes:
name = str(name, "utf8")
elif type(name) is not str:
raise TypeError("name must be a string")
else:
if type(name) not in (str, unicode): # noqa
raise TypeError("name must be a string")
name = str(name) # IronPython issue #10 + py3k issue
if type(name) is bytes:
name = str(name, "utf8")
elif type(name) is not str:
raise TypeError("name must be a string")
accessor = getattr(type(obj), overrider, None)
if accessor is None:
accessor = default
Expand Down
7 changes: 3 additions & 4 deletions rpyc/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from functools import partial

from rpyc.lib import hybridmethod
from rpyc.lib.compat import execute, is_py_3k
from rpyc.lib.compat import execute
from rpyc.core.protocol import Connection


Expand Down Expand Up @@ -215,13 +215,12 @@ def on_connect(self, conn):
@staticmethod
def _install(conn, slave):
modules = ModuleNamespace(slave.getmodule)
builtin = modules.builtins if is_py_3k else modules.__builtin__
conn.modules = modules
conn.eval = slave.eval
conn.execute = slave.execute
conn.namespace = slave.namespace
conn.builtin = builtin
conn.builtins = builtin
conn.builtins = modules.builtins
conn.builtin = modules.builtins # TODO: cruft from py2 that requires cleanup elsewhere and CHANGELOG note
from rpyc.utils.classic import teleport_function
conn.teleport = partial(teleport_function, conn)

Expand Down
20 changes: 2 additions & 18 deletions rpyc/core/vinegar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,13 @@
from rpyc.core import brine
from rpyc.core import consts
from rpyc import version
from rpyc.lib.compat import is_py_3k


REMOTE_LINE_START = "\n\n========= Remote Traceback "
REMOTE_LINE_END = " =========\n"
REMOTE_LINE = "{0}({{}}){1}".format(REMOTE_LINE_START, REMOTE_LINE_END)


try:
BaseException
except NameError:
# python 2.4 compatible
BaseException = Exception


def dump(typ, val, tb, include_local_traceback, include_local_version):
"""Dumps the given exceptions info, as returned by ``sys.exc_info()``
Expand Down Expand Up @@ -135,16 +127,8 @@ class simply doesn't exist on the local machine), a :class:`GenericException`
else:
cls = None

if is_py_3k:
if not isinstance(cls, type) or not issubclass(cls, BaseException):
cls = None
else:
if not isinstance(cls, (type, ClassType)):
cls = None
elif issubclass(cls, ClassType) and not instantiate_oldstyle_exceptions:
cls = None
elif not issubclass(cls, BaseException):
cls = None
if not isinstance(cls, type) or not issubclass(cls, BaseException):
cls = None

if cls is None:
fullname = "%s.%s" % (modname, clsname)
Expand Down
2 changes: 1 addition & 1 deletion rpyc/utils/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import os
import inspect
from rpyc.lib.compat import pickle, execute, is_py_3k # noqa: F401
from rpyc.lib.compat import pickle, execute
from rpyc.core.service import ClassicService, Slave
from rpyc.utils import factory
from rpyc.core.service import ModuleNamespace # noqa: F401
Expand Down
72 changes: 16 additions & 56 deletions rpyc/utils/teleportation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,22 @@
import __builtin__
except ImportError:
import builtins as __builtin__ # noqa: F401
from rpyc.lib.compat import is_py_3k, is_py_gte38
from rpyc.lib.compat import is_py_gte38
from types import CodeType, FunctionType
from rpyc.core import brine
from rpyc.core import netref
from rpyc.core import brine, netref
from dis import _unpack_opargs

CODEOBJ_MAGIC = "MAg1c J0hNNzo0hn ZqhuBP17LQk8"


# NOTE: dislike this kind of hacking on the level of implementation details,
# should search for a more reliable/future-proof way:
CODE_HAVEARG_SIZE = 2 if sys.version_info >= (3, 6) else 3
try:
from dis import _unpack_opargs
except ImportError:
# COPIED from 3.5's `dis.py`, this should hopefully be correct for <=3.5:
def _unpack_opargs(code):
extended_arg = 0
n = len(code)
i = 0
while i < n:
op = code[i]
offset = i
i = i + 1
arg = None
if op >= opcode.HAVE_ARGUMENT:
arg = code[i] + code[i + 1] * 256 + extended_arg
extended_arg = 0
i = i + 2
if op == opcode.EXTENDED_ARG:
extended_arg = arg * 65536
yield (offset, op, arg)
CODE_HAVEARG_SIZE = 3


def decode_codeobj(codeobj):
# adapted from dis.dis
if is_py_3k:
codestr = codeobj.co_code
else:
codestr = [ord(ch) for ch in codeobj.co_code]
codestr = codeobj.co_code
free = None
for i, op, oparg in _unpack_opargs(codestr):
opname = opcode.opname[op]
Expand Down Expand Up @@ -81,28 +58,18 @@ def _export_codeobj(cobj):
cobj.co_stacksize, cobj.co_flags, cobj.co_code, tuple(consts2), cobj.co_names, cobj.co_varnames,
cobj.co_filename, cobj.co_name, cobj.co_firstlineno, cobj.co_lnotab, cobj.co_freevars,
cobj.co_cellvars)
elif is_py_3k:
exported = (cobj.co_argcount, cobj.co_kwonlyargcount, cobj.co_nlocals, cobj.co_stacksize, cobj.co_flags,
cobj.co_code, tuple(consts2), cobj.co_names, cobj.co_varnames, cobj.co_filename,
cobj.co_name, cobj.co_firstlineno, cobj.co_lnotab, cobj.co_freevars, cobj.co_cellvars)
else:
exported = (cobj.co_argcount, cobj.co_nlocals, cobj.co_stacksize, cobj.co_flags,
exported = (cobj.co_argcount, cobj.co_kwonlyargcount, cobj.co_nlocals, cobj.co_stacksize, cobj.co_flags,
cobj.co_code, tuple(consts2), cobj.co_names, cobj.co_varnames, cobj.co_filename,
cobj.co_name, cobj.co_firstlineno, cobj.co_lnotab, cobj.co_freevars, cobj.co_cellvars)

assert brine.dumpable(exported)
return (CODEOBJ_MAGIC, exported)


def export_function(func):
if is_py_3k:
func_closure = func.__closure__
func_code = func.__code__
func_defaults = func.__defaults__
else:
func_closure = func.func_closure
func_code = func.func_code
func_defaults = func.func_defaults
func_closure = func.__closure__
func_code = func.__code__
func_defaults = func.__defaults__

if func_closure:
raise TypeError("Cannot export a function closure")
Expand All @@ -113,18 +80,14 @@ def export_function(func):


def _import_codetup(codetup):
if is_py_3k:
# Handle tuples sent from 3.8 as well as 3 < version < 3.8.
if len(codetup) == 16:
(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames,
filename, name, firstlineno, lnotab, freevars, cellvars) = codetup
else:
(argcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames,
filename, name, firstlineno, lnotab, freevars, cellvars) = codetup
posonlyargcount = 0
# Handle tuples sent from 3.8 as well as 3 < version < 3.8.
if len(codetup) == 16:
(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames,
filename, name, firstlineno, lnotab, freevars, cellvars) = codetup
else:
(argcount, nlocals, stacksize, flags, code, consts, names, varnames,
(argcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames,
filename, name, firstlineno, lnotab, freevars, cellvars) = codetup
posonlyargcount = 0

consts2 = []
for const in consts:
Expand All @@ -136,12 +99,9 @@ def _import_codetup(codetup):
if is_py_gte38:
codetup = (argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames,
filename, name, firstlineno, lnotab, freevars, cellvars)
elif is_py_3k:
else:
codetup = (argcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name,
firstlineno, lnotab, freevars, cellvars)
else:
codetup = (argcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno,
lnotab, freevars, cellvars)
return CodeType(*codetup)


Expand Down

0 comments on commit 7afaec2

Please sign in to comment.