9

I have been asking a few questions regarding the nuances of Cython and how to integrate it with C++. So I am not clear on what constitutes a minimum reproducible verifiable example of Cython as I am concerned the question will get to be very big

Here is a list of what I think is necesssary

  1. Minimal header file from C++

  2. Minimal .cpp code from C++

  3. The header file for cython .pxd file

  4. The relevant .pyx file

  5. The Python code that you need to integrate it with

  6. The setup.py file that one uses to compile

  7. The error that one is experiencing i.e. compilation error

As you can see this is already a long list of things to show to the SO community and I obviously do not want to show more than what is necessary.

I am hoping the Cython/Python/C/C++ experts can guide me on what is adequate. Just today I stripped off a lot of information for a question and got myself downvoted because it is not a minimum reproducible verifiable example. But my concern was my question was already too big.

2 Answers 2

10

Too big is better than incomplete. The main point is to make sure that we can take the exact code you used and compile it.

The trouble with leaving stuff out is that there's a good chance that a) the error is in what's left out and b) the answerer will get bored of "filling in the gaps" before they get to the answer.


What follows is specifically Cython related so doesn't apply too much to making an MRE in other languages:

You can usually cut a lot of it out.

  • C/C++ code can be included directly in your Cython file using

    cdef extern from *:
        """
        C/C++ code here is included directly in the source
        """
    

    This can often eliminate both the C/C++ header and source file (remember that you don't have to have a separate declaration and definition - just include the definition). It also makes the compilation simpler. Even if you don't do this, it's often worth putting code inline in the header to save having to compile+link a separate C file.

  • .pxd files are used similarly to C/C++ headers - to share definitions between multiple source .pyx files. If you only have one .pyx file then you aren't sharing definitions so the contents of the .pxd file can go in the .pyx. (This is really the same as with .h and .c files)

  • If you can get it down to one source file then it can be compiled on the command-line cythonize -i name_of_sourcefile.pyx. This saves reproducing a setup.py. Do mention that you've done it this way.

The other things you mention are generally a good idea to include, especially the error message.

The final point worth making is that Cython has a three step process: 1) Use Cython to generate C code; 2) Compile the C code to a Python module; 3) Run that Python module. If the error is happening in step 1 then you don't necessarily need to include any C headers you use, or the Python file that you've eventually use to test it. (This information may be useful for context though).


Other people have it harder though: for writing graphics shaders there's a huge amount of boring setup code needed - I'm sure I've seen a meta question about this issue but I can't find it right now. Cython is "not that bad" in terms of code length for an MRE.

0
9

I am concerned the question will get to be very big

There's usually no need to be concerned about this.

A minimum reproducible and verifyable example means exactly what it says.

Include all code which is necessary that your problem can be reproduced out of the box by anyone else.
If there's any irrelevant code in your original stuff, just remove it.

Doing that process to isolate your problem might also be helpful to solve it yourself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .