Skip to content
This repository has been archived by the owner on Oct 16, 2021. It is now read-only.

Commit

Permalink
streamlined naming for cpy2py.proxy (see issue #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfischer2781 committed Apr 19, 2018
1 parent 480deaa commit 324347f
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cpy2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

from cpy2py.meta import __version__
from cpy2py.utility.compat import NullHandler as _NullHandler
from cpy2py.proxy.proxy_object import TwinObject, localmethod
from cpy2py.proxy.proxy_func import twinfunction
from cpy2py.proxy.baseclass import TwinObject, localmethod
from cpy2py.proxy.function import twinfunction
from cpy2py.twinterpreter.master import TwinMaster
from cpy2py.kernel import kernel_state

Expand Down
6 changes: 3 additions & 3 deletions cpy2py/kernel/kernel_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
from cpy2py.utility.exceptions import format_exception
from cpy2py.ipyc import ipyc_exceptions
from cpy2py.kernel.kernel_exceptions import StopTwinterpreter
from cpy2py.proxy import proxy_tracker
from cpy2py.proxy import tracker
from cpy2py.kernel.kernel_requesthandler import RequestDispatcher, RequestHandler


def _connect_ipyc(ipyc, pickle_protocol):
"""Connect pickle/unpickle trackers to a duplex IPyC"""
pickler = proxy_tracker.twin_pickler(ipyc.writer, pickle_protocol)
pickler = tracker.twin_pickler(ipyc.writer, pickle_protocol)
send = pickler.dump
unpickler = proxy_tracker.twin_unpickler(ipyc.reader)
unpickler = tracker.twin_unpickler(ipyc.reader)
recv = unpickler.load
return send, recv

Expand Down
12 changes: 6 additions & 6 deletions cpy2py/proxy/proxy_object.py → cpy2py/proxy/baseclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# - # limitations under the License.
import time

from cpy2py.proxy import proxy_tracker
from cpy2py.proxy.proxy_meta import TwinMeta
from cpy2py.proxy.proxy_twin import TwinProxy
from cpy2py.proxy import tracker
from cpy2py.proxy.metaclass import TwinMeta
from cpy2py.proxy.proxy import InstanceProxy


def instance_id(instance):
Expand All @@ -41,7 +41,7 @@ def new_twin_object(cls, *args, **kwargs): # pylint: disable=unused-argument
self = object.__new__(cls)
object.__setattr__(self, '__instance_id__', instance_id(self))
# register our reference for lookup
proxy_tracker.__active_instances__[self.__twin_id__, self.__instance_id__] = self
tracker.__active_instances__[self.__twin_id__, self.__instance_id__] = self
return self

# calling TwinMeta to set metaclass explicitly works for py2 and py3
Expand Down Expand Up @@ -76,6 +76,6 @@ def localmethod(method):
setattr(method, TwinMeta.mark_localmember, True)
return method

# TwinObject and TwinProxy are not created by metaclass, initialize manually
# TwinObject and InstanceProxy are not created by metaclass, initialize manually
TwinObject.__import_mod_name__ = (TwinObject.__module__, TwinObject.__name__)
TwinProxy.__import_mod_name__ = TwinObject.__import_mod_name__
InstanceProxy.__import_mod_name__ = TwinObject.__import_mod_name__
File renamed without changes.
14 changes: 7 additions & 7 deletions cpy2py/proxy/proxy_meta.py → cpy2py/proxy/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import types
from cpy2py.kernel import kernel_state

from cpy2py.proxy.proxy_twin import TwinProxy, ProxyMethod
from cpy2py.proxy.proxy import InstanceProxy, UnboundedMethodProxy


class TwinMeta(type):
Expand Down Expand Up @@ -103,13 +103,13 @@ def __new_proxy_class__(mcs, name, bases, class_dict):
del proxy_dict[aname]
# methods must be proxy'd
elif isinstance(proxy_dict[aname], types.FunctionType):
proxy_dict[aname] = ProxyMethod(proxy_dict[aname])
proxy_dict[aname] = UnboundedMethodProxy(proxy_dict[aname])
elif isinstance(proxy_dict[aname], (classmethod, staticmethod)):
try:
real_func = proxy_dict[aname].__func__
except AttributeError: # py2.6
real_func = proxy_dict[aname].__get__(None, object)
proxy_dict[aname] = ProxyMethod(real_func)
proxy_dict[aname] = UnboundedMethodProxy(real_func)
# remove non-magic attributes so they don't shadow the real ones
elif aname not in mcs.__proxy_inherits_attributes__:
del proxy_dict[aname]
Expand Down Expand Up @@ -141,12 +141,12 @@ def __get_real_class(mcs, unknown_class):
@classmethod
def register_proxy(mcs, real_class, proxy_class):
"""
Register a class acting as :py:class:`~.TwinProxy` for a real class
Register a class acting as :py:class:`~.InstanceProxy` for a real class
:param real_class: a normal, non-cpy2py class
:type real_class: object or :py:class:`~cpy2py.proxy.proxy_object.TwinObject`
:param proxy_class: a proxy class similar to :py:class:`~.TwinProxy`
:type proxy_class: object or :py:class:`~.TwinProxy`
:param proxy_class: a proxy class similar to :py:class:`~.InstanceProxy`
:type proxy_class: object or :py:class:`~.InstanceProxy`
"""
# TODO: weakref any of these? - MF@20160401
mcs.__real_class_store__[proxy_class] = real_class
Expand Down Expand Up @@ -175,4 +175,4 @@ def __delattr__(cls, name):
type.__setattr__(cls, name)


TwinMeta.register_proxy(object, TwinProxy)
TwinMeta.register_proxy(object, InstanceProxy)
20 changes: 12 additions & 8 deletions cpy2py/proxy/proxy_twin.py → cpy2py/proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
# - # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# - # See the License for the specific language governing permissions and
# - # limitations under the License.
"""
Proxies for entities in other twinterpreters
"""
from cpy2py.kernel.kernel_exceptions import TwinterpeterUnavailable
from cpy2py.kernel import kernel_state

from cpy2py.proxy import proxy_tracker
from cpy2py.proxy import tracker


class ProxyMethod(object):
class UnboundedMethodProxy(object):
"""
Proxy for Methods
Proxy for unbounded methods
:param real_method: the method object to be proxied
"""
:param real_method: the unbounded method object to be proxied
:note: In Python3 terms, an unbounded method is simply a function bound to a class.
"""
def __init__(self, real_method):
self.__wrapped__ = real_method
for attribute in ('__doc__', '__defaults__', '__name__', '__module__'):
Expand All @@ -48,9 +52,9 @@ def __get__(self, instance, owner):
)


class TwinProxy(object):
class InstanceProxy(object):
"""
Proxy for instances existing in another twinterpreter
Proxy for instances of classes
:see: Real object :py:class:`~cpy2py.proxy.proxy_object.TwinObject` for magic attributes.
Expand Down Expand Up @@ -81,7 +85,7 @@ def __new__(cls, *args, **kwargs):
object.__setattr__(self, '__instance_id__', __instance_id__)
__kernel__.increment_instance_ref(self)
# store for later use without requiring explicit lookup/converter calls
proxy_tracker.__active_instances__[self.__twin_id__, self.__instance_id__] = self
tracker.__active_instances__[self.__twin_id__, self.__instance_id__] = self
return self

def __repr__(self):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cpy2py/twinterpreter/group_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# - # See the License for the specific language governing permissions and
# - # limitations under the License.
from __future__ import print_function
from cpy2py.proxy.proxy_object import TwinObject, localmethod
from cpy2py.proxy.baseclass import TwinObject, localmethod
from cpy2py.kernel import kernel_state
import threading

Expand Down
4 changes: 2 additions & 2 deletions cpy2py_unittests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time

from cpy2py import TwinMaster, TwinObject
import cpy2py.proxy.proxy_twin # proxy type check
import cpy2py.proxy.proxy # proxy type check


class PrimitiveObject(TwinObject):
Expand Down Expand Up @@ -36,7 +36,7 @@ def tearDown(self):

def test_init(self):
instance = PrimitiveObject()
self.assertTrue(isinstance(instance, cpy2py.proxy.proxy_twin.TwinProxy))
self.assertTrue(isinstance(instance, cpy2py.proxy.proxy.InstanceProxy))

def test_class_attribute(self):
instance = PrimitiveObject()
Expand Down
6 changes: 3 additions & 3 deletions cpy2py_unittests/test_proxy/test_proxy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import time

from cpy2py import kernel_state, TwinMaster
from cpy2py.proxy import proxy_func
from cpy2py.proxy import function


@proxy_func.twinfunction(kernel_state.TWIN_ID)
@function.twinfunction(kernel_state.TWIN_ID)
def native_func(arg):
"""docstring"""
return arg


@proxy_func.twinfunction('pypy')
@function.twinfunction('pypy')
def proxied_func(arg):
"""docstring"""
return arg
Expand Down
2 changes: 1 addition & 1 deletion cpy2py_unittests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time

from cpy2py import kernel_state, TwinMaster, TwinObject
from cpy2py.proxy.proxy_object import localmethod
from cpy2py.proxy.baseclass import localmethod


def test_kernel(kernel_id):
Expand Down

0 comments on commit 324347f

Please sign in to comment.