Bug in Cython with .py file

770 views
Skip to first unread message

Ian Bell

unread,
Apr 27, 2012, 12:53:51 AM4/27/12
to cython-users
I have a Pure Python module that I need to keep in pure-Python for
readability and maintainability for my clients/users. In the process
I have run into a bug with Cython. Here is a simple example of what
doesn't work for me. If I have a .py file with only the contents

A.py:
--------
cdef extern from "math.h":
double sin(double)

with the setup file setup.py:
--------------------------------------
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import sys
if len(sys.argv)==1:
sys.argv+=['build_ext','--inplace']

ext_modules = [Extension("A", ["A.py"])]

setup(
name = 'Hello world app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

it won't compile and gives me an error

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "math.h":
^
------------------------------------------------------------

A.py:1:5: Syntax error in simple statement list

But if I change the file name to A.pyx and change the setup.py file
accordingly (A.py --> A.pyx), it compiles without a problem. Any idea
what is going on? In principle you should be able to give the files
whatever extension you want, right?

Thanks for any light you can shed.

Regards,
Ian

Stefan Behnel

unread,
Apr 27, 2012, 3:42:44 AM4/27/12
to cython...@googlegroups.com
Ian Bell, 27.04.2012 06:53:
> I have a Pure Python module that I need to keep in pure-Python for
> readability and maintainability for my clients/users. In the process
> I have run into a bug with Cython. Here is a simple example of what
> doesn't work for me. If I have a .py file with only the contents
>
> A.py:
> --------
> cdef extern from "math.h":
> double sin(double)

This is not valid Python syntax. Just run the Python interpreter over it
and it will give you a SyntaxError.
Cython determines the type of file it compiles from the filename extension
and adapts the syntax (and semantics) accordingly. So, no, a .py file and a
.pyx file are not the same thing and you cannot use Cython syntax in a
Python source file.

Stefan

Ian Bell

unread,
Apr 27, 2012, 3:53:00 AM4/27/12
to cython...@googlegroups.com
Stefan,

Sorry I should have worded my question better.  Definitely cdef extern... is not valid CPython syntax.  My question is more about the compilation of a file using Cython.  From the standpoint of Cython, you should be able to call the file whatever you like, should you not?  In principle I should be able to call the file A.abc and then compile the file using distutils, right?  Assuming that I make the necessary changes to setup.py

Thanks,
Ian

Stefan Behnel

unread,
Apr 27, 2012, 4:21:10 AM4/27/12
to cython...@googlegroups.com
Hi,

please don't top-post.


Ian Bell, 27.04.2012 09:53:
> Sorry I should have worded my question better. Definitely cdef extern...
> is not valid CPython syntax. My question is more about the compilation of
> a file using Cython. From the standpoint of Cython, you should be able to
> call the file whatever you like, should you not?

No. Cython can compile Python code and it can compile Cython code, which
are not the same thing. Python syntax is a subset of the Cython syntax, so
you can use any Python code file as Cython code file (minus compiler bugs),
but you cannot use an arbitrary Cython code file as Python code file, and
both Python and Cython should tell you that if you try. Thus, the
distinction between the two can only safely be done based on the filename
extension. Essentially, that's what filename extensions are there for: to
provide a visible distinction between different file types.


> In principle I should be
> able to call the file A.abc and then compile the file using distutils,
> right? Assuming that I make the necessary changes to setup.py

I really don't understand why you would want to do this. Would you name
your PDF files "text.guesswhatiam" instead of "text.pdf"? Or rename your .c
files to .py to be able to pass them into a Python interpreter?

Stefan

Ian Bell

unread,
Apr 27, 2012, 4:41:46 AM4/27/12
to cython...@googlegroups.com
Stefan,

Sorry but what is top-post?  I have naively replied to you in GMail, it seems to post to cython...@googlegroups.com as well.

My basic question is why the same (valid Cython) code in a file called A.pyx can go through distutils without a problem, but if I put the same code in a file called A.py it doesn't work.  Is there something Windows specific that I am missing?

Thanks,
Ian

Stefan Behnel

unread,
Apr 27, 2012, 4:47:50 AM4/27/12
to cython...@googlegroups.com
Ian Bell, 27.04.2012 10:41:
> Sorry but what is top-post?

https://duckduckgo.com/?q=top-posting

> My basic question is why the same (valid Cython) code in a file called
> A.pyx can go through distutils without a problem, but if I put the same
> code in a file called A.py it doesn't work. Is there something Windows
> specific that I am missing?
> [...]
>> No. Cython can compile Python code and it can compile Cython code, which
>> are not the same thing. Python syntax is a subset of the Cython syntax, so
>> you can use any Python code file as Cython code file (minus compiler bugs),
>> but you cannot use an arbitrary Cython code file as Python code file, and
>> both Python and Cython should tell you that if you try. Thus, the
>> distinction between the two can only safely be done based on the filename
>> extension. Essentially, that's what filename extensions are there for: to
>> provide a visible distinction between different file types.

Please ask back if I was unclear here.

Stefan

Ian Bell

unread,
Apr 27, 2012, 4:59:59 AM4/27/12
to cython...@googlegroups.com

Thanks, that link is clear.  I'll stop top-posting in the future.

Ian

Reply all
Reply to author
Forward
0 new messages