Skip to content

Commit

Permalink
Merge branch 'fix-__class_init__'
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Apr 4, 2015
2 parents b096a3a + 762af92 commit 4963a7d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,7 @@ Changelog
4.1.2 (unreleased)
------------------

- TBD
- Fix calling of `__class_init__` hook by Python implementation.

4.1.1 (2015-03-20)
------------------
Expand Down
9 changes: 4 additions & 5 deletions src/ExtensionClass/__init__.py
Expand Up @@ -153,10 +153,6 @@ def __new__(cls, name, bases=(), attrs={}):
any(issubclass(b, _NoInstanceDictionaryBase) for b in bases)):
attrs['__slots__'] = []

# make sure __class_init__ is a class method
if '__class_init__' in attrs:
attrs['__class_init__'] = classmethod(attrs['__class_init__'])

cls = type.__new__(cls, name, bases, attrs)

# Inherit docstring
Expand All @@ -168,7 +164,10 @@ def __new__(cls, name, bases=(), attrs={}):

# call class init method
if hasattr(cls, '__class_init__'):
cls.__class_init__()
class_init = cls.__class_init__
if hasattr(class_init, '__func__'):
class_init = class_init.__func__
class_init(cls)
return cls

def __basicnew__(self):
Expand Down
22 changes: 20 additions & 2 deletions src/ExtensionClass/tests.py
Expand Up @@ -39,9 +39,9 @@ def test_mixing():
... O2.inheritedAttribute('x')(a[0]))
>>> class C(Base):
... def __class_init__(self):
... def __class_init__(cls):
... print('class init called')
... print(self.__name__)
... print(cls.__name__)
... def bar(self):
... return 'bar called'
class init called
Expand Down Expand Up @@ -871,6 +871,24 @@ def test__parent__does_not_get_wrapped():
b
"""

def test_unbound_function_as___class_init___hook():
"""
Zope patches an unbound function as a `__class_init__` hook onto `Persistent`;
let's make sure that gets called correctly.
>>> def InitializeClass(cls):
... print('InitializeClass called')
... print(cls.__name__)
>>> class A(Base):
... pass
>>> A.__class_init__ = InitializeClass
>>> class B(A):
... pass
InitializeClass called
B
"""


from doctest import DocTestSuite
import unittest

Expand Down

0 comments on commit 4963a7d

Please sign in to comment.