Skip to content

ProfilerMiddleware's default filename_format causes ValueError when ProfilerMiddleware is used with profile_dir #1511

@jlove-bazaarvoice

Description

@jlove-bazaarvoice

Environment

$ sw_vers 
ProductName:	Mac OS X
ProductVersion:	10.13.6
BuildVersion:	17G3025

$ python --version
Python 3.7.2

$ pip freeze
Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.2

Basically, the only Python dependency I installed was Flask because that's what I'm most familiar with. However, the error I'm describing looks to be contained within werkzeug.

Observed Behavior

When using ProfilerMiddleware with its profile_dir argument, the following error gets raised after a request is sent to the server:

Error on request:
Traceback (most recent call last):
  File "/dev/jlove-bazaarvoice/werkzeug-profiler-bug/.venv/lib/python3.7/site-packages/werkzeug/serving.py", line 302, in run_wsgi
    execute(self.server.app)
  File "/dev/jlove-bazaarvoice/werkzeug-profiler-bug/.venv/lib/python3.7/site-packages/werkzeug/serving.py", line 290, in execute
    application_iter = app(environ, start_response)
  File "/dev/jlove-bazaarvoice/werkzeug-profiler-bug/.venv/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/dev/jlove-bazaarvoice/werkzeug-profiler-bug/.venv/lib/python3.7/site-packages/werkzeug/middleware/profiler.py", line 119, in __call__
    time=time.time(),
ValueError: Unknown format code 'd' for object of type 'float'

Expected Behavior

No ValueError.

Steps to Reproduce

  1. pip install flask
  2. Save the following file as app.py.
# app.py
from flask import Flask
from werkzeug.middleware.profiler import ProfilerMiddleware

app = Flask(__name__)
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, profile_dir=".")


@app.route("/", methods=["GET"])
def get_index():
    return "Hello, world!"
  1. Start the server with FLASK_APP=app.py flask run.
  2. Send a request to the server (e.g. http://127.0.0.1:5000/).

Workaround/Solution

Slightly modify ProfilerMiddleware's filename_format, replacing the d with f. For example:

app.wsgi_app = ProfilerMiddleware(
    app.wsgi_app, profile_dir=".", filename_format="{method}.{path}.{elapsed:06f}ms.{time:f}.prof"
)

Both instances of d need to be replaced because both elapsed and time are floating point numbers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions