Commit 91c8b797 authored by Jason Madden's avatar Jason Madden

Restore python 2.6 and 3.4 compatibility.

parent 0fadb843
...@@ -450,10 +450,10 @@ class _Wrapper(ExtensionClass.Base): ...@@ -450,10 +450,10 @@ class _Wrapper(ExtensionClass.Base):
# a len? # a len?
nonzero = getattr(type_aq_self, '__len__', None) nonzero = getattr(type_aq_self, '__len__', None)
if nonzero: if nonzero:
return _rebound_method(nonzero, self)() return bool(nonzero(self)) # Py3 is strict about the return type
# If nothing was defined, then it's true # If nothing was defined, then it's true
return True return True
__bool__ = __nonzero__
def __unicode__(self): def __unicode__(self):
f = getattr(self.aq_self, '__unicode__', f = getattr(self.aq_self, '__unicode__',
...@@ -541,9 +541,9 @@ class _Wrapper(ExtensionClass.Base): ...@@ -541,9 +541,9 @@ class _Wrapper(ExtensionClass.Base):
'__oct__', '__oct__',
'__hex__', '__hex__',
'__index__', '__index__',
'__len__', #'__len__',
# strings # strings are special
#'__repr__', #'__repr__',
#'__str__', #'__str__',
] ]
...@@ -569,6 +569,17 @@ class _Wrapper(ExtensionClass.Base): ...@@ -569,6 +569,17 @@ class _Wrapper(ExtensionClass.Base):
# Container protocol # Container protocol
def __len__(self):
# if len is missing, it should raise TypeError
# (AttributeError is acceptable under Py2, but Py3
# breaks list conversion if AttributeError is raised)
try:
l = getattr(type(self._obj), '__len__')
except AttributeError:
raise TypeError('object has no len()')
else:
return l(self)
def __iter__(self): def __iter__(self):
# For things that provide either __iter__ or just __getitem__, # For things that provide either __iter__ or just __getitem__,
# we need to be sure that the wrapper is provided as self # we need to be sure that the wrapper is provided as self
...@@ -598,7 +609,7 @@ class _Wrapper(ExtensionClass.Base): ...@@ -598,7 +609,7 @@ class _Wrapper(ExtensionClass.Base):
aq_self = self._obj aq_self = self._obj
aq_contains = getattr(type(aq_self), '__contains__', None) aq_contains = getattr(type(aq_self), '__contains__', None)
if aq_contains: if aq_contains:
return _rebound_method(aq_contains, self)(item) return aq_contains(self, item)
# Next, we should attempt to iterate like the interpreter; but the C code doesn't # Next, we should attempt to iterate like the interpreter; but the C code doesn't
# do this, so we don't either. # do this, so we don't either.
#return item in iter(self) #return item in iter(self)
......
...@@ -348,6 +348,7 @@ if sys.version_info >= (3,): ...@@ -348,6 +348,7 @@ if sys.version_info >= (3,):
except AttributeError as e: except AttributeError as e:
return type(self).__str__(self) return type(self).__str__(self)
long = int
else: else:
PY2 = True PY2 = True
PY3 = False PY3 = False
...@@ -2760,21 +2761,25 @@ class TestProxying(unittest.TestCase): ...@@ -2760,21 +2761,25 @@ class TestProxying(unittest.TestCase):
def unary_acquired_func(self): def unary_acquired_func(self):
return self.value return self.value
acquire_meths = {k: binary_acquired_func acquire_meths = {}
for k in self.__binary_numeric_methods__} for k in self.__binary_numeric_methods__:
acquire_meths[k] = binary_acquired_func
for k in self.__unary_special_methods__:
acquire_meths[k] = unary_acquired_func
acquire_meths.update( {k: unary_acquired_func
for k in self.__unary_special_methods__} )
def make_converter(f): def make_converter(f):
def converter(self,*args): def converter(self,*args):
return f(self.value) return f(self.value)
return converter return converter
acquire_meths.update( {k: make_converter(convert) for k, convert in self.__unary_conversion_methods__.items():
for k, convert acquire_meths[k] = make_converter(convert)
in self.__unary_conversion_methods__.items()})
acquire_meths['__len__'] = lambda self: self.value acquire_meths['__len__'] = lambda self: self.value
if PY3:
# Under Python 3, oct() and hex() call __index__ directly
acquire_meths['__index__'] = acquire_meths['__int__']
if base_class == Acquisition.Explicit: if base_class == Acquisition.Explicit:
acquire_meths['value'] = Acquisition.Acquired acquire_meths['value'] = Acquisition.Acquired
AcquireValue = type('AcquireValue', (base_class,), acquire_meths) AcquireValue = type('AcquireValue', (base_class,), acquire_meths)
...@@ -2827,6 +2832,7 @@ class TestProxying(unittest.TestCase): ...@@ -2827,6 +2832,7 @@ class TestProxying(unittest.TestCase):
continue continue
self.assertEqual(converter(base.value), self.assertEqual(converter(base.value),
getattr(base.derived, meth)()) getattr(base.derived, meth)())
self.assertEqual(converter(base.value), self.assertEqual(converter(base.value),
converter(base.derived)) converter(base.derived))
...@@ -2992,6 +2998,7 @@ class TestProxying(unittest.TestCase): ...@@ -2992,6 +2998,7 @@ class TestProxying(unittest.TestCase):
def __nonzero__(self): def __nonzero__(self):
return bool(self.value) return bool(self.value)
__bool__ = __nonzero__
class WithLen(base_class): class WithLen(base_class):
if base_class is Acquisition.Explicit: if base_class is Acquisition.Explicit:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment