On Wednesday, January 18, 2017 at 9:40:35 PM UTC-5, Brendan Barnwell wrote: > > On Wednesday, January 18, 2017 at 2:12:35 PM UTC-8, Anthony wrote: >> >> A web2py controller is not like a Python module. It is meant to be >> executed in a prepared environment to generate the response to an HTTP >> request. Because the top level of the controller may include code you >> wouldn't want executed unless proceeding with an HTTP request, it must >> first be determined whether the requested function actually exists in the >> file *before* executing it (in order to allow execution to be aborted in >> case the function is not present). Also, when web2py compiles applications, >> it creates a separate bytecode compiled file for each function in each >> controller -- in order to do this, it must know the function names, again, >> without executing the file (as the compilation process takes place outside >> the environment of an HTTP request, and we don't want any side effects). >> >> > I see, thanks. What kind of things can be usefully done at the top level > that would cause problems outside the request environment? Most of the > stuff I see looking through the docs is stuff happening internally to > controller functions, except for a small amount of setup stuff (like > "service = Service()") that doesn't appear to have side effects. Are there > useful web2py design patterns that involve significant logic in the > controller top-level? >
For example, there may be some code that handles authentication for the entire controller or sets some DAL attributes that apply to the entire controller (e.g., defining a _common_filter). This means that to prevent this code from throwing errors, all the model files that would typically run during a request to this controller must be run (and that can be difficult to determine, as it is possible for the model files themselves to dynamically determine which other model files run on any given request). Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.