summaryrefslogtreecommitdiff
path: root/docs/narr
diff options
context:
space:
mode:
authorBert JW Regeer <xistence@0x58.com>2016-04-25 16:56:16 -0600
committerBert JW Regeer <xistence@0x58.com>2016-04-25 16:56:16 -0600
commit0fdf616cebb439e5dff388ca3bd5cd0c3bdcfd1b (patch)
tree93cca74308e7a7440badd91eb1c0611af420c7bd /docs/narr
parent1a6e20fbc048a03bee8b8be8955788861c4ed9ed (diff)
parent0fec6d3d000425314f5a50f98d6409277b89512c (diff)
downloadpyramid-0fdf616cebb439e5dff388ca3bd5cd0c3bdcfd1b.tar.gz
pyramid-0fdf616cebb439e5dff388ca3bd5cd0c3bdcfd1b.tar.bz2
pyramid-0fdf616cebb439e5dff388ca3bd5cd0c3bdcfd1b.zip
Merge pull request #2530 from stevepiercy/master
Allow Sphinx doctests to run and pass with `make doctest SPHINXBUILD=$VENV/bin/sphinx-build`
Diffstat (limited to 'docs/narr')
-rw-r--r--docs/narr/hooks.rst62
-rw-r--r--docs/narr/sessions.rst45
2 files changed, 86 insertions, 21 deletions
diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst
index b776f99e8..150eca944 100644
--- a/docs/narr/hooks.rst
+++ b/docs/narr/hooks.rst
@@ -300,13 +300,38 @@ added as a property and its result is cached per-request by setting
``reify=True``. This way, we eliminate the overhead of running the function
multiple times.
+.. testsetup:: group1
+
+ from pyramid.config import Configurator
+
+
+ def total(request, *args):
+ return sum(args)
+
+
+ def prop(request):
+ print("getting the property")
+ return "the property"
+
+
+
+ config = Configurator()
+ config.add_request_method(total)
+ config.add_request_method(prop, reify=True)
+ config.commit()
+
+ from pyramid.scripting import prepare
+ request = prepare(registry=config.registry)["request"]
+
+.. doctest:: group1
+
>>> request.total(1, 2, 3)
6
>>> request.prop
getting the property
- the property
+ 'the property'
>>> request.prop
- the property
+ 'the property'
To not cache the result of ``request.prop``, set ``property=True`` instead of
``reify=True``.
@@ -338,13 +363,42 @@ Here is an example of passing a class to ``Configurator.add_request_method``:
We attach and cache an object named ``extra`` to the ``request`` object.
+.. testsetup:: group2
+
+ from pyramid.config import Configurator
+ from pyramid.decorator import reify
+
+ class ExtraStuff(object):
+
+ def __init__(self, request):
+ self.request = request
+
+ def total(self, *args):
+ return sum(args)
+
+ # use @property if you don't want to cache the result
+ @reify
+ def prop(self):
+ print("getting the property")
+ return "the property"
+
+ config = Configurator()
+ config.add_request_method(ExtraStuff, 'extra', reify=True)
+ config.commit()
+
+ from pyramid.scripting import prepare
+ request = prepare(registry=config.registry)["request"]
+
+.. doctest:: group2
+
>>> request.extra.total(1, 2, 3)
6
>>> request.extra.prop
getting the property
- the property
+ 'the property'
>>> request.extra.prop
- the property
+ 'the property'
+
.. index::
single: response factory
diff --git a/docs/narr/sessions.rst b/docs/narr/sessions.rst
index 7e961f5e8..a1319e45f 100644
--- a/docs/narr/sessions.rst
+++ b/docs/narr/sessions.rst
@@ -260,19 +260,28 @@ added to the flash queue, and empties the queue.
.. method:: pop_flash(queue='')
->>> request.session.flash('info message')
->>> request.session.pop_flash()
-['info message']
+.. testsetup::
+
+ from pyramid import testing
+ request = testing.DummyRequest()
+
+.. doctest::
+
+ >>> request.session.flash('info message')
+ >>> request.session.pop_flash()
+ ['info message']
Calling ``session.pop_flash()`` again like above without a corresponding call
to ``session.flash()`` will return an empty list, because the queue has already
been popped.
->>> request.session.flash('info message')
->>> request.session.pop_flash()
-['info message']
->>> request.session.pop_flash()
-[]
+.. doctest::
+
+ >>> request.session.flash('info message')
+ >>> request.session.pop_flash()
+ ['info message']
+ >>> request.session.pop_flash()
+ []
.. index::
single: session.peek_flash
@@ -287,15 +296,17 @@ flash storage.
.. method:: peek_flash(queue='')
->>> request.session.flash('info message')
->>> request.session.peek_flash()
-['info message']
->>> request.session.peek_flash()
-['info message']
->>> request.session.pop_flash()
-['info message']
->>> request.session.peek_flash()
-[]
+.. doctest::
+
+ >>> request.session.flash('info message')
+ >>> request.session.peek_flash()
+ ['info message']
+ >>> request.session.peek_flash()
+ ['info message']
+ >>> request.session.pop_flash()
+ ['info message']
+ >>> request.session.peek_flash()
+ []
.. index::
single: preventing cross-site request forgery attacks