我想去接西顿。在import counter
cdef public void increment():
counter.increment()
cdef public int get():
return counter.get()
cdef public void say(int times):
counter.say(times)
这是我用来调用函数的“粘合代码”计数器.py,一个纯Python源代码文件。它的布局是这样的:
^{pr2}$
我已经成功地编译并运行了这个程序。功能正常。然而,当我测试这个程序时发生了一件非常奇怪的事情:int main(int argc, char *argv[]) {
Py_Initialize();
// The following two lines add the current working directory
// to the environment variable `PYTHONPATH`. This allows us
// to import Python modules in this directory.
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
PyInit_glue();
// Tests
for (int i = 0; i < 10; i++)
{
increment();
}
int x = get();
printf("Incremented %d times\n", x);
printf("The binary representation of the number 42 is");
say(3);
Py_Finalize();
return 0;
}
我希望程序产生以下输出:Incremented 10 times
The binary representation of the number 42 is
101010
但是,它打印的是:Incremented 10 times
101010
The binary representation of the number 42 is
但如果我换线printf("The binary representation of the number 42 is");
到printf("The binary representation of the number 42 is\n");
然后对输出进行校正。在
这在我看来很奇怪。我知道,如果我想打印Python函数的输出,我可以将其返回到C并将其存储在一个变量中,并使用C的printf()而不是本机Python print()。但我很想知道发生这种事的原因。毕竟,printf()语句是在say()语句之前到达的(我在gdb中再次检查了这个语句以确保)。谢谢你的阅读。在