Skip to content

Commit

Permalink
Merge pull request #81 from zopefoundation/patch-interface-213
Browse files Browse the repository at this point in the history
Patch zope.interface to remove docstrings and avoid publishing.
  • Loading branch information
mauritsvanrees committed Dec 27, 2016
2 parents fd844dc + 4ffa049 commit 1190844
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ http://docs.zope.org/zope2/
2.13.25 (unreleased)
--------------------

- Patch zope.interface to remove docstrings and avoid publishing.
From Products.PloneHotfix20161129. [maurits]

- Don't copy items the user is not allowed to view.
From Products.PloneHotfix20161129. [maurits]

Expand Down
14 changes: 14 additions & 0 deletions src/Zope2/App/patches/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Import all patches.
import publishing

# Have the patches been applied yet?
_patched = False


def apply_patches():
global _patched
if _patched:
return
_patched = True

publishing.apply_patches()
69 changes: 69 additions & 0 deletions src/Zope2/App/patches/publishing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-


def delete_method_docstring(klass, method_name):
# Delete the docstring from the class method.
# Objects must have a docstring to be published.
# So this avoids them getting published.
method = getattr(klass, method_name, None)
if (method is not None and hasattr(method, 'im_func') and
hasattr(method.im_func, '__doc__')):
del method.im_func.__doc__


element_methods = [
'getDoc',
'getName',
'getTaggedValue',
'getTaggedValueTags',
'queryTaggedValue',
'setTaggedValue',
]
interface_methods = [
'changed',
'dependents',
'direct',
'extends',
'get',
'getBases',
'getDescriptionFor',
'implementedBy',
'interfaces',
'isEqualOrExtendedBy',
'isOrExtends',
'names',
'namesAndDescriptions',
'providedBy',
'queryDescriptionFor',
'subscribe',
'unsubscribe',
'validateInvariants',
'weakref',
]


# Has this patch been applied yet?
_patched = False


def apply_patches():
global _patched
if _patched:
return
_patched = True

from zope.interface import Attribute
from zope.interface import Interface
from zope.interface.interface import Element
from zope.interface.interface import Method

for klass in [Element, Attribute, Interface, Method]:
try:
del klass.__doc__
except:
pass
for method_name in element_methods:
delete_method_docstring(klass, method_name)

for method_name in interface_methods:
delete_method_docstring(Interface, method_name)
2 changes: 2 additions & 0 deletions src/Zope2/App/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def startup():
from App.PersistentExtra import patchPersistent
import Globals # to set / fetch data
patchPersistent()
from Zope2.App import patches
patches.apply_patches()

global app

Expand Down
9 changes: 9 additions & 0 deletions src/Zope2/App/tests/test_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def test_database_events(self):
startup()
self.assertEqual(str(handler), logged)

def test_interface(self):
# We don't want Interface methods to be publishable.
# So they should not have a docstring.
# This is done in Zope2.App.patches.publishing.
from zope.interface import Interface
self.assertFalse(Interface.names.__doc__)
self.assertFalse(Interface.getTaggedValue.__doc__)
self.assertFalse(Interface.setTaggedValue.__doc__)


def test_suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit 1190844

Please sign in to comment.