如何使用Python SVN API SWIG绑定调用svn.client.svn_client_list2?

0 投票
1 回答
800 浏览
提问于 2025-04-15 13:42

问题

我想知道如何通过 SVN API 的 SWIG 绑定,从 Python 调用 svn_client_list2 这个 C API 函数?

问题描述

我在 svn.client 模块中找到了这个函数,但调用它时遇到了问题,因为它使用的回调函数是一个类型定义 svn_client_list_func_t,我不知道如何在 Python 中使用这个类型定义。

虽然我找到了一个类 svn.client.svn_client_list_func_tsvn.client.svn_client_list_func_tPtr,但我找不到如何使用它的例子。

错误使用 svn.client.svn_client_list2

如果你用一个普通的 Python 函数作为回调参数来调用 svn.client.svn_client_list2 函数,会出现错误。

import svn.core, svn.client

path = svn.core.svn_path_canonicalize("/path/to/a/working_copy/")
pool = svn.core.Pool()
ctx = svn.client.svn_client_create_context(pool)
revision = svn.core.svn_opt_revision_t()
SVN_DIRENT_ALL = 0xffffffffl
def _handle_list(path, dirent, abs_path, pool):
  print(path, dirent, abs_path, pool)

svn.client.svn_client_list2(path,
                            revision,
                            revision,
                            svn.core.svn_depth_infinity,
                            SVN_DIRENT_ALL,
                            True,
                            _handle_list,
                            ctx,
                            pool)

TypeError: argument number 7: a 'svn_client_list_func_t *' is expected, 'function(<function _handle_list at 0x01365270>)' is received

错误使用 svn.client.svn_client_list_func_t

尝试初始化 svn.client.svn_client_list_func_t 会导致异常。

callback_function = svn.client.svn_client_list_func_t()

RuntimeError: No constructor defined

有没有什么想法可以继续进行?

1 个回答

0

看起来你现在是无法做到这一点的。当我深入研究SWIG的绑定代码和文档时,发现当你使用目标语言的函数作为回调函数时,需要为它准备一个类型映射,正如SWIG文档中所说的:

虽然SWIG通常不允许在目标语言中编写回调函数,但可以通过使用类型映射和其他高级SWIG功能来实现。

看起来在Python中缺少这个功能……

撰写回答