Is there a clean way to suppress compiler warnings from Cython when using pyximport.install? Is there a clean way to suppress compiler warnings from Cython when using pyximport.install? python python

Is there a clean way to suppress compiler warnings from Cython when using pyximport.install?


I've battled this exact thing myself (glad to know I'm not alone!) and have not found a perfect solution. Unfortunately pyximport is rather opaque and doesn't AFAICT have much in the way of customizability.

But I do have what I think is a reasonable workaround, which helps especially once you have a growing number of Cython modules.

Basically, I have a module somewhere (let's say common.cython) that contains something like this:

from distutils.extension import ExtensionDEFAULT_EXTENSION_KWARGS = {    "extra_compile_args": ["-w"]}def pyx_extension(**kwargs):    for key, value in DEFAULT_EXTENSION_KWARGS.items():        if key not in kwargs:            kwargs[key] = value    return Extension(**kwargs)def make_ext(modname, pyxfilename):    return pyx_extension(name=modname, sources=[pyxfilename])

Basically a thin wrapper around the distutils Extension class, where I can set some custom defaults.

Then next to pyx modules that don't need any custom building, I put a some_module.pyxbld with just one line:

from common.cython import make_ext

This works nicely because the .pyxbld file is just a python module that's expected to contain a make_ext function with that signature.

If I do need to customize the .pyxbld for the module, say if I need to add a C source file or something, it'll look something like this:

def make_ext(modname, pyxfilename):    from common.cython import pyx_extension    return pyx_extension(name=modname, sources=[pyxfilename, "my_extra_source.c"])

So, not hugely different from the basic documented way, but just enough to satisfy my DRY OCD :) Hope this helps, and please let me know if you find a better way.