【问题标题】:conditional `ctypedef` with Cython带有 Cython 的条件 `ctypedef`
【发布时间】:2010-08-23 01:44:14
【问题描述】:

我需要在我正在编写的一些包装代码中从stdint.h 访问uint64_t typedef,但我不知道如何完成它。问题是,从我从文档中可以看出,我的 ctypedef 必须采用以下形式:

ctypedef unsigned long uint64_t

ctypedef unsigned long long uint64_t

取决于 bits/wordsize.h 中的 WORDSIZE 是 64 还是 32。我无法找出如何从 Cython 访问此预处理器定义,如果可以的话,Cython 似乎不喜欢ctypedef 语句在 if 语句中,当我尝试将 if 语句放在 cdef 块中时,似乎将它与声明混淆了。有任何想法吗?希望我只是在这里遗漏了一些非常基本的东西。

【问题讨论】:

    标签: python c cython


    【解决方案1】:
    cdef extern from "stdint.h":
        ctypedef unsigned long long uint64_t
    

    任何ctypedefextern 都不会在.c 文件中生成typedef。 Cython 将包含 stdint.h 并且您的 C 编译器将从那里使用实际的 typedef。

    提供的类型唯一重要的是 cython 生成自动在 C 类型和 Python 类型之间转换的代码。使用 unsigned long long 意味着 Cython 将使用 PyLong_FromUnsignedLongLongPyLong_AsLongLongAndOverflow。这样,您希望不会在转换时出现任何截断。

    【讨论】:

    • 完美。这正是我在此期间使用的。我不需要改变任何东西。现在我想起来应该检查生成的 C 文件。
    【解决方案2】:

    Cython 已经在模块 libc.stdint 中包含这些定义:

    from libc cimport stdint
    
    ctypedef stdint.uint64_t foo
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多