Personal computing discussed

Moderators: SecretSquirrel, just brew it!

 
just brew it!
Gold subscriber
Administrator
Topic Author
Posts: 49673
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

CFFI

Tue Dec 05, 2017 6:27 pm

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?"
Nostalgia isn't what it used to be.
 
notfred
Maximum Gerbil
Posts: 4390
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: CFFI

Tue Dec 05, 2017 10:43 pm

Oh dear $Deity. Looks like another "Just because you can doesn't mean that you should!"
 
just brew it!
Gold subscriber
Administrator
Topic Author
Posts: 49673
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: CFFI

Tue Dec 05, 2017 11:10 pm

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.
Nostalgia isn't what it used to be.
 
morphine
Gold subscriber
Gerbilus Supremus
Posts: 11406
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: CFFI

Wed Dec 06, 2017 11:02 am

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.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
just brew it!
Gold subscriber
Administrator
Topic Author
Posts: 49673
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: CFFI

Wed Dec 06, 2017 11:31 am

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...
Nostalgia isn't what it used to be.
 
UberGerbil
Grand Admiral Gerbil
Posts: 10273
Joined: Thu Jun 19, 2003 3:11 pm

Re: CFFI

Wed Dec 06, 2017 2:27 pm

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"
 
notfred
Maximum Gerbil
Posts: 4390
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: CFFI

Wed Dec 06, 2017 4:31 pm

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
 
FlamingSpaceJunk
Gerbil
Posts: 11
Joined: Thu Nov 09, 2017 4:21 pm

Re: CFFI

Thu Dec 07, 2017 11:33 am

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. :)
 
just brew it!
Gold subscriber
Administrator
Topic Author
Posts: 49673
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: CFFI

Thu Dec 07, 2017 11:36 am

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.
Nostalgia isn't what it used to be.

Who is online

Users browsing this forum: No registered users and 0 guests