Skip to content

Commit

Permalink
Patch zope.interface to remove docstrings and avoid publishing.
Browse files Browse the repository at this point in the history
From Products.PloneHotfix20161129.

Moved patch_persistent to the new 'patches' folder.
  • Loading branch information
mauritsvanrees committed Dec 27, 2016
1 parent ffefc3f commit 6ca2f5e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -11,9 +11,13 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
Bugs Fixed
++++++++++

- 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]


Features Added
++++++++++++++

Expand Down
16 changes: 16 additions & 0 deletions src/Zope2/App/patches/__init__.py
@@ -0,0 +1,16 @@
# Import all patches.
import persistence
import publishing

# Have the patches been applied yet?
_patched = False


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

persistence.apply_patches()
publishing.apply_patches()
13 changes: 13 additions & 0 deletions src/Zope2/App/patches/persistence.py
@@ -0,0 +1,13 @@
# Has this patch been applied yet?
_patched = False


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

from Persistence import Persistent
from AccessControl.class_init import InitializeClass
Persistent.__class_init__ = InitializeClass
68 changes: 68 additions & 0 deletions src/Zope2/App/patches/publishing.py
@@ -0,0 +1,68 @@
# -*- 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)
15 changes: 2 additions & 13 deletions src/Zope2/App/startup.py
Expand Up @@ -47,7 +47,6 @@

app = None
startup_time = asctime()
_patched = False


def load_zcml():
Expand All @@ -60,19 +59,9 @@ def load_zcml():
configure_vocabulary_registry()


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

from Persistence import Persistent
from AccessControl.class_init import InitializeClass
Persistent.__class_init__ = InitializeClass


def startup():
patch_persistent()
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
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 6ca2f5e

Please sign in to comment.