从C调用Pyrex结果 [英] calling Pyrex results from C

查看:37
本文介绍了从C调用Pyrex结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个类提交C / C ++代码。 (这不是一个编程

级。语言的选择是惯性的。我认为它主要是

用来分散学生的课程主题。)我用C语言相当可靠

但是当考虑使用Python时,考虑用C语言编写是很痛苦的。* b $ b *更适合这些操作。


我想通过在Python中编程并将代码转换为C来保持理智并满足课程要求。它看起来像

a很少有人已经抓住了这个痒,但是我发现的翻译人员中,他们中的大多数(Python2C,P2C,PyFront)似乎已经死了。然而,Pyrex似乎维护良好,甚至可以作为Debian软件包使用。
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/


我意识到Pyrex的目标是创建一个可以从Python调用的
模块。因为我需要从C调用结果,

其他实用程序可能更合适但我认为

Pyrex可能对我来说对于不那么人为的情况比这个有用

因此值得了解更多信息。
http://www.cosc.canterbury.ac.nz/~gr...ngWithExternal


您还可以使用公共声明来生成C函数

和Pyrex模块中定义的变量,这些变量可用于外部C代码
。对此的需求预计会少于b $ b b b,但你可能想要这样做,例如,如果你将
嵌入另一个应用程序作为脚本

语言。正如Pyrex模块可以用作桥接器来允许Python代码调用C代码,它也可以用于允许C代码调用Python代码。


[...]


您可以在Pyrex

模块中定义C变量和函数使用public关键字对外部C代码(或另一个Pyrex

模块)

我发现只要代码中的所有内容都是cdef- ed,

我可以从我的C代码中调用Pyrex代码。但是,如果我非常喜欢提及任何Python,那么在执行期间会出现段错误。


例如,这很好。

cdef public int foo():

cdef int i

i = 123


return(i)


这会导致段错误。

cdef public int foo():

cdef int i

i = 123

j = 5

返回(i)


这意味着我不能,例如,创建一个带有

文件名(字符串)的函数,使用PIL进行一系列操作,并且

返回一个字符串。


任何建议(除了吮吸它并用C语言完成)?


谢谢。


- -kyler

I need to submit C/C++ code for a class. (It''s not a programming
class. The choice of language is inertial. I think that it mostly
serves to distract students from the course subject.) I''m fairly
fluent with C but it hurts to think about writing in C when Python
is *so* much more appropriate for these operations.

I''d like to keep my sanity and satisfy the course requirements by
programming in Python and converting the code to C. It looks like
a few people have scratched this itch already, but of the
translators I found, most of them (Python2C, P2C, PyFront) seem to
be dead. Pyrex, however, appears to be well-maintained and is even
available as a Debian package.
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

I realize that the goal of Pyrex is to create a module that can be
called from Python. For my need of calling the result from C, the
other utilities are probably more appropriate but I think that
Pyrex could be useful to me for less contrived situations than this
so it''s worthwhile to learn more about it.
http://www.cosc.canterbury.ac.nz/~gr...ngWithExternal

You can also use public declarations to make C functions
and variables defined in a Pyrex module available to
external C code. The need for this is expected to be less
frequent, but you might want to do it, for example, if you
are embedding Python in another application as a scripting
language. Just as a Pyrex module can be used as a bridge to
allow Python code to call C code, it can also be used to
allow C code to call Python code.

[...]

You can make C variables and functions defined in a Pyrex
module accessible to external C code (or another Pyrex
module) using the public keyword

I''ve discovered that as long as everything in the code is cdef-ed,
I can call Pyrex code from my C code. If, however, I so much as
mention anything Pythonish, it segfaults during execution.

For example, this is fine.
cdef public int foo():
cdef int i
i = 123

return(i)

And this results in a segfault.
cdef public int foo():
cdef int i
i = 123
j = 5

return(i)

This means that I can''t, for example, make a function that takes a
filename (string), uses PIL to do a bunch of operations, and
returns a string.

Any suggestions (besides "suck it up and do it all in C")?

Thank you.

--kyler

推荐答案

[Kyler Laird]
[Kyler Laird]
我意识到Pyrex的目标是创建一个可以从Python调用的模块。


反之亦然。我发现Pyrex对于在C程序中调用来自

的Python模块非常有用,这些程序不支持Python。你可以

甚至写完Pyrex程序。

我发现只要代码中的所有东西都是cdef-ed,我就可以打电话给Pyrex我的C代码中的代码。但是,如果我非常提及任何Python,那么它在执行期间会出现段错误。
I realize that the goal of Pyrex is to create a module that can be
called from Python.
Or vice-versa. I found Pyrex useful for calling Python modules from
within C programs which were not Python-aware to start with. You may
even write wholly Pyrex programs.
I''ve discovered that as long as everything in the code is cdef-ed,
I can call Pyrex code from my C code. If, however, I so much as
mention anything Pythonish, it segfaults during execution.




建立Python上下文需要一些神奇的东西,并不多。

希望它会有所帮助,这是我在生成

Pyrex主程序时使用的模板。 '%(module)s''将被Pyrex模块取代

名称,因为Pyrex需要初始化自己。当Python调用Pyrex时,

初始化会在import时自动发生,但是当C

最初调用Pyrex时,应该明确启动初始化。


cdef extern void Py_Initialize()

cdef extern void Py_Finalize()

cdef extern void init%(module)s()


cdef extern int main(int argc,char ** argv):

cdef int index,status

Py_Initialize()

init%(模块)s()

导入sys

尝试:

arguments = []

,索引从0< = index< argc:

arguments.append(argv [index])

sys.argv =参数

result = main_application()

提高SystemExit,结果或0

除了SystemExit,结果:

result = str(结果)

试试:

status = int(result)

除了ValueError:

如果不是result.endswith(''\ n''):

结果=结果+''\ n''

sys.stderr.write(结果)

status = 1

除外:

导入追溯

traceback.print_exc()

status = 1

Py_Finalize()

返回状态

-

Fran?ois Pinard http://www.iro.umontreal.ca/~pinard



There is some magic needed to establish Python context, not much.
Hoping that it will help, here is a template I use when generating
Pyrex main programs. `%(module)s'' shall be replaced by the Pyrex module
name, as Pyrex needs to initialise itself. When Python calls Pyrex,
initialisation occurs automatically at `import'' time, but when C
initially calls Pyrex, initialisation should be explicitly launched.

cdef extern void Py_Initialize()
cdef extern void Py_Finalize()
cdef extern void init%(module)s()

cdef extern int main(int argc, char **argv):
cdef int index, status
Py_Initialize()
init%(module)s()
import sys
try:
arguments = []
for index from 0 <= index < argc:
arguments.append(argv[index])
sys.argv = arguments
result = main_application()
raise SystemExit, result or 0
except SystemExit, result:
result = str(result)
try:
status = int(result)
except ValueError:
if not result.endswith(''\n''):
result = result + ''\n''
sys.stderr.write(result)
status = 1
except:
import traceback
traceback.print_exc()
status = 1
Py_Finalize()
return status
--
Fran?ois Pinard http://www.iro.umontreal.ca/~pinard


Kyler Laird< Ky ***@news.Lairds.org>写道:

[...]
Kyler Laird <Ky***@news.Lairds.org> writes:
[...]
这意味着我不能,例如,制作一个带有
文件名(字符串)的函数,使用PIL进行一系列操作,并返回一个字符串。


我不明白为什么你不能在Pyrex中编写模块,然后

使用标准嵌入技术来嵌入Python和然后使用几个C API调用调用您的

模块。或者做同样的事,但也写一个Pyrex

cdef函数来包装它,所以你可以把它称为普通的

old。 C函数而不是最后几个C API调用(你仍然需要其他嵌入调用,当然,初始化Python等)。


如果你被卡住了,试试pyrex邮件列表。


我肯定会检查以确保你不会被标记

因为是一个聪明的人(尽管你正在生成C,

根本不依赖于Python,它可能仍然是不可读的。)


任何建议(除了吮吸它并在C中全部完成)?
This means that I can''t, for example, make a function that takes a
filename (string), uses PIL to do a bunch of operations, and
returns a string.
I don''t see why you shouldn''t be able to write a module in Pyrex, then
use standard embedding techniques to embed Python and then call your
module with a few C API calls. Or do the same, but also write a Pyrex
cdef function to wrap it all up, so you can just call that as a "plain
old" C function instead of those final few C API calls (you''ll still
need other embedding calls, of course, to initialise Python, etc.).

Try the pyrex mailing list if you''re stuck.

I''d very definitely check to make sure you aren''t going to get marked
down for being a smartass, though (even if you''re generating C that
doesn''t depend on Python at all, it might still be unreadable).

Any suggestions (besides "suck it up and do it all in C")?




我想我会在C,无论是否允许

使用Pyrex。

John



I think I''d do it in C, regardless of whether or not it''s allowable to
use Pyrex.
John


On Tue ,2004年1月20日下午12:27:33 -0500,Fran?ois Pinard写道:
On Tue, Jan 20, 2004 at 12:27:33PM -0500, Fran?ois Pinard wrote:
建立Python上下文需要一些魔力,并不多。
希望它会有所帮助,这是我生成
P时使用的模板yrex主程序。
There is some magic needed to establish Python context, not much.
Hoping that it will help, here is a template I use when generating
Pyrex main programs.




保罗在这条道路上开始了我,你给了我正确的东西

继续。


谢谢!


--kyler



Paul started me on this path and you''ve given me exactly what I need to
keep going.

Thank you!

--kyler


这篇关于从C调用Pyrex结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆