3

I have a python program that uses OpenCV. The program runs as expected as it is at the moment. Now I would like to use Cython to compile my python code to C code. I am doing this instead of re-writing the entire program in C because I would still like other python programs to be able to import my_program.

I have never used Cython before but have just read few blog posts about it. Can someone please tell me what I should be prepared for and how much of an uphill task it would be.... My current python program is ~200 LoC.

Anthony
  • 33,838
  • 42
  • 169
  • 278
  • With a 200 line script, I'd just go ahead and try it, and see what issues (if any) I run into. Not saying the answers to this question wouldn't be useful in general. – Dan Mašek Apr 11 '16 at 04:37
  • @Anthony what are you hoping to achieve by doing this? Because you could probably compile your Python code using Cython with (almost?) no changes to it, but you wouldn't really have gained much. – DavidW Apr 11 '16 at 12:52
  • @DavidW The sole reason why I'm doing this is to obfuscate the code, which python doesn't allow. I understand that someone can still reverse engineer it but my goal is just to make it harder for them to reverse engineer it. I got this idea from this answer http://stackoverflow.com/a/7347168/44286 – Anthony Apr 11 '16 at 15:27
  • @Anthony In which case your code would probably work as is (you'd probably have to work quite hard to find the incompatibilities, although they do exist). I may (possibly) see if I can answer with a few of the more obvious incompatibilities at some point, maybe. – DavidW Apr 11 '16 at 15:54
  • @DavidW while doing more research I came across https://liftoff.github.io/pyminifier/ . Have you used it? It seems promising for my case where my sole purpose is to make reverse engineering the code "harder" but still be able to allow users to `import my_code` as any other module. – Anthony Apr 11 '16 at 15:59
  • @Anthony I wanted to ask you if the task you were trying to do worked out. – ArafatK Jun 05 '18 at 19:14
  • Also, do you think it's a good idea to build a relatively large application with Cython to be able to protect it. – ArafatK Jun 05 '18 at 19:15
  • Any comments are very welcome, thanks a lot. – ArafatK Jun 05 '18 at 19:15

1 Answers1

2

Based on your comments you're looking to run your existing code "as is" to avoid providing the source, rather than make any significant changes to use Cython-specific features. With that in mind I'd expect it to just work without any major effort. One easy alternative to consider would be to just provide pyc bytecode files.

A list of minor gotchas that I know of (in rough order of importance) follows. A few others are listed in the documentation. Most of these are fairly minor so you'd be unlucky to meet them.

  1. You will likely have to recompile your module for every platform, 32bit and 64bit, every (major, e.g. 3.4, 3.5) version of Python used, and possibly on Windows with multiple different compilers.

  2. You can't use __file__ at the module level. This is sometimes becomes an issue when trying to find the path of static resources stored in the same place as your code.

  3. A few things that try to do clever things by inspecting the stack (to see what variables are defined in the functions that called them) break, for example some of sympy and possibly some shortcuts to string formatting (see for example for some recipes that might use this idea)

  4. Anything that looks at the bytecode of functions (since it isn't generated by Cython). Numba is probably the most commonly used example in numerical python, but I know of at least one (unmaintained) MATLAB/Python wrapper that inspects the bytecode of the calling function to try to work out the number of arguments being returned.

  5. You must have an __init__.py file to make a folder into a module - it won't recognise a compiled __init__.so file on its own.

  6. String concatenation can go through a fast path in Python that Cython doesn't manage. You should not being doing this too much in your code anyway, but you may see large performance differences if you rely on it.

Community
  • 1
  • 1
DavidW
  • 29,336
  • 6
  • 55
  • 86