Sentry-python: Errors when using FlaskIntegration with pytest-flask

Created on 19 Dec 2018  Â·  7Comments  Â·  Source: getsentry/sentry-python

Version 0.6.3 introduced a couple of errors when running tests for Flask based project.

First case:

― ERROR at teardown of TestResource.test_one ―

tp = <class 'AssertionError'>, value = None, tb = None                                                                                                                                                                                                                            

    def reraise(tp, value, tb=None):                                                                                                                                                                                                                                             
        try:      
            if value is None:                                                
                value = tp()                                                                  
            if value.__traceback__ is not tb:                                                                                                                                                                                                                                    
                raise value.with_traceback(tb)                                              
>           raise value                                                

../../.virtualenvs/myproject/lib/python3.6/site-packages/six.py:693:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.virtualenvs/myproject/lib/python3.6/site-packages/six.py:693: in reraise
    raise value                                                                                                                                                                                                                                                                  
../../.virtualenvs/myproject/lib/python3.6/site-packages/six.py:693: in reraise
    raise value                                                                                                        
../../.virtualenvs/myproject/lib/python3.6/site-packages/pytest_flask/plugin.py:124: in teardown
    ctx.pop()                                                                                                                                                                                                                                                                    
../../.virtualenvs/myproject/lib/python3.6/site-packages/flask/ctx.py:423: in pop               
    app_ctx.pop(exc)                                                                                
../../.virtualenvs/myproject/lib/python3.6/site-packages/flask/ctx.py:231: in pop                                                                                                                                                                                       
    self.app.do_teardown_appcontext(exc)                                                                          
../../.virtualenvs/myproject/lib/python3.6/site-packages/flask/app.py:2167: in do_teardown_appcontext
    appcontext_tearing_down.send(self, exc=exc)                        
../../.virtualenvs/myproject/lib/python3.6/site-packages/blinker/base.py:267: in send                
    for receiver in self.receivers_for(sender)]
../../.virtualenvs/myproject/lib/python3.6/site-packages/blinker/base.py:267: in <listcomp>
    for receiver in self.receivers_for(sender)]
../../.virtualenvs/myproject/lib/python3.6/site-packages/sentry_sdk/integrations/flask.py:72: in _pop_appctx
    hub.pop_scope_unsafe()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <sentry_sdk.hub.Hub object at 0x7f834f613cf8>

    def pop_scope_unsafe(self):
        """Pops a scope layer from the stack. Try to use the context manager
        `push_scope()` instead."""
        rv = self._stack.pop()
>       assert self._stack, "stack must have at least one layer"
E       AssertionError: stack must have at least one layer

../../.virtualenvs/myproject/lib/python3.6/site-packages/sentry_sdk/hub.py:321: AssertionError

Second case:

― ERROR at setup of TestResource.test_two ―

request = <SubRequest '_push_request_context' for <Function 'test_two'>>

    @pytest.fixture(autouse=True)                                             
    def _push_request_context(request):                                      
        """During tests execution request context has been pushed, e.g. `url_for`,
        `session`, etc. can be used in tests as is::

            def test_app(app, client):
                assert client.get(url_for('myview')).status_code == 200

        """
        if 'app' not in request.fixturenames:                                                 
            return                                                                                                                                                                                                                                                               

        app = getfixturevalue(request, 'app')

        # Get application bound to the live server if ``live_server`` fixture
        # is applied. Live server application has an explicit ``SERVER_NAME``,                 
        # so ``url_for`` function generates a complete URL for endpoint which
        # includes application port as well.
        if 'live_server' in request.fixturenames:
            app = getfixturevalue(request, 'live_server').app

        ctx = app.test_request_context()
>       ctx.push()

../../.virtualenvs/myproject/lib/python3.6/site-packages/pytest_flask/plugin.py:121:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.virtualenvs/myproject/lib/python3.6/site-packages/flask/ctx.py:360: in push
    app_ctx.push()
../../.virtualenvs/myproject/lib/python3.6/site-packages/flask/ctx.py:222: in push
    appcontext_pushed.send(self.app)
../../.virtualenvs/myproject/lib/python3.6/site-packages/blinker/base.py:267: in send
    for receiver in self.receivers_for(sender)]
../../.virtualenvs/myproject/lib/python3.6/site-packages/blinker/base.py:267: in <listcomp>
    for receiver in self.receivers_for(sender)]
../../.virtualenvs/myproject/lib/python3.6/site-packages/sentry_sdk/integrations/flask.py:61: in _push_appctx
    if hub.get_integration(FlaskIntegration) is not None:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <sentry_sdk.hub.Hub object at 0x7f834f613cf8>, name_or_class = 'flask'

    def get_integration(self, name_or_class):
        """Returns the integration for this hub by name or class.  If there
        is no client bound or the client does not have that integration
        then `None` is returned.

        If the return value is not `None` the hub is guaranteed to have a
        client attached.
        """
        if not isinstance(name_or_class, string_types):
            name_or_class = name_or_class.identifier
>       client = self._stack[-1][0]
E       IndexError: list index out of range

../../.virtualenvs/myproject/lib/python3.6/site-packages/sentry_sdk/hub.py:175: IndexError
bug

All 7 comments

This is a bit vague. What is the testcase that causes this?

Oh sorry I can reproduce this issue. Interesting, it depends on init order

Perhaps the reason not in pytest-flask plugin..

import sentry_sdk
from flask import Flask
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(integrations=[FlaskIntegration()])

app = Flask(__name__)


@app.route('/')
def hello():
    return


def test_failure():
    with app.test_client() as client:
        client.get('/')
$ pytest example.py

yes sorry, this is a serious regression. Will fix asap

master should fix this. Could you try?

pip install git+https://github.com/getsentry/sentry-python#egg=sentry-sdk

It's ok now, thanks! Can you release it, please?

0.6.4 should fix this

Was this page helpful?
0 / 5 - 0 ratings