Edgewall Software

Opened 8 years ago

Last modified 6 years ago

#12352 closed enhancement

Log a warning when interpreter optimization level is nonzero — at Version 5

Reported by: Ryan J Ollos Owned by: Ryan J Ollos
Priority: normal Milestone: 1.2
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

EnvironmentError is raised when Python interpreter optimization level is nonzero.

API Changes:
Internal Changes:

Description

Documented in #8956 and recently raised in gmessage:trac-users:AQpVWTXVwsg/Rkey-2SHFQAJ, the header and footer are non displayed when Python interpreter optimizations are enabled.

The issue can be reproduced with TracStandalone using the PYTHONOPTIMIZE environment variable.

$PYTHONOPTIMIZE=1 tracd -r -s -p 8000 proj-1.0

The optimization level can be retrieved at runtime, so we could log a warning, raise an error or display the optimization level in the System Information on the About Trac page.

$python -OO
Python 2.7.11 (default, Dec  5 2015, 14:44:53) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags.optimize
2
$python
Python 2.7.11 (default, Dec  5 2015, 14:44:53) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags.optimize
0

Change History (5)

comment:1 by Ryan J Ollos, 8 years ago

I'm unsure of the best location to do this check. Here is a proposed patch, which puts the check next to warn_setuptools_issue:

  • trac/web/main.py

    diff --git a/trac/web/main.py b/trac/web/main.py
    index 84a881f..dfcbadb 100644
    a b class RequestDispatcher(Component):  
    391391        return resp
    392392
    393393
    394 _warn_setuptools = False
     394_initialization_checks = False
    395395_slashes_re = re.compile(r'/+')
    396396
    397397def dispatch_request(environ, start_response):
    def dispatch_request(environ, start_response):  
    401401    :param start_response: the WSGI callback for starting the response
    402402    """
    403403
    404     global _warn_setuptools
    405     if _warn_setuptools is False:
    406         _warn_setuptools = True
     404    global _initialization_checks
     405    if _initialization_checks is False:
    407406        warn_setuptools_issue(out=environ.get('wsgi.errors'))
    408407
     408        if sys.flags.optimize != 0:
     409            raise EnvironmentError("Python with optimizations enabled is not "
     410                                   "supported.")
     411        _initialization_checks = True
     412
    409413    # SCRIPT_URL is an Apache var containing the URL before URL rewriting
    410414    # has been applied, so we can use it to reconstruct logical SCRIPT_NAME
    411415    script_url = environ.get('SCRIPT_URL')

The EnvironmentError is used because it's also used here: tags/trac-1.0.10/trac/web/main.py@:486-490#L441. However, this results in an empty response.

Another idea is to raise an exception in the Environment initializer.

Last edited 8 years ago by Ryan J Ollos (previous) (diff)

in reply to:  1 comment:2 by Ryan J Ollos, 8 years ago

Replying to Ryan J Ollos:

Another idea is to raise an exception in the Environment initializer.

I'm tending to favor this approach because I think the error message will be more clear.

comment:3 by Jun Omae, 8 years ago

Raising an EnvironmentError sounds good. I consider we should check sys.flags.optimize every time rather than first time. If checking only first time, it might be ignored. Also, it would be good to add the same check to run() in trac/admin/console.py.

comment:4 by Ryan J Ollos, 8 years ago

Owner: set to Ryan J Ollos
Status: newassigned

comment:5 by Ryan J Ollos, 8 years ago

Release Notes: modified (diff)
Note: See TracTickets for help on using tickets.