Skip to content

Commit fa51e57

Browse files
committed
Document the wsgiref attributes instead of suppressing the references
Replace the "!"-suppressed references with real targets: .. method:: directives for Headers.keys(), Headers.values() and Headers.items(), and .. attribute:: directives for WSGIServer.base_environ, BaseHandler.environ and SimpleHandler's stdin, stdout and stderr. Correct the SimpleHandler paragraph while documenting it: the constructor stores the supplied environment in base_env, not environ, and add_cgi_vars() merges it into BaseHandler.environ during setup_environ(). paste.lint (a third-party module) and FileWrapper.close (bound conditionally from the wrapped object, never defined on the class) keep the "!" prefix, since neither can have a real target.
1 parent 665de3a commit fa51e57

1 file changed

Lines changed: 63 additions & 21 deletions

File tree

Doc/library/wsgiref.rst

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,30 @@ manipulation of WSGI response headers using a mapping-like interface.
219219
nonexistent header just returns ``None``, and deleting a nonexistent header does
220220
nothing.
221221

222-
:class:`Headers` objects also support :meth:`!keys`, :meth:`!values`, and
223-
:meth:`!items` methods. The lists returned by :meth:`!keys` and
224-
:meth:`!items` can include the same key more than once if there is a
225-
multi-valued header. The ``len()`` of a :class:`Headers` object is the same
226-
as the length of its :meth:`!items`, which is the same as the length of the
227-
wrapped header list. In fact, the :meth:`!items` method just returns a copy
228-
of the wrapped header list.
222+
:class:`Headers` objects also support :meth:`keys`, :meth:`values`, and
223+
:meth:`items` methods. The lists returned by :meth:`keys` and :meth:`items` can
224+
include the same key more than once if there is a multi-valued header. The
225+
``len()`` of a :class:`Headers` object is the same as the length of its
226+
:meth:`items`, which is the same as the length of the wrapped header list.
227+
228+
229+
.. method:: Headers.keys()
230+
231+
Return a list of all the header field names, in the order the fields
232+
appeared in the original header list or were added to this instance. Any
233+
fields deleted and re-inserted are always appended to the header list.
234+
235+
236+
.. method:: Headers.values()
237+
238+
Return a list of all the header values, in the same order as :meth:`keys`.
239+
240+
241+
.. method:: Headers.items()
242+
243+
Return a copy of the wrapped header list, as a list of ``(name, value)``
244+
pairs in the same order as :meth:`keys`.
245+
229246

230247
Calling ``bytes()`` on a :class:`Headers` object returns a formatted bytestring
231248
suitable for transmission as HTTP response headers. Each header is placed on a
@@ -351,6 +368,16 @@ request. (E.g., using the :func:`~wsgiref.util.shift_path_info` function from
351368
:meth:`get_app` exists mainly for the benefit of request handler instances.
352369

353370

371+
.. attribute:: WSGIServer.base_environ
372+
373+
The base set of CGI environment variables the server supplies to every
374+
request, such as ``SERVER_NAME``, ``SERVER_PORT`` and
375+
``GATEWAY_INTERFACE``. It is populated when the server is bound to its
376+
address, and :meth:`WSGIRequestHandler.get_environ` copies it to build the
377+
environment for each individual request, adding and overriding entries
378+
that are specific to that request.
379+
380+
354381
.. class:: WSGIRequestHandler(request, client_address, server)
355382

356383
Create an HTTP handler for the given *request* (i.e. a socket), *client_address*
@@ -365,13 +392,12 @@ request. (E.g., using the :func:`~wsgiref.util.shift_path_info` function from
365392

366393
.. method:: WSGIRequestHandler.get_environ()
367394

368-
Return a :data:`~wsgiref.types.WSGIEnvironment` dictionary for a
369-
request. The default
370-
implementation copies the contents of the :class:`WSGIServer` object's
371-
:attr:`!base_environ` dictionary attribute and then adds various headers derived
372-
from the HTTP request. Each call to this method should return a new dictionary
373-
containing all of the relevant CGI environment variables as specified in
374-
:pep:`3333`.
395+
Return a :data:`~wsgiref.types.WSGIEnvironment` dictionary for a request.
396+
The default implementation copies the contents of the :class:`WSGIServer`
397+
object's :attr:`~WSGIServer.base_environ` dictionary attribute and then
398+
adds various headers derived from the HTTP request. Each call to this
399+
method should return a new dictionary containing all of the relevant CGI
400+
environment variables as specified in :pep:`3333`.
375401

376402

377403
.. method:: WSGIRequestHandler.get_stderr()
@@ -533,14 +559,24 @@ input, output, and error streams.
533559
:meth:`~BaseHandler.get_stderr`, :meth:`~BaseHandler.add_cgi_vars`,
534560
:meth:`~BaseHandler._write`, and :meth:`~BaseHandler._flush` methods to
535561
support explicitly setting the
536-
environment and streams via the constructor. The supplied environment and
537-
streams are stored in the :attr:`!stdin`, :attr:`!stdout`, :attr:`!stderr`,
538-
and :attr:`!environ` attributes.
562+
environment and streams via the constructor. The supplied streams are stored
563+
in the :attr:`stdin`, :attr:`stdout`, and :attr:`stderr` attributes, and the
564+
supplied environment is merged into :attr:`~BaseHandler.environ` when the
565+
environment for the request is set up.
539566

540567
The :meth:`~io.BufferedIOBase.write` method of *stdout* should write
541568
each chunk in full, like :class:`io.BufferedIOBase`.
542569

543570

571+
.. attribute:: SimpleHandler.stdin
572+
SimpleHandler.stdout
573+
SimpleHandler.stderr
574+
575+
The streams supplied to the constructor. *stdin* is used as the
576+
``wsgi.input`` stream, *stdout* receives the response, and *stderr* is
577+
used as the ``wsgi.errors`` stream.
578+
579+
544580
.. class:: BaseHandler()
545581

546582
This is an abstract base class for running WSGI applications. Each instance
@@ -591,8 +627,7 @@ input, output, and error streams.
591627

592628
.. method:: BaseHandler.add_cgi_vars()
593629

594-
Insert CGI variables for the current request into the :attr:`!environ`
595-
attribute.
630+
Insert CGI variables for the current request into the :attr:`environ` attribute.
596631

597632
Here are some other methods and attributes you may wish to override. This list
598633
is only a summary, however, and does not include every method that can be
@@ -651,18 +686,25 @@ input, output, and error streams.
651686
Return the URL scheme being used for the current request. The default
652687
implementation uses the :func:`~wsgiref.util.guess_scheme` function from
653688
:mod:`wsgiref.util` to guess whether the scheme should be "http" or
654-
"https", based on the current request's :attr:`!environ` variables.
689+
"https", based on the current request's :attr:`environ` variables.
655690

656691

657692
.. method:: BaseHandler.setup_environ()
658693

659-
Set the :attr:`!environ` attribute to a fully populated WSGI environment. The
694+
Set the :attr:`environ` attribute to a fully populated WSGI environment. The
660695
default implementation uses all of the above methods and attributes, plus the
661696
:meth:`get_stdin`, :meth:`get_stderr`, and :meth:`add_cgi_vars` methods and the
662697
:attr:`wsgi_file_wrapper` attribute. It also inserts a ``SERVER_SOFTWARE`` key
663698
if not present, as long as the :attr:`origin_server` attribute is a true value
664699
and the :attr:`server_software` attribute is set.
665700

701+
702+
.. attribute:: BaseHandler.environ
703+
704+
The :data:`~wsgiref.types.WSGIEnvironment` dictionary for the request
705+
currently being processed. It is created by :meth:`setup_environ` and
706+
passed to the application by :meth:`run`.
707+
666708
Methods and attributes for customizing exception handling:
667709

668710

0 commit comments

Comments
 (0)