首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python ctype和librsvg出错

Python ctype和librsvg出错
EN

Stack Overflow用户
提问于 2011-05-27 01:38:50
回答 2查看 1.1K关注 0票数 4

我试图用Python的ctype来包装librsvg的基本函数,但是我得到了一个Segfault值。

C:

代码语言:javascript
复制
// pycairo excerpt
typedef struct {
  PyObject_HEAD
  cairo_t *ctx;
  PyObject *base; /* base object used to create context, or NULL */
} PycairoContext;

// librsvg excerpt
RsvgHandle * rsvg_handle_new_from_file (const gchar * file_name, GError ** error);
// ...
gboolean rsvg_handle_render_cairo (RsvgHandle * handle, cairo_t * cr);

Python ctype:

代码语言:javascript
复制
from ctypes import *
from ctypes import util


librsvg = cdll.LoadLibrary('/brew/lib/librsvg-2.2.dylib')
libgobject = cdll.LoadLibrary('/brew/lib/libgobject-2.0.dylib')

libgobject.g_type_init()


class RSVGDimensionData(Structure):

    _fields_ = (
        ('width', c_int),
        ('height', c_int),
        ('em', c_double),
        ('ex', c_double)
    )

class PycairoContext(Structure):

    _fields_ = (
        ('PyObject_HEAD', c_byte * object.__basicsize__),
        ('ctx', c_void_p),
        ('base', c_void_p)
    )


class RSVGHandle(object):

    def __init__(self, path):
        self.path = path
        self.error = ''
        self.handle = librsvg.rsvg_handle_new_from_file(self.path, self.error)

    def render_cairo(self, context):
        context.save()
        z = PycairoContext.from_address(id(context))
        librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)
        context.restore()


import cairo

h = RSVGHandle('bank.svg')
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)


# segmentation fault....
h.render_cairo(ctx)

错误发生在这一行:librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)

你知道这是怎么回事吗?

EN

回答 2

Stack Overflow用户

发布于 2013-02-19 02:36:43

问题是没有定义返回类型的规范;仅对结果使用c_void_p不足以解决这种情况下的问题。你需要把你的

代码语言:javascript
复制
librsvg.rsvg_handle_new_from_file.restype = c_void_p

在适当的地方。然后,它(也)在OSX中以32位或64位方式工作。

但我发现增强基本包装更有帮助,以便在从文件创建句柄时处理可能出现的错误。下面是一个基本的包装器,可以做到这一点。它还以基本相同的方式复制了标准rsvg绑定的基本用法。

代码语言:javascript
复制
from ctypes import CDLL, POINTER, Structure, byref, util
from ctypes import c_bool, c_byte, c_void_p, c_int, c_double, c_uint32, c_char_p

class _PycairoContext(Structure):
    _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                ("ctx", c_void_p),
                ("base", c_void_p)]

class _RsvgProps(Structure):
    _fields_ = [("width", c_int), ("height", c_int),
                ("em", c_double), ("ex", c_double)]

class _GError(Structure):
    _fields_ = [("domain", c_uint32), ("code", c_int), ("message", c_char_p)]


def _load_rsvg(rsvg_lib_path=None, gobject_lib_path=None):
    if rsvg_lib_path is None:
        rsvg_lib_path = util.find_library('rsvg-2')
    if gobject_lib_path is None:
        gobject_lib_path = util.find_library('gobject-2.0')
    l = CDLL(rsvg_lib_path)
    g = CDLL(gobject_lib_path)
    g.g_type_init()

    l.rsvg_handle_new_from_file.argtypes = [c_char_p, POINTER(POINTER(_GError))]
    l.rsvg_handle_new_from_file.restype = c_void_p
    l.rsvg_handle_render_cairo.argtypes = [c_void_p, c_void_p]
    l.rsvg_handle_render_cairo.restype = c_bool
    l.rsvg_handle_get_dimensions.argtypes = [c_void_p, POINTER(_RsvgProps)]

    return l    

_librsvg = _load_rsvg()


class Handle(object):
    def __init__(self, path):
        lib = _librsvg
        err = POINTER(_GError)()
        self.handle = lib.rsvg_handle_new_from_file(path.encode(), byref(err))
        if self.handle is None:
            gerr = err.contents
            raise Exception(gerr.message)
        self.props = _RsvgProps()
        lib.rsvg_handle_get_dimensions(self.handle, byref(self.props))

    def render_cairo(self, ctx):
        """Returns True is drawing succeeded."""
        z = _PycairoContext.from_address(id(ctx))
        return _librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)

示例用法可以在https://stackoverflow.com/a/14928770/1832154上看到。

票数 6
EN

Stack Overflow用户

发布于 2011-07-08 20:00:59

librsvg.rsvg_handle_render_cairo需要的是指针,而不是整数。不能确定这里的整个故事,但这一修改至少不会导致segfault。

尝尝这个

代码语言:javascript
复制
 librsvg.rsvg_handle_render_cairo(c_void_p(self.handle), c_void_p(z.ctx))

请注意,我将两个参数包装在c_void_p中,使它们成为空*指针。不是很理想,但看起来很管用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6142757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档