Re: [capi-sig] Problem with reference
On 2009-03-03 10:38, Stefan Behnel wrote:
Hrvoje Niksic wrote:
Stefan Behnel writes:
pycnweb wrote:
I am now writing a python extension, but I confuse with the reference. If you care more about working code than fighting confusion, Cython might be worth a look. Stefan, please stop doing this. This list is about helping people using Python/C, not Cython advocacy.
Where is the difference?
The list would be called cython-sig otherwise :-)
There are many other alternatives to approaching Python extension writing in C/C++:
- use ctypes
- use Boost.Python
- use SWIG
- use sip
- use PyCXX
- use PyRex/Cython
- use Robin
- ...
(there are lots more)
However, this list is about the layer underneath all of those. It's about the Python C API and there are many good reasons to use it directly rather than using one of the above wrappers or wrapper generators.
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Mar 03 2009)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
M.-A. Lemburg wrote:
On 2009-03-03 10:38, Stefan Behnel wrote:
Hrvoje Niksic wrote:
Stefan Behnel writes:
pycnweb wrote:
I am now writing a python extension, but I confuse with the reference. If you care more about working code than fighting confusion, Cython might be worth a look. Stefan, please stop doing this. This list is about helping people using Python/C, not Cython advocacy.
Where is the difference?
The list would be called cython-sig otherwise :-)
There are many other alternatives to approaching Python extension writing in C/C++:
- use ctypes
- use Boost.Python
- use SWIG
- use sip
- use PyCXX
- use PyRex/Cython
- use Robin
- ...
(there are lots more)
I know almost all of these at least by name. However, most of them are wrapper generators, which clearly limits their field of applicability (and rarely leads to good wrappers without major manual interaction). Cython is not a wrapper generator. It's a programming language that sits right between Python and C and generates code for the C-API of Python. That's why its related to this mailing list and why I advocate it to people who are clearly starting to write code against that API. It just makes their life a lot easier without sacrificing anything.
It's similar to what the Jython developers say, "we write C so you don't have to". If you ever need to make a direct call to Python's C-API in Cython code, it's one cimport away. In all other cases, it's better to concentrate on real logic and algorithmic optimisations in working code, than on counting references, converting types, building dicts and writing extension class structs. It's pretty close to Python's own spirit in that regard. Plus, no matter how long you grow your code, Cython just doesn't start to get in your way - which has always been the case for any wrapper generator I've ever worked with.
I do see ctypes as a pure-Python-level alternative, but it's not related to this list in any obvious way.
However, this list is about the layer underneath all of those. It's about the Python C API and there are many good reasons to use it directly rather than using one of the above wrappers or wrapper generators.
I agree regarding the wrapper generators, but I do not agree regarding the need to call Python's C-API. I know that from personal experience as the author of lxml (which is written in Cython). lxml's code uses direct C-API calls in quite a few places, but that's almost purely for historical reasons. Today, most of that has become a premature performance optimisation that only makes the code less obvious. Last year, I started refactoring that back into Python/Cython code, because in 95% of the code, there just isn't any reason left to use direct C-API calls. Plain Cython code is at least as fast and often faster than hand-written code, especially when it comes to interaction with the Python runtime.
Call me a burnt child, I hope this makes it understandable that I'm trying to keep new C-API users from investing tons of time into their C code, just to notice that it will finally run, say, 30% slower and be less portable than what they could have had with 10-15% of the code size in Cython. And since the factor of 10 in code size is very real, it leaves you with 80-95% of the plain-C development time to benchmark and optimise your code.
Stefan
2009/3/3 M.-A. Lemburg <mal@egenix.com>
On 2009-03-03 10:38, Stefan Behnel wrote:
Hrvoje Niksic wrote:
Stefan Behnel writes:
pycnweb wrote:
I am now writing a python extension, but I confuse with the reference. If you care more about working code than fighting confusion, Cython might be worth a look. Stefan, please stop doing this. This list is about helping people using Python/C, not Cython advocacy.
Where is the difference?
The list would be called cython-sig otherwise :-)
There are many other alternatives to approaching Python extension writing in C/C++:
- use ctypes
- use Boost.Python
- use SWIG
- use sip
- use PyCXX
- use PyRex/Cython
- use Robin
- ...
And,
* PyBindGen
:-)
And Cython *is* a wrapper generator. I clearly remember seeing (ugly) C code generated by Cython, before I decided to write PyBindGen.
(there are lots more)
However, this list is about the layer underneath all of those. It's about the Python C API and there are many good reasons to use it directly rather than using one of the above wrappers or wrapper generators.
Agreed!
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Mar 03 2009)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
-- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
Gustavo Carneiro wrote:
And Cython *is* a wrapper generator.
Well, no, but PyBindGen is, now that you mention it. It looks a bit like a FFI (like ctypes), but it generates the corresponding wrapper code, if I'm not mistaken. Using it for anything but wrapping C/C++ code to make it callable in Python would be futile.
Cython is (literally) not a wrapper generator as it will not generate a wrapper for you. You have to write it yourself. Cython is a programming language to write extension modules for CPython, as generic as that. It focuses on Python-like simplicity, C-like speed and easy interfacing with both CPython and C (while better C++ and Fortran support is still being fledged out). It is being used to write wrappers, but it is also being used to write high-performance computational code, with or without interfacing to external C libraries. In the second line, it's also heading to become a fully-compliant Python compiler (a 1.0 goal), but that doesn't belong here (just like the automatic-compile-on-import support for Python files *wink*).
I clearly remember seeing (ugly) C code generated by Cython, before I decided to write PyBindGen.
Let's not start a discussion about taste here. Readable, commented, easy to debug C code is an obvious goal of the Cython project, simply because its developers are those who debug it most of the time. That said, we generally tell our users to look at the highlighted HTML page that Cython generates of their source code, so that they can focus more easily on those parts of the generated C code that are worth optimising. Having to read the entire C code file just to see where things go wrong is not a good idea.
Stefan
participants (3)
-
Gustavo Carneiro
-
M.-A. Lemburg
-
Stefan Behnel