Skip to content

Commit

Permalink
Lint the code.
Browse files Browse the repository at this point in the history
* Add support for Python 3.9.
  • Loading branch information
Michael Howitz committed Jul 15, 2021
1 parent 6c6e6dc commit 930cde8
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 108 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
4.3.6 (unreleased)
==================

- Nothing changed yet.
- Add support for Python 3.9.

- Create aarch64 wheels.


4.3.5 (2020-03-16)
Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class optional_build_ext(build_ext):
"""This class subclasses build_ext and allows
the building of C extensions to fail.
"""

def run(self):
try:
build_ext.run(self)
Expand Down Expand Up @@ -103,6 +104,7 @@ def read(*rnames):
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Framework :: Zope :: 3',
Expand Down Expand Up @@ -130,8 +132,9 @@ def read(*rnames):
'zope.testrunner',
],
'docs': [
'Sphinx<4', # Until repoze.sphinx.autointerface supports Sphinx 4.x we cannot use it
# Need < 4 until repoze.sphinx.autointerface supports Sphinx 4:
'Sphinx < 4',
'repoze.sphinx.autointerface',
],
},
)
)
78 changes: 49 additions & 29 deletions src/zope/proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import operator
import os
import pickle
import sys

from zope.interface import moduleProvides
from zope.proxy.interfaces import IProxyIntrospection

moduleProvides(IProxyIntrospection)
__all__ = tuple(IProxyIntrospection)


def ProxyIterator(p):
yield p
while isProxy(p):
Expand All @@ -33,6 +33,7 @@ def ProxyIterator(p):

_MARKER = object()


def _WrapperType_Lookup(type_, name):
"""
Looks up information in class dictionaries in MRO
Expand All @@ -49,12 +50,14 @@ def _WrapperType_Lookup(type_, name):
return res
return _MARKER


def _get_wrapped(self):
"""
Helper method to access the wrapped object.
"""
return super(AbstractPyProxyBase, self).__getattribute__('_wrapped')


class _EmptyInterfaceDescriptor(object):
"""A descriptor for the attributes used on the class by the
Python implementation of `zope.interface`.
Expand All @@ -79,11 +82,13 @@ def __next__(self):
raise StopIteration()
next = __next__


class _ProxyMetaclass(type):
# The metaclass is applied after the class definition
# for Py2/Py3 compatibility.
__implemented__ = _EmptyInterfaceDescriptor()


class AbstractPyProxyBase(object):
"""
A reference implementation that cannot be instantiated. Most users
Expand Down Expand Up @@ -116,10 +121,10 @@ def __repr__(self):
def __str__(self):
return str(self._wrapped)

def __unicode__(self):
return unicode(self._wrapped)
def __unicode__(self): # pragma: no cover PY2
return unicode(self._wrapped) # noqa: F821 undefined name

def __reduce__(self): # pragma: no cover (__reduce_ex__ prevents normal)
def __reduce__(self): # pragma: no cover (__reduce_ex__ prevents normal)
raise pickle.PicklingError

def __reduce_ex__(self, proto):
Expand All @@ -146,7 +151,7 @@ def __ge__(self, other):

def __nonzero__(self):
return bool(self._wrapped)
__bool__ = __nonzero__ # Python3 compat
__bool__ = __nonzero__ # Python3 compat

def __hash__(self):
return hash(self._wrapped)
Expand Down Expand Up @@ -240,18 +245,19 @@ def __delitem__(self, key):
del self._wrapped[key]

def __iter__(self):
# This handles a custom __iter__ and generator support at the same time.
# This handles a custom __iter__ and generator support at the same
# time.
return iter(self._wrapped)

def next(self):
def next(self): # pragma: no cover PY2
# Called when we wrap an iterator itself.
return self._wrapped.next()

def __next__(self): # pragma: no cover Python3
def __next__(self):
return self._wrapped.__next__()

# Python 2.7 won't let the C wrapper support __reversed__ :(
#def __reversed__(self):
# def __reversed__(self):
# return reversed(self._wrapped)

def __contains__(self, item):
Expand All @@ -277,8 +283,8 @@ def __complex__(self):
def __int__(self):
return int(self._wrapped)

def __long__(self):
return long(self._wrapped)
def __long__(self): # pragma: no cover PY2
return long(self._wrapped) # noqa: F821 undefined name

def __float__(self):
return float(self._wrapped)
Expand All @@ -293,9 +299,10 @@ def __index__(self):
return operator.index(self._wrapped)

# Numeric protocol: binary coercion
def __coerce__(self, other):
left, right = coerce(self._wrapped, other)
if left == self._wrapped and type(left) is type(self._wrapped):
def __coerce__(self, other): # pragma: no cover PY2
left, right = coerce(self._wrapped, other) # noqa: F821 undefined name
if (left == self._wrapped
and type(left) is type(self._wrapped)): # noqa: E721
left = self
return left, right

Expand All @@ -312,11 +319,11 @@ def __mul__(self, other):
def __floordiv__(self, other):
return self._wrapped // other

def __truediv__(self, other): # pragma: no cover
def __truediv__(self, other): # pragma: no cover
# Only one of __truediv__ and __div__ is meaningful at any one time.
return self._wrapped / other

def __div__(self, other): # pragma: no cover
def __div__(self, other): # pragma: no cover
# Only one of __truediv__ and __div__ is meaningful at any one time.
return self._wrapped / other

Expand All @@ -343,11 +350,11 @@ def __rmul__(self, other):
def __rfloordiv__(self, other):
return other // self._wrapped

def __rtruediv__(self, other): # pragma: no cover
def __rtruediv__(self, other): # pragma: no cover
# Only one of __rtruediv__ and __rdiv__ is meaningful at any one time.
return other / self._wrapped

def __rdiv__(self, other): # pragma: no cover
def __rdiv__(self, other): # pragma: no cover
# Only one of __rtruediv__ and __rdiv__ is meaningful at any one time.
return other / self._wrapped

Expand All @@ -361,7 +368,7 @@ def __rpow__(self, other, modulus=None):
if modulus is None:
return pow(other, self._wrapped)
# We can't actually get here, because we can't lie about our type()
return pow(other, self._wrapped, modulus) # pragma: no cover
return pow(other, self._wrapped, modulus) # pragma: no cover

# Numeric protocol: binary bitwise operators
def __lshift__(self, other):
Expand Down Expand Up @@ -407,12 +414,12 @@ def __imul__(self, other):
self._wrapped *= other
return self

def __idiv__(self, other): # pragma: no cover
def __idiv__(self, other): # pragma: no cover
# Only one of __itruediv__ and __idiv__ is meaningful at any one time.
self._wrapped /= other
return self

def __itruediv__(self, other): # pragma: no cover
def __itruediv__(self, other): # pragma: no cover
# Only one of __itruediv__ and __idiv__ is meaningful at any one time.
self._wrapped /= other
return self
Expand Down Expand Up @@ -448,14 +455,16 @@ def __ior__(self, other):
def __ipow__(self, other, modulus=None):
if modulus is None:
self._wrapped **= other
else: # pragma: no cover
else: # pragma: no cover
# There is no syntax which triggers in-place pow w/ modulus
self._wrapped = pow(self._wrapped, other, modulus)
return self


AbstractPyProxyBase = _ProxyMetaclass(str('AbstractPyProxyBase'), (),
dict(AbstractPyProxyBase.__dict__))


class PyProxyBase(AbstractPyProxyBase):
"""Reference implementation.
"""
Expand All @@ -467,24 +476,28 @@ def py_getProxiedObject(obj):
return obj._wrapped
return obj


def py_setProxiedObject(obj, new_value):
if not isinstance(obj, PyProxyBase):
raise TypeError('Not a proxy')
old, obj._wrapped = obj._wrapped, new_value
return old


def py_isProxy(obj, klass=None):
if klass is None:
klass = PyProxyBase
return isinstance(obj, klass)


def py_sameProxiedObjects(lhs, rhs):
while isinstance(lhs, PyProxyBase):
lhs = super(PyProxyBase, lhs).__getattribute__('_wrapped')
while isinstance(rhs, PyProxyBase):
rhs = super(PyProxyBase, rhs).__getattribute__('_wrapped')
return lhs is rhs


def py_queryProxy(obj, klass=None, default=None):
if klass is None:
klass = PyProxyBase
Expand All @@ -494,36 +507,42 @@ def py_queryProxy(obj, klass=None, default=None):
return obj
return default


def py_queryInnerProxy(obj, klass=None, default=None):
if klass is None:
klass = PyProxyBase
found = []
while obj is not None:
if isinstance(obj, klass):
found.append(obj) # stack
found.append(obj) # stack
obj = getattr(obj, '_wrapped', None)
if found:
return found[-1]
return default


def py_removeAllProxies(obj):
while isinstance(obj, PyProxyBase):
obj = super(PyProxyBase, obj).__getattribute__('_wrapped')
return obj


_c_available = False
if 'PURE_PYTHON' not in os.environ:
try:
try: # pragma: no cover
from zope.proxy._zope_proxy_proxy import ProxyBase as _c_available
except ImportError: # pragma: no cover
except ImportError:
pass


class PyNonOverridable(object):
"Deprecated, only for BWC."
def __init__(self, method_desc): # pragma: no cover PyPy

def __init__(self, method_desc): # pragma: no cover PyPy
self.desc = method_desc

if _c_available:

if _c_available: # pragma: no cover
# Python API: not used in this module
from zope.proxy._zope_proxy_proxy import ProxyBase
from zope.proxy._zope_proxy_proxy import getProxiedObject
Expand All @@ -535,9 +554,9 @@ def __init__(self, method_desc): # pragma: no cover PyPy
from zope.proxy._zope_proxy_proxy import removeAllProxies

# API for proxy-using C extensions.
from zope.proxy._zope_proxy_proxy import _CAPI
from zope.proxy._zope_proxy_proxy import _CAPI # noqa: F401 unused

else: # pragma: no cover
else:
# no C extension available, fall back
ProxyBase = PyProxyBase
getProxiedObject = py_getProxiedObject
Expand All @@ -548,5 +567,6 @@ def __init__(self, method_desc): # pragma: no cover PyPy
queryInnerProxy = py_queryInnerProxy
removeAllProxies = py_removeAllProxies


def non_overridable(func):
return property(lambda self: func.__get__(self))
3 changes: 2 additions & 1 deletion src/zope/proxy/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
from zope.interface.declarations import ObjectSpecification
from zope.interface import providedBy


class DecoratorSpecificationDescriptor(ObjectSpecificationDescriptor):
"""Support for interface declarations on decorators
"""

def __get__(self, inst, cls=None):
if inst is None:
return getObjectSpecification(cls)
Expand All @@ -46,4 +48,3 @@ class SpecificationDecoratorBase(ProxyBase):
"""Base class for a proxy that provides additional interfaces."""

__providedBy__ = DecoratorSpecificationDescriptor()

1 change: 1 addition & 0 deletions src/zope/proxy/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from zope.interface import Interface


class IProxyIntrospection(Interface):
"""Provides methods for indentifying proxies and extracting proxied objects
"""
Expand Down
Loading

0 comments on commit 930cde8

Please sign in to comment.