setting C++ mode in cythonize()

437 views
Skip to first unread message

Stefan Behnel

unread,
Oct 3, 2014, 3:27:54 AM10/3/14
to Cython-users, Cython-devel
Hi,

the documentation suggests to pass language='c++' into cythonize() to
globally enable C++ mode, but AFAICT, this never actually worked. Cython
previously ignored this option, Cython 0.21 rejects it, and we didn't
update the docs, so the current situation is outright confusing.

http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#specify-c-language-in-setup-py

To me, this option seems the one obvious way to do it, as it mimics the
distutils Extension API most closely. So, I now implemented support for
this, but only for modules found by file name, not for user provided
Extension objects. I think that's a reasonable restriction. If users
construct their own Extension instances anyway, they can just as well set
the 'language' option on them directly (and probably already do so).

https://github.com/cython/cython/commit/88df97f250073d8a06f965d6a7e09d8fe6f4ca63

Stefan

Matthew Honnibal

unread,
Oct 4, 2014, 9:37:23 AM10/4/14
to cython...@googlegroups.com, cython...@python.org, stef...@behnel.de


On Friday, 3 October 2014 17:27:54 UTC+10, Stefan Behnel wrote:
Hi,

the documentation suggests to pass language='c++' into cythonize() to
globally enable C++ mode, but AFAICT, this never actually worked. Cython
previously ignored this option, Cython 0.21 rejects it, and we didn't
update the docs, so the current situation is outright confusing.

http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#specify-c-language-in-setup-py


Thanks for this. Cython's a big project that's been changing fast, and as a user I've often found the docs a bit lacking.

For instance, I wanted to use language="c++" for pyximport, and had some trouble doing so. I found this answer on StackOverflow:

https://stackoverflow.com/questions/7620003/how-do-you-tell-pyximport-to-use-the-cython-cplus-option

It recommends the use of a "foo.pyxbld" file, in the same directory as "foo.pyx", which pyximport will read for options. I tried it, and it does indeed work. This blew me away --- I'm fairly sure it's never mentioned in the docs, and it left me with many questions...

My main question is: is this actually the recommended solution? Or is it there for historical reasons, e.g. via Pyrex?

Björn Dahlgren

unread,
Oct 7, 2014, 6:58:07 AM10/7/14
to cython...@googlegroups.com, cython...@python.org, stef...@behnel.de

For instance, I wanted to use language="c++" for pyximport, and had some trouble doing so. I found this answer on StackOverflow:

https://stackoverflow.com/questions/7620003/how-do-you-tell-pyximport-to-use-the-cython-cplus-option

It recommends the use of a "foo.pyxbld" file, in the same directory as "foo.pyx", which pyximport will read for options. I tried it, and it does indeed work. This blew me away --- I'm fairly sure it's never mentioned in the docs, and it left me with many questions...

My main question is: is this actually the recommended solution? Or is it there for historical reasons, e.g. via Pyrex?
 

I've never see nthat either. I think it would be great if pyximport looks for lines like:

 # distutils: language = c++

in the pyx file.

Stefan Behnel

unread,
Oct 7, 2014, 7:48:51 AM10/7/14
to cython...@googlegroups.com
Björn Dahlgren schrieb am 07.10.2014 um 12:58:
> I think it would be great if pyximport looks
> for lines like:
>
> # distutils: language = c++
>
> in the pyx file.

Pyximport only marginally changed since the old days of Pyrex. It should
use cythonize() for compilation.

We certainly take patches.

Stefan

Robert Bradshaw

unread,
Oct 7, 2014, 11:53:25 AM10/7/14
to cython...@googlegroups.com
On Tue, Oct 7, 2014 at 4:48 AM, Stefan Behnel <stef...@behnel.de> wrote:
> Björn Dahlgren schrieb am 07.10.2014 um 12:58:
>> I think it would be great if pyximport looks
>> for lines like:
>>
>> # distutils: language = c++
>>
>> in the pyx file.
>
> Pyximport only marginally changed since the old days of Pyrex. It should
> use cythonize() for compilation.

+1

> We certainly take patches.

Contribution to docs are also well accepted; often as developers we're
not in the best position to know what is and isn't obvious. You can
even edit them right online:

https://github.com/cython/cython/blob/master/docs/src/userguide/wrapping_CPlusPlus.rst

- Robert
Reply all
Reply to author
Forward
0 new messages