NewEvent is not triggered by pyramid.scripting #520

dairiki opened this Issue Mar 30, 2012 · 6 comments


None yet

3 participants

dairiki commented Mar 30, 2012

I have a root_factory which depends on a custom request property (via config.set_request_property).

When the code in pyramid.scripting generates a request, it does not emit a NewRequest. This means that (when I try to run pshell, for example) my custom request property does not get set. Consequently, my root_factory pukes.

Is this all my fault, or is it a bug?

(If it is deemed a bug, I'd be happy to formulate a pull request. My hunch is that pyramid.scripting._make_request should emit a NewRequest event when it constructs a new request.)

Edited: I'm having trouble with pshell not pscript.


This is a symptom of a larger issue with how bootstrap works. It currently just sets up the thread locals for you and doesn't really simulate a full request. Meaning that you can use the request to generate URLs and talk to your database but that's about it. What (might) be nice is to simulate a real request, but this isn't easy to do. The motivation here is that things like pyramid_tm aren't activated inside of a boostrapped environment so you have to do transaction.commit() yourself right now (and set your request properties). I'll wait for @mcdonc to chime in on this, however.

Is your root_factory puking before entering your script? A traceback would be nice regardless.

dairiki commented Mar 30, 2012

Oof. Nice to know that it's a "deep" problem, and not just me being stupid. Still, it seems to me that this particular part of problem is easily fixed. I'm a bit out of my depth though, so I could be wrong.

Sorry, it is pshell not pscript that is currently giving me trouble. A traceback is included below, but it's not that exciting. My root factory is trying to get a db session (normally available as a custom request property) so it can load the root. The custom property has not been set, which leads to an AttributeError. (I can work around this in my root factory without too much trouble.)

(py26-ale)dairiki@hairball$ pshell development.ini 
Traceback (most recent call last):
  File "/srv/w/users/dairiki/junk/python/virtualenvs/py26-ale/bin/pshell", line 9, in <module>
    load_entry_point('pyramid==1.3b3', 'console_scripts', 'pshell')()
  File "/srv/w/users/dairiki/junk/python/virtualenvs/py26-ale/lib/python2.6/site-packages/pyramid/scripts/", line 14, in main
  File "/srv/w/users/dairiki/junk/python/virtualenvs/py26-ale/lib/python2.6/site-packages/pyramid/scripts/", line 90, in run
    env = self.bootstrap[0](config_uri)
  File "/srv/w/users/dairiki/junk/python/virtualenvs/py26-ale/lib/python2.6/site-packages/pyramid/", line 120, in bootstrap
    env = prepare(request)
  File "/srv/w/users/dairiki/junk/python/virtualenvs/py26-ale/lib/python2.6/site-packages/pyramid/", line 80, in prepare
    root = root_factory(request)
  File "/srv/w/users/dairiki/git/discnw/python-rewrite/ale2/discnw/content/", line 20, in root_factory
    session = request.dbsession
AttributeError: 'Request' object has no attribute 'dbsession'

Thanks for the traceback, it confirms my suspicions that the error occurs not in your script but rather in the setup. Placing a NewRequest event in bootstrap makes me hesitant, but maybe the Pyramid logic for adding request properties can be modified to happen more automatically, without requiring the event be emitted.

dairiki commented Mar 31, 2012

maybe the Pyramid logic for adding request properties can be modified to happen more automatically, without requiring the event be emitted.

That is pretty straight-forward, I think. Here is a patch which does that:


Note that the above patch does not set the custom properties on any request instances explicitly passed in to prepare() or bootstrap(). That doesn't feel quite right to me, but it feels even more wrong to mess with any explicit request.

mcdonc commented Mar 31, 2012

Looks good to me, how about you @mmerickel

@mcdonc mcdonc added a commit that referenced this issue Sep 11, 2012
@mcdonc mcdonc - Request properties and methods added via ``config.set_request_prope…
…rty`` or

  ``config.add_request_method`` are now available to tweens.

- Request properties and methods added via ``config.set_request_property`` or
  ``config.add_request_method`` are now available in the request object
  returned from ``pyramid.paster.bootstrap``.

Related partially to issue #520 and issue #538 and issue #596, although it doesn't actually allow us to close any of them, because we still don't issue a newrequest event when bootstrap is used.

This has been fixed in 7359873 and released in 1.4a1. The solution does not emit a NewRequest event, but it does attach request properties to the request.

@mmerickel mmerickel closed this Nov 1, 2012
@spladug spladug added a commit to reddit/baseplate that referenced this issue May 29, 2016
@spladug spladug Add paste.app_factory adapter
This adapter allows PasteDeploy-based tools to load a baseplate
application. In a baseplate-style config file, adding the line "use =
egg:baseplate" is all that's necessary.

Note: a root span is not automatically created in scripting context
(e.g. pshell) see Pylons/pyramid#520.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment