[英]Using a python module's C API in my own C module
This seems to be sort of what I want, except I want to make an instance of a C PyObject in another C module. 这似乎是我想要的,除了我想在另一个C模块中创建一个C PyObject的实例。
Create instance of a python class , declared in python, with C API 使用C API创建在python中声明的python类的实例
But I am not able to find the documentation for using a Python module from C. I can't seem to find the relevent docs online as everyone just talks about extending Python with C. 但我无法找到使用C语言的Python模块的文档。我似乎无法在线找到相关的文档,因为每个人都只是谈论用C语言扩展Python。
My issue is that I have a Pygame which I want to speed up. 我的问题是我有一个Pygame,我想加快速度。 So I am making a C module which needs access to Pygame.
所以我正在制作一个需要访问Pygame的C模块。 How do I import pygame?
如何导入pygame? I don't want to import two copies, right?
我不想导入两份副本,对吗? The coding part I can figure out, I just don't know how to configure and link the code.
我可以搞清楚编码部分,我只是不知道如何配置和链接代码。 I know I must be missing some docs, so if anyone can point me in the right direction, I'd be much obliged.
我知道我必须错过一些文档,所以如果有人能指出我正确的方向,我会非常感激。
Update: Sorry I reread my post and realized my wording was terrible. 更新:对不起,我重读了我的帖子,意识到我的措辞很糟糕。
If you have pygame installed you can look in your Python/include directory and find pygame header files. 如果你安装了pygame,你可以查看你的Python / include目录并找到pygame头文件。 What are they for?
它们适用于什么? I thought that your C module could access the pygame C module directly, so that your python script AND your C module used the same pygame instance.
我认为你的C模块可以直接访问pygame C模块,这样你的python脚本和你的C模块就使用了相同的pygame实例。
Clarification anyone? 澄清任何人?
诀窍是编写普通的Python例程,但在C中。根据需要使用PyImport_ImportModule()
, PyObject_GetAttr()
和PyObject_Call()
。
我用Google搜索了一下“我感到很幸运。”
You can use the pygame C-API, even though it is probably not meant to be used in that way (which then again begs the question why the pygame headers are installed with the pygame package). 您可以使用pygame C-API,即使它可能不是以这种方式使用(这再次引出了为什么pygame标头与pygame包一起安装的问题)。
An extension built against such an "internal" API is likely to break when the next version/build of pygame comes out, unless pygame developers care about the API/ABI compatibility of these C headers... 当pygame的下一个版本/版本出现时,针对这种“内部”API构建的扩展可能会破坏,除非pygame开发人员关心这些C头的API / ABI兼容性......
Anyway, here is an example: 无论如何,这是一个例子:
C extension module code (red.c): C扩展模块代码(red.c):
#include <Python.h>
#include <pygame/pygame.h>
static PyObject* fill_surface(PyObject* self, PyObject* args)
{
PyObject* py_surface = NULL;
if( !PyArg_ParseTuple(args, "O", &py_surface) )
return NULL;
PySurfaceObject* c_surface = (PySurfaceObject*) py_surface;
SDL_FillRect( c_surface->surf, 0, SDL_MapRGB(c_surface->surf->format, 255, 0, 0) );
return Py_None;
}
PyMethodDef methods[] = {
{"fill_surface", fill_surface, METH_VARARGS, "Paints surface red"},
{NULL, NULL, 0, NULL},
};
void initred()
{
Py_InitModule("red", methods);
}
Makefile for building (Linux): 用于构建的Makefile(Linux):
SOURCES = red.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = red.so
CFLAGS += -Wall -O2 -g
CFLAGS += $(shell python-config --cflags)
CFLAGS += $(shell sdl-config --cflags)
LDFLAGS += $(shell sdl-config --libs)
all: $(TARGET)
clean:
$(RM) $(OBJECTS)
$(RM) $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS) $(LDFLAGS)
$(OBJECTS): Makefile
Python test code: Python测试代码:
import pygame
import red
pygame.init()
screen = pygame.display.set_mode( (640, 480) )
red.fill_surface(screen)
pygame.display.update()
pygame.time.delay(3000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.