[Checkins] SVN: zope.component/tseaver-test_cleanup/ Explicit doctest sections.
Tres Seaver
cvs-admin at zope.org
Sun Jun 17 18:32:17 UTC 2012
Log message for revision 126896:
Explicit doctest sections.
Changed:
_U zope.component/tseaver-test_cleanup/
U zope.component/tseaver-test_cleanup/docs/hooks.rst
-=-
Modified: zope.component/tseaver-test_cleanup/docs/hooks.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/hooks.rst 2012-06-17 18:32:10 UTC (rev 126895)
+++ zope.component/tseaver-test_cleanup/docs/hooks.rst 2012-06-17 18:32:14 UTC (rev 126896)
@@ -11,86 +11,102 @@
As long as we haven't set a site, none is being considered current:
->>> from zope.component.hooks import getSite
->>> print getSite()
-None
+.. doctest::
+ >>> from zope.component.hooks import getSite
+ >>> print getSite()
+ None
+
We can also ask for the current component registry (aka site manager
historically); it will return the global one if no current site is set:
->>> from zope.component.hooks import getSiteManager
->>> getSiteManager()
-<BaseGlobalComponents base>
+.. doctest::
+ >>> from zope.component.hooks import getSiteManager
+ >>> getSiteManager()
+ <BaseGlobalComponents base>
+
Let's set a site now. A site has to be an object that provides the
``getSiteManager`` method, which is specified by
``zope.component.interfaces.IPossibleSite``:
->>> from zope.interface.registry import Components
->>> class Site(object):
-... def __init__(self):
-... self.registry = Components('components')
-... def getSiteManager(self):
-... return self.registry
+.. doctest::
->>> from zope.component.hooks import setSite
->>> site1 = Site()
->>> setSite(site1)
+ >>> from zope.interface.registry import Components
+ >>> class Site(object):
+ ... def __init__(self):
+ ... self.registry = Components('components')
+ ... def getSiteManager(self):
+ ... return self.registry
+ >>> from zope.component.hooks import setSite
+ >>> site1 = Site()
+ >>> setSite(site1)
+
After this, the newly set site is considered the currently active one:
->>> getSite() is site1
-True
->>> getSiteManager() is site1.registry
-True
+.. doctest::
+ >>> getSite() is site1
+ True
+ >>> getSiteManager() is site1.registry
+ True
+
If we set another site, that one will be considered current:
->>> site2 = Site()
->>> site2.registry is not site1.registry
-True
->>> setSite(site2)
+.. doctest::
->>> getSite() is site2
-True
->>> getSiteManager() is site2.registry
-True
+ >>> site2 = Site()
+ >>> site2.registry is not site1.registry
+ True
+ >>> setSite(site2)
+ >>> getSite() is site2
+ True
+ >>> getSiteManager() is site2.registry
+ True
+
Finally we can unset the site and the global component registry is used again:
->>> setSite()
->>> print getSite()
-None
->>> getSiteManager()
-<BaseGlobalComponents base>
+.. doctest::
+ >>> setSite()
+ >>> print getSite()
+ None
+ >>> getSiteManager()
+ <BaseGlobalComponents base>
+
Context manager
===============
There also is a context manager for setting the site, which is especially
useful when writing tests:
->>> import zope.component.hooks
->>> print getSite()
-None
->>> with zope.component.hooks.site(site2):
-... getSite() is site2
-True
->>> print getSite()
-None
+.. doctest::
+ >>> import zope.component.hooks
+ >>> print getSite()
+ None
+ >>> with zope.component.hooks.site(site2):
+ ... getSite() is site2
+ True
+ >>> print getSite()
+ None
+
The site is properly restored even if the body of the with statement
raises an exception:
->>> print getSite()
-None
->>> with zope.component.hooks.site(site2):
-... getSite() is site2
-... raise ValueError('An error in the body')
-Traceback (most recent call last):
- ...
-ValueError: An error in the body
->>> print getSite()
-None
+.. doctest::
+ >>> print getSite()
+ None
+ >>> with zope.component.hooks.site(site2):
+ ... getSite() is site2
+ ... raise ValueError('An error in the body')
+ Traceback (most recent call last):
+ ...
+ ValueError: An error in the body
+ >>> print getSite()
+ None
+
More information about the checkins
mailing list