Ticket #2432 (closed defect: fixed)

Opened 8 months ago

Last modified 7 months ago

call_on_startup functions do not successfully modify things in tg.config

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

Description

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:
        ignore.append('_csrf_token')
        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})
    else:
        config['variable_provider'] = lambda: {'fedora_template':
                fedora_template}
    print 'after', config.get('ignore_parameters', [])
    print 'after', config.get('variable_provider', [])

config/app_cfg.py:

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.

Attachments

traceback (3.0 kB) - added by toshio on 01/23/10 01:39:40.
traceback with current tip when giving a parameter specified via base_config.ignore_parameters

Change History

01/06/10 19:33:49 changed by lmacken

  • cc set to lmacken.

01/23/10 01:37:25 changed 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.

01/23/10 01:39:40 changed by toshio

  • attachment traceback added.

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

01/23/10 16:29:57 changed by percious

  • milestone changed from __unclassified__ to 2.1b1.

01/23/10 21:04:44 changed 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.

01/24/10 00:06:41 changed by percious

  • status changed from new to closed.
  • resolution set to fixed.