pallets-eco / flask-debugtoolbar

A toolbar overlay for debugging Flask applications
https://flask-debugtoolbar.readthedocs.io
BSD 3-Clause "New" or "Revised" License
953 stars 146 forks source link

AttributeError: 'ProfilerDebugPanel' object has no attribute 'dump_filename' #286

Open mconigliaro opened 2 months ago

mconigliaro commented 2 months ago

I enable the profiler and this happens:

Traceback (most recent call last):
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1498, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1476, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 883, in full_dispatch_request
    return self.finalize_request(rv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 904, in finalize_request
    response = self.process_response(response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1281, in process_response
    response = self.ensure_sync(func)(response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask_debugtoolbar/__init__.py", line 248, in process_response
    panel.process_response(real_request, response)
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask_debugtoolbar/panels/profiler.py", line 96, in process_response
    if self.dump_filename:
       ^^^^^^^^^^^^^^^^^^^
AttributeError: 'ProfilerDebugPanel' object has no attribute 'dump_filename'

I have to clear my cookies to make the error go away.

Environment:

mconigliaro commented 2 months ago

According to the code, self.dump_filename doesn't get set unless DEBUG_TB_PROFILER_ENABLED is True. But if I enable that, I get this:

Traceback (most recent call last):
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1498, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1476, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mike/Developer/other/test/test-flask/.venv/lib/python3.12/site-packages/flask_debugtoolbar/__init__.py", line 149, in dispatch_request
    return view_func(**req.view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12/cProfile.py", line 109, in runcall
    self.enable()
ValueError: Another profiling tool is already active
liaosf commented 1 month ago

Set environment variant to resolve the issues like "no attribute 'dump_filename' ". DEBUG_TB_PROFILER_ENABLED = True DEBUG_TB_DUMP_FILENAME_FORMAT = {request.url} # other setting like: {request.method}, {request.path}

trupka commented 1 month ago

It seems that ProfilerDebugPanel().dump_filename attribute is undefined whenDEBUG_TB_PROFILER_ENABLED != True. Possible solution is to modify __init__(self, jinja_env, context={}) method, someting like:

diff --git a/.venv/Lib/site-packages/flask_debugtoolbar/panels/profiler.py b/.venv/Lib/site-packages/flask_debugtoolbar/panels/profiler.py
--- a/.venv/Lib/site-packages/flask_debugtoolbar/panels/profiler.py 
+++ b/.venv/Lib/site-packages/flask_debugtoolbar/panels/profiler.py (date 1727604243564)
@@ -27,6 +27,8 @@
             self.dump_filename = current_app.config.get(
                 "DEBUG_TB_PROFILER_DUMP_FILENAME"
             )
+        else:
+            self.dump_filename = None

     def has_content(self):
         return bool(self.profiler)
jeffwidman commented 1 week ago

Feel free to submit a PR with your suggested fix.