Re: [cython-users] Some questions, maybe naive ones

96 views
Skip to first unread message

Yichao Yu

unread,
Sep 28, 2012, 2:17:37 AM9/28/12
to cython...@googlegroups.com
On Fri, Sep 28, 2012 at 12:39 AM, JBT <jianb...@gmail.com> wrote:
> Hi,
>
> I am new to cython. I am currently trying to make a python wrapper to use
> NASA CDF library. This is currently for practicing purposes, but hopefully
> it will eventually work out and turn into something useful.
>
> Anyway, for now I am trying to translate the following c codes to cython
> codes.
> ------------------------ below is from cdf.h
> ------------------------------------------------
> typedef void *CDFid;
> typedef long CDFstatus;
>
> static double *TT2000NULL = 0;
>
> #define HOST_ENCODING 8L
> #define ROW_MAJOR 1L
> #define SINGLE_FILE 1L
>
>
> ----------------------------- below is from an example, say, example.c
> -----------------------
> CDFid test_id; /* CDF identifier (handle). */
> CDFstatus status; /* Status returned from CDF library.
> */
> static char CDFname[] = {"test1"}; /* File name of the CDF. */
> long numDims = 2; /* Number of dimensions. */
> static long dimSizes[2] = {100,200}; /* Dimension sizes. */
> long encoding = HOST_ENCODING; /* Data encoding. */
> long majority = ROW_MAJOR; /* Variable data majority. */
> long form = SINGLE_FILE; /* Format of CDF. */
>
>
> Below is my translation.
> ------------------------------ below is from nasacdf.pxd
> ----------------------------
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid;
> ctypedef long CDFstatus;
> static double *TT2000NULL = 0;
> enum:
> HOST_ENCODING = 8;
> ROW_MAJOR = 1;
> SINGLE_FILE = 1;
>
> -------------------------------below is from test.pyx
> ------------------------------
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> cdef CDFstatus status
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> cdef long form = SINGLE_FILE
>
> print CDFname
>
> ----------------------------- below is my setup.py ---------------------
> from distutils.core import setup
> from distutils.extension import Extension
> from Cython.Distutils import build_ext
>
> cdfdist = 'cdf34_1-dist/'
> modules = Extension("cdf",
> sources = ['test.pyx'],
> libraries = [cdfdist + 'lib/libcdf.a'],
> extra_compile_args = ['-D_FILE_OFFSET_BITS=64', '-D_LARGEFILE64_SOURCE',
> '-D_LARGEFILE_SOURCE'])
>
> setup(name = 'pyNASACDF',
> version = '0.1.0',
> cmdclass = {'build_ext':build_ext},
> ext_modules = [modules]
> )
>
> ---------------------------------------- end of code
> --------------------------------------
> I modified the CDFname declaration and commented out dimSizes declaration
> because the compiler issued errors on them. After I run:
> python setup.py build_ext --inplace
> I got errors. I attached the error message below. Can anyone tell me what is
> wrong with my translation, please? Thank you very much.
>
>>$ python setup.py build_ext --inplace
> running build_ext
> cythoning test.pyx to test.c
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # File: nasacdf.pxd
>
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid;
> ^

remove all your semicolon

> ------------------------------------------------------------
>
> nasacdf.pxd:4:24: Syntax error in ctypedef statement
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # File test.pyx
>
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> ^
> ------------------------------------------------------------
>
> test.pyx:6:9: 'CDFid' is not a type identifier
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
>
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> cdef CDFstatus status
> ^
> ------------------------------------------------------------
>
> test.pyx:7:9: 'CDFstatus' is not a type identifier
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef CDFstatus status
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> ^
> ------------------------------------------------------------
>
> test.pyx:12:45: undeclared name not builtin: HOST_ENCODING
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> ^
> ------------------------------------------------------------
>
> test.pyx:13:41: undeclared name not builtin: ROW_MAJOR
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> cdef long form = SINGLE_FILE
> ^
> ------------------------------------------------------------
>
> test.pyx:14:39: undeclared name not builtin: SINGLE_FILE
> building 'cdf' extension
> clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common
> -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX
> -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall
> -Wstrict-prototypes -DENABLE_DTRACE -pipe -arch i386 -arch x86_64
> -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
> -c test.c -o build/temp.macosx-10.8-intel-2.7/test.o -D_FILE_OFFSET_BITS=64
> -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE
> clang: warning: argument unused during compilation: '-mno-fused-madd'
> test.c:1:2: error: Do not use this file, it is the result of a failed Cython
> compilation.
> #error Do not use this file, it is the result of a failed Cython
> compilation.
> ^
> 1 error generated.
> error: command 'clang' failed with exit status 1
>
>

Stefan Behnel

unread,
Sep 28, 2012, 9:47:16 AM9/28/12
to cython...@googlegroups.com
JBT, 28.09.2012 06:39:
> ------------------------------ below is from nasacdf.pxd
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid;
> ctypedef long CDFstatus;
> static double *TT2000NULL = 0;
> enum:
> HOST_ENCODING = 8;
> ROW_MAJOR = 1;
> SINGLE_FILE = 1;
>
> -------------------------------below is from test.pyx
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> cdef CDFstatus status
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> cdef long form = SINGLE_FILE
>
> print CDFname

As normal in Python, you have to either prefix the names you use with the
module that you imported, or import the names that the module defines
directly into your code. I.e. either use

cimport nasacdf

cdef nasacdf.CDFid test_id

or

from nasacdf cimport CDFid

cdef CDFid test_id

or any mix of the two as you see fit. Cython is a lot closer to Python than
to C. It actually has namespaces.

Stefan

Yichao Yu

unread,
Sep 28, 2012, 12:45:31 PM9/28/12
to cython...@googlegroups.com, stef...@behnel.de
On Fri, Sep 28, 2012 at 12:32 PM, JBT <jianb...@gmail.com> wrote:
> Hi Stefan,
>
> Thank you. That helps a lot.
>
> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement
> to:
> from cdc cimport *
>
> Then I got this error:
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # File: cdf.pxd
>
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid
> ctypedef long CDFstatus
> static double *TT2000NULL = 0

don't think cython understand static (or I'm wrong?)
and u need a cdef here

cdef double *.... = NULL

works fine for me

> ^
> ------------------------------------------------------------
>
> cdf.pxd:6:18: Syntax error in C variable declaration
>
> As you can see, the *static double* statement is copied from cdf.h, so I
> don't understand why cython sees that as an error. Any ideas? Thank you so
> much.
>
> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
That is where the original syntax error coming from. afaik

>
> JBT

Stefan Behnel

unread,
Sep 28, 2012, 12:53:10 PM9/28/12
to cython...@googlegroups.com
Yichao Yu, 28.09.2012 18:45:
> On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement
>> to:
>> from cdc cimport *
>>
>> Then I got this error:
>> Error compiling Cython file:
>> ------------------------------------------------------------
>> ...
>> # File: cdf.pxd
>>
>> cdef extern from "cdf34_1-dist/include/cdf.h":
>> ctypedef void *CDFid
>> ctypedef long CDFstatus
>> static double *TT2000NULL = 0
>
> don't think cython understand static (or I'm wrong?)
> and u need a cdef here
>
> cdef double *.... = NULL
>
> works fine for me

The "cdef" isn't needed when you're inside of a "cdef extern" block anyway.


>> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
> That is where the original syntax error coming from. afaik

No, semicolons work the same way as in Python.

Stefan

Yichao Yu

unread,
Sep 28, 2012, 1:03:26 PM9/28/12
to cython...@googlegroups.com
On Fri, Sep 28, 2012 at 12:53 PM, Stefan Behnel <stef...@behnel.de> wrote:
> Yichao Yu, 28.09.2012 18:45:
>> On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>>> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement
>>> to:
>>> from cdc cimport *
>>>
>>> Then I got this error:
>>> Error compiling Cython file:
>>> ------------------------------------------------------------
>>> ...
>>> # File: cdf.pxd
>>>
>>> cdef extern from "cdf34_1-dist/include/cdf.h":
>>> ctypedef void *CDFid
>>> ctypedef long CDFstatus
>>> static double *TT2000NULL = 0
>>
>> don't think cython understand static (or I'm wrong?)
>> and u need a cdef here
>>
>> cdef double *.... = NULL
>>
>> works fine for me
>
> The "cdef" isn't needed when you're inside of a "cdef extern" block anyway.
true....

>
>
>>> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
>> That is where the original syntax error coming from. afaik
>
> No, semicolons work the same way as in Python.
well not for cdef, (as in Python def)

i.e. not here
cdef extern from "cdf34_1-dist/include/cdf.h":
ctypedef void *CDFid;
^
------------------------------------------------------------

nasacdf.pxd:4:24: Syntax error in ctypedef statement


>
> Stefan
>

Chris Barker

unread,
Sep 28, 2012, 3:36:41 PM9/28/12
to cython...@googlegroups.com
On Fri, Sep 28, 2012 at 10:32 AM, JBT <jianb...@gmail.com> wrote:

> So, here is another question. Cython won't compile these two statements:
> cdef static char CDFname[] = {"test1"}
> cdef static long dimSizes[2] = {100,200}
> because cython sees them with syntax error. I found the corresponding c code
> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
> counterpart is correct. So, why does cython think the two statements are not
> correct?

becaseu that is how you initialize arrays in C, not Python (or Cython)
-- remember that Cyton is closer to Python is syntax than C. For
instance, the {} are used to defien a dcitionaly literal.

try:

cdef static char CDFname = "test1"
cdef static long dimSizes[2] = [100, 200]

(though you may need a char* there -- a char is one charactor.

(unteseted, and I'm no expert, but this is closer, for sure.)

-Chris






> JBT
--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Stefan Behnel

unread,
Sep 28, 2012, 3:43:30 PM9/28/12
to cython...@googlegroups.com
Chris Barker, 28.09.2012 21:36:
> On Fri, Sep 28, 2012 at 10:32 AM, JBT wrote:
>> So, here is another question. Cython won't compile these two statements:
>> cdef static char CDFname[] = {"test1"}
>> cdef static long dimSizes[2] = {100,200}
>> because cython sees them with syntax error. I found the corresponding c code
>> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
>> counterpart is correct. So, why does cython think the two statements are not
>> correct?
>
> becaseu that is how you initialize arrays in C, not Python (or Cython)
> -- remember that Cyton is closer to Python is syntax than C. For
> instance, the {} are used to defien a dcitionaly literal.
>
> try:
>
> cdef static char CDFname = "test1"
> cdef static long dimSizes[2] = [100, 200]
>
> (though you may need a char* there -- a char is one charactor.
>
> (unteseted, and I'm no expert, but this is closer, for sure.)

Cython has no keyword "static" because all globally declared C names are
static anyway.

Stefan

Jianbao Tao

unread,
Sep 28, 2012, 5:42:56 PM9/28/12
to cython...@googlegroups.com
Thanks, Stefan, Chris.

So, now, the char statement can go through, but the dimSizes one still can't. Here is my current version:
cdef long dimSizes[2] = [100, 200]

Cython still consider this with syntax error, which is quite confusing to me. Any ideas?

JBT

Yichao Yu

unread,
Sep 28, 2012, 1:38:55 PM9/28/12
to cython...@googlegroups.com, stef...@behnel.de
On Fri, Sep 28, 2012 at 1:32 PM, JBT <jianb...@gmail.com> wrote:
> Thanks, Yichao, Stefan.
>
> So, here is another question. Cython won't compile these two statements:
> cdef static char CDFname[] = {"test1"}
> cdef static long dimSizes[2] = {100,200}
> because cython sees them with syntax error. I found the corresponding c code
> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
> counterpart is correct. So, why does cython think the two statements are not
> correct?

same with this one?
http://stackoverflow.com/questions/8320951/can-i-create-a-static-c-array-with-cython

>
> JBT
Reply all
Reply to author
Forward
0 new messages