English 中文(简体)
通过cy,向C提供 p
原标题:passing python strings, through cython, to C

我正试图用一些 c和一些on子书写一个单元。 我正在利用cy来弥合这一差距。

我想把我(非常长)的常数储存在平线上,因为许多nic子:

const char long_string = "
part of string
"
  "next part
"
  "last part
";

相对于:

long_string = """
part of string
next part
last part
"""

(the strings are much longer than this, and more complicated - to the extent that I don t want to have to add and remove the "s and "s every time I want to edit it with syntax highlighting. In fact, they are openCL kernels.)

我需要能够把这些内容变成使用方言的方言,根据。 我只需要这样做:

cdef bytes py_bytes = py_string.encode()
cdef char* c_string = py_bytes

只要我提到<代码>py_bytes,则不进行人工记忆管理。

However, I can t get this working with a simple printf test. Here is my cython file:

cdef extern from "stdio.h":
  printf(char* string)

def go():
  py_string = """
a complicated string
with a few
newlines.
"""

  cdef bytes py_bytes = py_string.encode()

  cdef char* c_string = py_bytes

  printf(c_string)

  print "we don t get this far :("

在操作时使用<代码>pyximport汇编 定点前向终点站提供以下产出:

a complicated string
with a few
newlines.
Segmentation fault: 11

现在,我检查了在案卷中实际运用的cy子,并在一个面包车C档案中对其进行审判。

#include "stdio.h"

static char __pyx_k_1[] = "
a complicated string
with a few
newlines.
";

int main(void) {
  void* output = printf(__pyx_k_1);
  if (!output) {
    printf("apparently, !output.");
  }
}

为明确起见,cy星生成的编码可捕获<条码>(>/条码>的产出,并对“而不是”进行测试。 变量类型为<代码>PyObject*。

我在座的唯一猜测是,扼杀行为被不当终止,因此,照样只是过去结束,造成ault,但由于这确实发生在我的纯粹考验中,我完全 st。

因此,我的实际问题是,我是如何? 是否从cy到 code? 答复指出,解决Im试图在顶端解决的实际问题更为容易,也令人非常欢迎:

最佳回答

Importing printf from libc.stdio fixes the problem for me:

from libc.stdio cimport printf

def go():
    py_string = """
a complicated string
with a few
newlines.
"""

    cdef bytes py_bytes = py_string.encode()
    cdef char* c_string = py_bytes
    printf(c_string)

    print "we actually got this far! :)"

这一错误在<代码>印本/编码>的申报中。 这本应该作为<代码>stdio.pxd清单。

cdef extern from *:
    ctypedef char const_char "const char"

int printf(const_char *, ...)

虽然你的版本隐含在objecttextf(char *)上;默认的回归价值类型是int,例如C. 右声明转而试图Py_XDECREF。 return/代码。

(Btw,载于“vanilla” C problem, You shouldn t be madeing the Return Value from to und *

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签