Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cythonize Dispatcher methods #24

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.pyc
*.swp
*.so
*.html
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ python:
- "3.3"
- "pypy"

env:
- USE_CYTHON=True
- USE_CYTHON=False

install:
- pip install coverage --use-mirrors

- if [[ $USE_CYTHON == 'True' ]]; then
- pip install cython --use-mirrors
- python setup.py build_ext --inplace --with-cython
- fi
script:
- nosetests --with-doctest
- if [[ $TRAVIS_PYTHON_VERSION == 'pypy' ]]; then USE_CYTHON=False ; fi
- python -c 'import multipledispatch ; assert multipledispatch.dispatcher.USE_CYTHON == '$USE_CYTHON

after_success:
- if [[ $TRAVIS_PYTHON_VERSION != 'pypy' ]]; then pip install coveralls --use-mirrors ; coveralls ; fi
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PYTHON ?= python

inplace:
$(PYTHON) setup.py build_ext --inplace --with-cython

test: inplace
nosetests -s --with-doctest multipledispatch/
10 changes: 10 additions & 0 deletions multipledispatch/_dispatcher.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cdef class DispatcherBase:
cdef public object name
cdef public object funcs
cdef public object ordering
cdef public object _cache


cdef class MethodDispatcherBase(DispatcherBase):
cdef public object obj
cdef public object cls
108 changes: 108 additions & 0 deletions multipledispatch/_dispatcher.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from .conflict import ordering
from .dispatcher import str_signature

from cpython.dict cimport PyDict_GetItem, PyDict_SetItem
from cpython.object cimport PyObject_Call
from cpython.ref cimport PyObject, Py_INCREF
from cpython.tuple cimport (PyTuple_GET_ITEM, PyTuple_GET_SIZE, PyTuple_New,
PyTuple_SET_ITEM)


cdef class DispatcherBase:
def __call__(self, *args, **kwargs):
cdef PyObject *obj
cdef object val
cdef Py_ssize_t i, N = PyTuple_GET_SIZE(args)
if N == 1:
types = type(<object>PyTuple_GET_ITEM(args, 0))
else:
types = PyTuple_New(N)
for i in range(N):
val = type(<object>PyTuple_GET_ITEM(args, i))
Py_INCREF(val)
PyTuple_SET_ITEM(types, i, val)

obj = PyDict_GetItem(self._cache, types)
if obj is not NULL:
return PyObject_Call(<object>obj, args, kwargs)
elif N == 1:
# *Always* pass a tuple to `self.resolve`
val = self.resolve((types,))
else:
val = self.resolve(types)
PyDict_SetItem(self._cache, types, val)
return PyObject_Call(val, args, kwargs)

def resolve(self, types):
""" Deterimine appropriate implementation for this type signature

This method is internal. Users should call this object as a function.
Implementation resolution occurs within the ``__call__`` method.

>>> from multipledispatch import dispatch
>>> @dispatch(int)
... def inc(x):
... return x + 1

>>> implementation = inc.resolve((int,))
>>> implementation(3)
4

>>> inc.resolve((float,))
Traceback (most recent call last):
...
NotImplementedError: Could not find signature for inc: <float>

See Also:
``multipledispatch.conflict`` - module to determine resolution order
"""
cdef PyObject *obj = PyDict_GetItem(self.funcs, types)
if obj is not NULL:
return <object>obj

n = len(types)
for signature in self.ordering:
if len(signature) == n and all(map(issubclass, types, signature)):
result = self.funcs[signature]
return result
raise NotImplementedError('Could not find signature for %s: <%s>' %
(self.name, str_signature(types)))

def __reduce__(self):
return (type(self), (self.name,), self.funcs)

def __setstate__(self, state):
self.funcs = state
self._cache = {}
self.ordering = ordering(self.funcs)


cdef class MethodDispatcherBase(DispatcherBase):
def __get__(self, instance, owner):
self.obj = instance
self.cls = owner
return self

def __call__(self, *args, **kwargs):
cdef PyObject *obj
cdef object val
cdef Py_ssize_t i, N = PyTuple_GET_SIZE(args)
types = PyTuple_New(N) # = tuple([type(arg) for arg in args])
selfargs = PyTuple_New(N + 1) # = (self.obj,) + args
Py_INCREF(self.obj)
PyTuple_SET_ITEM(selfargs, 0, self.obj)
for i in range(N):
val = <object>PyTuple_GET_ITEM(args, i)
Py_INCREF(val)
PyTuple_SET_ITEM(selfargs, i + 1, val)
val = type(val)
Py_INCREF(val)
PyTuple_SET_ITEM(types, i, val)

obj = PyDict_GetItem(self._cache, types)
if obj is not NULL:
return PyObject_Call(<object>obj, selfargs, kwargs)
else:
val = self.resolve(types)
PyDict_SetItem(self._cache, types, val)
return PyObject_Call(val, selfargs, kwargs)
174 changes: 99 additions & 75 deletions multipledispatch/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,101 @@
from .utils import expand_tuples


def str_signature(sig):
""" String representation of type signature

>>> str_signature((int, float))
'int, float'
"""
return ', '.join(cls.__name__ for cls in sig)


try:
import platform
USE_CYTHON = platform.python_implementation() == 'CPython'
if USE_CYTHON:
from ._dispatcher import DispatcherBase, MethodDispatcherBase
except ImportError:
USE_CYTHON = False


if not USE_CYTHON:
class DispatcherBase(object):
__slots__ = 'name', 'funcs', 'ordering', '_cache'

def __call__(self, *args, **kwargs):
types = tuple([type(arg) for arg in args])
try:
func = self._cache[types]
except KeyError:
func = self.resolve(types)
self._cache[types] = func
return func(*args, **kwargs)

def resolve(self, types):
""" Deterimine appropriate implementation for this type signature

This method is internal. Users should call this object as a function.
Implementation resolution occurs within the ``__call__`` method.

>>> from multipledispatch import dispatch
>>> @dispatch(int)
... def inc(x):
... return x + 1

>>> implementation = inc.resolve((int,))
>>> implementation(3)
4

>>> inc.resolve((float,))
Traceback (most recent call last):
...
NotImplementedError: Could not find signature for inc: <float>

See Also:
``multipledispatch.conflict`` - module to determine resolution order
"""

if types in self.funcs:
return self.funcs[types]

n = len(types)
for signature in self.ordering:
if len(signature) == n and all(map(issubclass, types, signature)):
result = self.funcs[signature]
return result
raise NotImplementedError('Could not find signature for %s: <%s>' %
(self.name, str_signature(types)))

def __getstate__(self):
return {'name': self.name,
'funcs': self.funcs}

def __setstate__(self, d):
self.name = d['name']
self.funcs = d['funcs']
self.ordering = ordering(self.funcs)
self._cache = dict()


class MethodDispatcherBase(DispatcherBase):
__slots__ = 'obj', 'cls'

def __get__(self, instance, owner):
self.obj = instance
self.cls = owner
return self

def __call__(self, *args, **kwargs):
types = tuple([type(arg) for arg in args])
try:
func = self._cache[types]
except KeyError:
func = self.resolve(types)
self._cache[types] = func
return func(self.obj, *args, **kwargs)


def ambiguity_warn(dispatcher, ambiguities):
""" Raise warning when ambiguity is detected

Expand All @@ -20,7 +115,7 @@ def ambiguity_warn(dispatcher, ambiguities):
warn(warning_text(dispatcher.name, ambiguities), AmbiguityWarning)


class Dispatcher(object):
class Dispatcher(DispatcherBase):
""" Dispatch methods based on type signature

Use ``dispatch`` to add implementations
Expand All @@ -42,7 +137,7 @@ class Dispatcher(object):
>>> f(3.0)
2.0
"""
__slots__ = 'name', 'funcs', 'ordering', '_cache'
__slots__ = ()

def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -112,65 +207,10 @@ def add(self, signature, func, on_ambiguity=ambiguity_warn):
on_ambiguity(self, amb)
self._cache.clear()

def __call__(self, *args, **kwargs):
types = tuple([type(arg) for arg in args])
try:
func = self._cache[types]
except KeyError:
func = self.resolve(types)
self._cache[types] = func
return func(*args, **kwargs)

def __str__(self):
return "<dispatched %s>" % self.name
__repr__ = __str__

def resolve(self, types):
""" Deterimine appropriate implementation for this type signature

This method is internal. Users should call this object as a function.
Implementation resolution occurs within the ``__call__`` method.

>>> from multipledispatch import dispatch
>>> @dispatch(int)
... def inc(x):
... return x + 1

>>> implementation = inc.resolve((int,))
>>> implementation(3)
4

>>> inc.resolve((float,))
Traceback (most recent call last):
...
NotImplementedError: Could not find signature for inc: <float>

See Also:
``multipledispatch.conflict`` - module to determine resolution order
"""

if types in self.funcs:
return self.funcs[types]

n = len(types)
for signature in self.ordering:
if len(signature) == n and all(map(issubclass, types, signature)):
result = self.funcs[signature]
return result
raise NotImplementedError('Could not find signature for %s: <%s>' %
(self.name, str_signature(types)))

def __getstate__(self):
return {'name': self.name,
'funcs': self.funcs}

def __setstate__(self, d):
self.name = d['name']
self.funcs = d['funcs']
self.ordering = ordering(self.funcs)
self._cache = dict()


@property
def __doc__(self):
doc = " Multiply dispatched method: %s\n\n" % self.name
Expand All @@ -196,30 +236,14 @@ def __doc__(self):
return doc


class MethodDispatcher(Dispatcher):
class MethodDispatcher(MethodDispatcherBase, Dispatcher):
""" Dispatch methods based on type signature

See Also:
Dispatcher
"""
def __get__(self, instance, owner):
self.obj = instance
self.cls = owner
return self

def __call__(self, *args, **kwargs):
types = tuple([type(arg) for arg in args])
func = self.resolve(types)
return func(self.obj, *args, **kwargs)


def str_signature(sig):
""" String representation of type signature
__slots__ = ()

>>> str_signature((int, float))
'int, float'
"""
return ', '.join(cls.__name__ for cls in sig)

def warning_text(name, amb):
""" The text for ambiguity warnings """
Expand Down
Loading