Question

I have a system where an application is started with:

python -OO my_app.py

I cannot change the way the application is started but I have full control of the application. When adding a parser to the application using PLY I was bitten by the fact that -OO removes the docstrings that PLY relies on.

Is it impossible to use PLY in an application which is started with -OO? Are there any clever Python tricks that can be used to solve this problem?

I have been thinking on using Cython to compile the parser module. I am not sure that it can be done, and it would add a layer of complexity, so before I go down that road I would appreciate any other ideas.

Was it helpful?

Solution

Ok, classic case of finding the answer just as the question was written...

It is clearly stated in the PLY manual that there is an optimize mode which is used for this exact use case. I thought it referred to another kind of performance optimization.

Note that since -OO removes docstrings, instantiating your lexer and parser with optimize=1 will not handle empty rules like the one below:

def p_commands(self, p):
    """commands :
                | commands command"""
    # This will fail when running optimized

def p_command(self, p):
    """command : foo
               | bar"""
    p[0] = p[1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top