Skip to content

Options.emit_code_comments is not used #2740

@yuriescl

Description

@yuriescl

Setting Cython.Compiler.Options.emit_code_comments = False does not work when generating .c code from .py files.

from distutils.core import setup
from Cython.Distutils import build_ext
from Cython.Compiler import Options
from Cython.Build import cythonize

Options.emit_code_comments = False  # not being applied

...

setup(
    ext_modules=cythonize("mymodule.py")
    cmdclass={'build_ext': build_ext}
)

The generated .c file contains the code comments, which should be disabled since emit_code_comments is set to false.

Code.py:1856:

def emit_marker(self):
        pos, trace = self.last_pos
        self.last_marked_pos = pos
        self.last_pos = None
        self.write("\n")
        if self.code_config.emit_code_comments:
            self.indent()
            self.write("/* %s */\n" % self._build_marker(pos))

self.code_config.emit_code_comments is True even when setting Options.emit_code_comments to False.

The documentation says it's an option but actually it's currently a compiler directive. Sounds like there was going to be a transition from being an option to being a compiler directive, but the option variable is still there and not being used.

Here's where the value is passed to the code config. It's obtained from the directives, not the Options.
ModuleNode.py:53:

return Code.CCodeConfig(
        emit_linenums=emit_linenums,
        emit_code_comments=env.directives['emit_code_comments'],
        c_line_in_traceback=options.c_line_in_traceback)

So I tried to set emit_code_comments as a compiler directive instead, and it worked as expected (no comments added to the .c file):

from distutils.core import setup
from Cython.Distutils import build_ext
from Cython.Compiler import Options
from Cython.Build import cythonize

compiler_directives = Options.get_directive_defaults()
compiler_directives["emit_code_comments"] = False  # works

...

setup(
    ext_modules=cythonize("mymodule.py",
                          compiler_directives=compiler_directives))
    cmdclass={'build_ext': build_ext}
)

Tested in Cython 0.29.1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions