Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #2432 (closed defect: fixed)

Opened 10 years ago

Last modified 9 years ago

call_on_startup functions do not successfully modify things in tg.config

Reported by: toshio Owned by:
Priority: normal Milestone: 2.1b1
Component: TurboGears Version: 2.0.3
Severity: normal Keywords:
Cc: lmacken


I have a library that does a few things for a TG2 app. To make things easy on the people who will be using my library in their apps I have a function to call on application startup to set a few things up. Among other things, this function attempts to modify base_config.variable_providers and base_config.ignore_parameters. Here's the code:

def enable_csrf():
    # Ignore the _csrf_token parameter
    ignore = config.get('ignore_parameters', [])
    print 'before', config.get('ignore_parameters', [])
    print 'before', config.get('variable_provider', [])
    if '_csrf_token' not in ignore:
        config.update['ignore_parameters'] = ignore

    # Add a function to the template tg stdvars that looks up a template.
    var_provider = config.get('variable_provider', None)
    if var_provider:
        config['variable_provider'] = lambda: \
            var_provider().update({'fedora_template': fedora_template})
        config['variable_provider'] = lambda: {'fedora_template':
    print 'after', config.get('ignore_parameters', [])
    print 'after', config.get('variable_provider', [])


base_config.call_on_startup = [enable_csrf]

When used like this, call_on_startup runs the enable_csrf() function but the modifications of tg.configignore_parameters? and tg.configvariable_providers? do not show up outside of the enable_csrf() function.

elpargo took an initial look at the function over IRC and had these comments:

right. It indeed seems like a) a bug in startup or b) we need a separate hook. take a look at tg/configuration. make_load_environment sets up the functions at setup_startup_and_shutdown the comment stays it will Register the functions however it is actually calling them. however in setup_tg_wsgi_app/make_base_app which is when things are actually created there is no call for that. to be honest I have never needed those, but this code doesn't seems right. all those setup_* functions are supposed to pull things in rather than execute them.

I have reworked my code to call my startup function in an overridden AppConfig? which may work out better for me in the long-run but thought this should either be addressed as a code or documentation bug. I can provide more code examples if needed but I'm out of town until January 4th.


traceback Download (3.0 KB) - added by toshio 9 years ago.
traceback with current tip when giving a parameter specified via base_config.ignore_parameters

Change History

comment:1 Changed 10 years ago by lmacken

  • Cc lmacken added

comment:2 Changed 9 years ago by toshio

With current tip, ignore_parameters appears to be broken altogether.

Fresh paster quickstart with this addition to app_cfg.py:

base_config.ignore_parameters = yum?

browse to  http://localhost:8080/?yum=1234 Get a traceback:

TypeError?: index() got an unexpected keyword argument 'yum'

Will atach the full traceback.

Changed 9 years ago by toshio

traceback with current tip when giving a parameter specified via base_config.ignore_parameters

comment:3 Changed 9 years ago by percious

  • Milestone changed from __unclassified__ to 2.1b1

comment:4 Changed 9 years ago by percious

Hey, i made some considerable strides with dispatch today. If you could try again with tip, and if you are still failing please provide me with the controller method code. At the very least your code should now 404 instead of 500.

comment:5 Changed 9 years ago by percious

  • Status changed from new to closed
  • Resolution set to fixed
Note: See TracTickets for help on using tickets.