简体   繁体   English

Chicken for python:使用共享库扩展python

[英]Chicken for python: Extending python with the use of a shared library

I have read that it is possible to extend Python by writing a shared library that the Python interpreter can load as part of an import statement. 我已经读到,可以通过编写一个共享库来扩展Python,Python解释程序可以将该共享库作为import语句的一部分加载。

Could anyone point me to a simple example or tutorial where such an endeavour is accomplished by the use of Chicken Scheme instead of C/C++ ? 谁能指出一个简单的示例或教程,其中通过使用Chicken Scheme而不是C / C ++来实现这一目标 Chicken is said to produce "portable and efficient C" - hence it should be suitable for such a task, no? 据说鸡肉会产生“轻便且高效的C”-因此它应该适合这种任务,不是吗?

Cheers! 干杯!

This is an interesting question. 这是个有趣的问题。 What you are suggesting looks like a good idea, however I don't think anyone has attempted it before and got round to writing a tutorial. 您所建议的内容似乎是个好主意,但我认为没有人尝试过此方法,而是开始编写教程。 There is an interface to call python from chicken scheme, see pyffi , this looks like a wrapper round the python C interface. 有一个接口可以从chicken方案中调用python,请参阅pyffi ,它看起来像是python C接口的包装器。 Pyffi doesn't allow calls to be made in the other direction as asked in the question. Pyffi不允许在问题中向另一个方向打电话。

Creating a module for python in C involves using quite a lot of python API to manipulate python objects, because python is dynamically typed all parameters based into the C code are of type PyObject this goes for built in types, user defined types and collections. 在C中为python创建模块需要使用大量的python API来操作python对象,因为python是动态类型化的,基于C代码的所有参数都是PyObject类型,这适用于内置类型,用户定义类型和集合。 The C API then provides functions to manipulate these objects, perform arithmetic, add them to and from collections, etc. In addition manual reference counting must be carried out on all pyObject s, to ensure garbage collection can be carried out correctly in python. 然后,C API提供了操作这些对象,执行算术,将它们添加到集合中或从集合中添加等的功能。此外,必须对所有pyObject进行手动引用计数,以确保可以在python中正确进行垃圾收集。 Documentation for this can be seen here . 有关此文档,请参见此处

It is possible to convert a PyObject into a C types such as int, double, etc, within the C code, which can make writing the module easier, if a function is large, or native C collections are being used. 可以在C代码中将PyObject转换为C类型,例如int,double等,如果函数很大或正在使用本机C集合,则可以简化模块的编写。 Then the output must be wrapped back into PyObject s to return data back to python code. 然后,必须将输出包装回PyObject以将数据返回到python代码。

Creating python modules in chicken could be much easier than C, as both are dynamically typed languages, removing the need of explicitly manipulating PyObjects , with the additional benefit of creating efficient C code. 在Chicken中创建python模块可能比C容易得多,因为这两种都是动态类型化的语言,因此无需显式操作PyObjects ,并且具有创建高效C代码的其他好处。 It looks like pyffi has gone some way towards mapping the types, although further work would be required to allow this to write modules. 看起来pyffi在映射类型上已经做了一些努力,尽管还需要进一步的工作才能允许它编写模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM