Liuke
Liuke

Reputation: 21

The implementation of CherryPy's redirect

Why request redirects in CherryPy 3.x are implemented with raise, but not with return? Redirect operation may look like the following:

raise cherrypy.HTTPRedirect('/index')

What CherryPy does when raise occurs? Why not return?

Upvotes: 2

Views: 464

Answers (1)

saaj
saaj

Reputation: 25263

Well, basically it is an implementation detail. Normally you return from CherryPy handler:

  • str, unicode, bytes or iterable of them
  • file-like object
  • generator for streaming content

All these objects eventually become a response body. Redirects (3xx HTTP codes) and errors (cherrypy.HTTPError, 4xx and 5xx HTTP codes) either have no body or the body is set out of the handler (default of custom error pages). So it makes sense to distinguish them this way.

Also note that in Python exceptions are also a part of normal flow, e.g. StopIteration.

Upvotes: 1

Related Questions