librelist archives

« back to archive

flask.g change

flask.g change

From:
Joe Angell
Date:
2013-06-18 @ 18:10
I love flask, you guys have done great work.  One issue with the latest
release, the flask.g changing from request to app context.  It's said in
the upgrade doc that the "change should be transparent for you..."  How is
this change transparent?  It changes the entire point of the g storage.  I
can't imagine a situation where this would not break code.  To get the old
behavior can we just use the request object like the old g?

Thanks,
-- 
---------------
Joe Angell
cell: (720) 260-2190

Re: [flask] flask.g change

From:
Patrick Landis
Date:
2013-06-18 @ 19:13
I think that all globals in Flask can be accessed from the active context.

This can be application or request contexts. I think the change is that
instead of the RequestContext grabbing g from _RequestGlobals (a generic
object), RequestContext grabs g from the ApplicationContext which grabs g
from _AppContextGlobals (replacement for _RequestGlobals). I think the
change is transparent in the sense that g doesn't really break backward
compatibility and instead allows you to use g in the app context in
addition to the request context.

You can take a look at:
Newest version of ctx.py (

https://github.com/mitsuhiko/flask/blob/master/flask/ctx.py<https://github.com/mitsuhiko/flask/blob/master/flask/ctx.py#L189>
)
Older version of ctx.py (

https://github.com/mitsuhiko/flask/blob/2b885ce4dc3f6b2ea2707a39a8198a84d7ad3991/flask/ctx.py)
(Last commit before g was switched to app context)




On Tue, Jun 18, 2013 at 2:10 PM, Joe Angell <joe.d.angell@gmail.com> wrote:

> I love flask, you guys have done great work.  One issue with the latest
> release, the flask.g changing from request to app context.  It's said in
> the upgrade doc that the "change should be transparent for you..."  How is
> this change transparent?  It changes the entire point of the g storage.  I
> can't imagine a situation where this would not break code.  To get the old
> behavior can we just use the request object like the old g?
>
> Thanks,
> --
> ---------------
> Joe Angell
> cell: (720) 260-2190
>

回复: [flask] flask.g change

From:
linnchord
Date:
2013-06-18 @ 19:49
code in flask/ctx.py

def _get_g(self):
    return _app_ctx_stack.top.g
def _set_g(self, value):
    _app_ctx_stack.top.g = value
g = property(_get_g, _set_g)

The g is from _app_ctx_stack.top, so if you access it when no request, you
get a g with app, else you get it with request.  


===

linnchord@gmail.com



在 2013年6月19日星期三,上午2:10,Joe Angell 写道:

> I love flask, you guys have done great work.  One issue with the latest 
release, the flask.g changing from request to app context.  It's said in 
the upgrade doc that the "change should be transparent for you..."  How is
this change transparent?  It changes the entire point of the g storage.  I
can't imagine a situation where this would not break code.  To get the old
behavior can we just use the request object like the old g?  
>  
> Thanks,
> --  
> ---------------
> Joe Angell
> cell: (720) 260-2190  

Re: 回复: [flask] flask.g change

From:
Joe Angell
Date:
2013-06-18 @ 19:55
Thanks for the replies.  It looks like the issue I ran into is related to
using flask-testing client.  I have one test that uses the client to send
two posts, and the g object must be coming from the app... still
investigating.


On Tue, Jun 18, 2013 at 1:49 PM, linnchord <linnchord@gmail.com> wrote:

> code in flask/ctx.py
>
> def _get_g(self):
>     return _app_ctx_stack.top.g
> def _set_g(self, value):
>     _app_ctx_stack.top.g = value
> g = property(_get_g, _set_g)
>
> The g is from *_app_ctx_stack.top*, so if you access it when no request,
you get a g with app, else you get it with request.
>
>
>
> ===
>
> linnchord@gmail.com
>
>
> 在 2013年6月19日星期三,上午2:10,Joe Angell 写道:
>
> I love flask, you guys have done great work.  One issue with the latest
> release, the flask.g changing from request to app context.  It's said in
> the upgrade doc that the "change should be transparent for you..."  How is
> this change transparent?  It changes the entire point of the g storage.  I
> can't imagine a situation where this would not break code.  To get the old
> behavior can we just use the request object like the old g?
>
> Thanks,
> --
> ---------------
> Joe Angell
> cell: (720) 260-2190
>
>
>


-- 
---------------
Joe Angell
cell: (720) 260-2190

Re: 回复: [flask] flask.g change

From:
Christopher O'Donnell
Date:
2013-06-18 @ 21:24
unsubscribe

Re: 回复: [flask] flask.g change

From:
Joe Angell
Date:
2013-06-18 @ 23:07
Here is a test case, the issue lies with flask-testing if anyone is
interested:

from flask import Flask, g
app = Flask(__name__)
from flask.ext.testing import TestCase
import flask.ext.testing

@app.route("/")
def hello():
    if hasattr(g, 'foo'):
        return "g.foo is set to:" + g.foo
    else:
        g.foo = "bar"
        return "Hello World!"

class GTest(TestCase):
    def create_app(self):

        return app
    def test_g(self):
        tc = self.client
        r = tc.get('/')
        print r.data
        r = tc.get('/')
        print r.data
        self.assertEqual(r.data, "Hello World!")

if __name__ == "__main__":
    flask.ext.testing.unittest.main()




On Tue, Jun 18, 2013 at 3:24 PM, Christopher O'Donnell <
mail@chrisodonnell.net> wrote:

> unsubscribe
>



-- 
---------------
Joe Angell
cell: (720) 260-2190