Page 1 of 1

CFFI

Posted: Tue Dec 05, 2017 6:27 pm
by just brew it!
https://cffi.readthedocs.io/en/latest/

Had not heard of this package until a few days ago. Now I need to debug some broken code that uses it. I have to say, it's an interesting hack. And it appears to be the answer to the question "How the heck is a Python program managing to corrupt the C heap and trigger segfaults?"

Re: CFFI

Posted: Tue Dec 05, 2017 10:43 pm
by notfred
Oh dear $Deity. Looks like another "Just because you can doesn't mean that you should!"

Re: CFFI

Posted: Tue Dec 05, 2017 11:10 pm
by just brew it!
notfred wrote:
Oh dear $Deity. Looks like another "Just because you can doesn't mean that you should!"

I used that exact line when describing what it does to a co-worker today.

Near as I can tell, it has two modes of operation, Method A shells out to gcc to compile the glue code to a native .so which then gets loaded. AFAICT method B interprets C data structure and function declarations, and generates the C calls to the native library on the fly.

Rube Goldberg would be proud.

Re: CFFI

Posted: Wed Dec 06, 2017 11:02 am
by morphine
You know, I fully agree with the assessment. And yet, I bet that this thing has been like super-mega-incredibly-useful to a number of people at some point. Probably explains the continued development.

Re: CFFI

Posted: Wed Dec 06, 2017 11:31 am
by just brew it!
morphine wrote:
You know, I fully agree with the assessment. And yet, I bet that this thing has been like super-mega-incredibly-useful to a number of people at some point. Probably explains the continued development.

Yup. But like any powerful tool, if you misuse it you're gonna get bitten on the a**. Hard. Like the project I'm trying to debug...

Re: CFFI

Posted: Wed Dec 06, 2017 2:27 pm
by UberGerbil
Being able to call down to a lower level to get things done in a way that can't be done any other way, or can't be performant without it, has a history as old as languages higher than assembly. And the perception of it has always been "this is awesome" when it works and "just because you can doesn't mean you should" when it doesn't (and sometimes even when it does). C itself allows inline assembly, of course, and just about every-higher level language makes some provision for calling outside or under itself. Even sandboxed languages like Java have things like the JNI for situations where reality requires things the ideology doesn't otherwise allow.

Back in '91 or so I wrote a piece for the Visual Basic Programmer's Journal that showed VB 1.0 programmers how to call the Windows API directly. Considering this was Windows 3.1x and the programmers involved were often new to Windows programming, I wondered how many machines I crashed through that one article alone. "A little knowledge is dangerous, unless it involves programming, in which case it can be catastrophic"

Re: CFFI

Posted: Wed Dec 06, 2017 4:31 pm
by notfred
Right, but going from an interpreted scripting language to something like C, I was expecting something a bit more controlled like the TCL C API https://wiki.tcl.tk/11153

Re: CFFI

Posted: Thu Dec 07, 2017 11:33 am
by FlamingSpaceJunk
Python can be extended via C or C++ (https://docs.python.org/3/extending/extending.html) as well, and that's really the proper way to use C/C++ code in Python.

CFFI is just quick and dirty. If you don't have the skills or the time to write a proper module yet still need to use a C library, CFFI. :)

Re: CFFI

Posted: Thu Dec 07, 2017 11:36 am
by just brew it!
FlamingSpaceJunk wrote:
Python can be extended via C or C++ (https://docs.python.org/3/extending/extending.html) as well, and that's really the proper way to use C/C++ code in Python.

CFFI is just quick and dirty. If you don't have the skills or the time to write a proper module yet still need to use a C library, CFFI. :)

Unfortunately you do need to have at least a modicum of C skills even to use CFFI. Otherwise you end up with code that crashes a lot due to segfaults and heap corruptions. Looks like I get to rewrite all the problematic CFFI stuff in this project.