home     documentation     download     Spyce logo

Documentation - Modules

Prev: - Error Up: 3.8.11 - Internal modules Next: - Spylambda

The stdout module is loaded implicitly and redirects Python's sys.stdout (in a thread-safe manner) to the appropriate response object for the duration of Spyce processing. This allows one to use print, without having to write print >> response, .... The stdout module provides a variable stdout.stdout, which refers to the original stream, but is unlikely to be needed. It may also be useful to know that sys.stderr is, under many configurations, connected to the webserver error log.

In addition, the stdout module provides the following functions for capturing or redirecting output:

  • push( [filename] ):
    Begin capturing output. Namely, the current output stream is pushed onto the stack and replaced with a memory buffer. An optional filename may be associated with this operation (see pop() method below).
  • pop():
    Close current output buffer, and return the captured output as a string. If a filename was associated with the push(), then the string will also be written to that file.
  • capture(f, [*args], [**kwargs] ):
    Push the current stream, call the given function f with any supplied arguments *args and keyword arguments **kwargs, and then pop it back. Capture returns a tuple (r,s), where r is the result returned by f and s is a string of its output.

The example below show how the module is used:

  [[ print '''Using the stdout module redirects 
    stdout to the response object, so you can use
  redirecting stdout can be used to...
  [[print 'capture']] out[[='put']]
  [[cached = stdout.pop()]]
  ... for later: <br>
Run this code

Prev: - Error Up: 3.8.11 - Internal modules Next: - Spylambda

Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo