-
Notifications
You must be signed in to change notification settings - Fork 888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using pyramid.paster.bootstrap as context manager #1822 #1828
Using pyramid.paster.bootstrap as context manager #1822 #1828
Conversation
I don't understand the motivation here: the environment currently returned from |
I agree -- I proposed something like this in IRC a week or two ago, but I was intending that the context manager would automatically call the closer on exit. |
Sorry, then I guess I did not understand the description of the issue. There speaks the bootstrap function should be context manager. I left it like that. prepare the function that makes the "closer", right? |
The reason for wanting it to be a context manager is so that I don't have to remember to call the closer myself. |
Not to mention that wrapping it in a If we want to enable such a feature, it would be more sensible to add a new function, wrapped in @contextmanager
def boostrap_cm(config_uri, request=None, options=None):
"""Self-closing boostrapped environment.
Usable only via ``with bootstrap_cm(...):``.
"""
env = bootstrap(configu_uri. request, options)
yield env
env['closer']() |
I assume this work is in reference to #1822 which is not intended to break compatibility. The returned |
@mmerickel @tseaver @davisagli class EnvManager(dict):
def __init__(self, env):
self.update(**env)
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self["closer"]()
# nothing change in prepare
# pyramid/paster.py
from pyramid.scripting import prepare, EnvManager
def bootstrap(...)
app = get_app(config_uri, options=options)
env = prepare(request)
env['app'] = app
return EnvManager(env) |
def test_it_request_with_registry_as_context_manager(self):
request = DummyRequest({})
request.registry = dummy_registry
result = self._callFUT('/foo/bar/myapp.ini', request)
with result:
self.assertEqual(result['app'], self.app)
self.assertEqual(result['root'], self.root)
self.assertTrue('closer' in result)
def test_it_request_with_registry_is_not_context_manager(self):
request = DummyRequest({})
request.registry = dummy_registry
result = self._callFUT('/foo/bar/myapp.ini', request)
self.assertEqual(result['app'], self.app)
self.assertEqual(result['root'], self.root)
self.assertTrue('closer' in result)
root@c7ac8588abbe:/azk/pyramid# nosetests pyramid.tests.test_paster.Test_bootstrap
..
----------------------------------------------------------------------
Ran 2 tests in 0.341s
OK |
I'm not sure that calling |
You will want to update your test to assert that the closer was called afterward. |
Ok @mmerickel :) |
Also the idea is that |
Nice, copy that. |
Sorry to chime in late, and I'm even sorrier to add more hoops to jump through to be able to merge this. I'd merge this if:
|
@mcdonc @tseaver @mmerickel @davisagli |
Hey @marioidival thanks for the update and your efforts on the patch. If you'd like to submit another commit with your name in CONTRIBUTORS.txt we can continue your work, otherwise we'll have to close this. In the meantime I'm going to close this PR. |
Hello guys, I'm back, I'll continue this PR, all right?? |
@marioidival glad to see you back! Please proceed and submit a new commit to this PR, then one of us will reopen the issue. Thank you! |
No description provided.