当使用“clinic”时,如何为python模块创建一个新函数?

2024-05-26 21:50:05 发布

您现在位置:Python中文网/ 问答频道 /正文

出于某些原因,我不得不扩展python的ssl模块。特别是我需要一个C语言的load\u cert_chain()的第二个版本

我的问题与openssl无关,而是如何处理“clinic”: 在原始函数前面有一个临床输入:

/*[clinic input]
_ssl._SSLContext.load_cert_chain
certfile: object
keyfile: object = NULL
password: object = NULL

[clinic start generated code]*/

static PyObject *
_ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile,
                                  PyObject *keyfile, PyObject *password)
/*[clinic end generated code: output=9480bc1c380e2095 input=7cf9ac673cbee6fc]*/
{
    PyObject *certfile_bytes = NULL, *keyfile_bytes = NULL;
    pem_password_cb *orig_passwd_cb = self->ctx->default_passwd_callback;
    ...

到目前为止,我知道,\u ssl_SSLContext_load_cert_chain_是load_cert_chain的实现,并在头文件\u ssl.c.h中定义的_SSLContext_load_cert_chain中调用 然而,这是诊所自动生成的,不是吗?在

既然一切都是自动创建的,那么我从哪里开始定义我的新函数load\u cert_chain2作为原始函数的副本呢?在


Tags: 函数sslchaininputcertobjectloadpassword
1条回答
网友
1楼 · 发布于 2024-05-26 21:50:05

为了消除一些误解:

不,参数诊所与链接C函数到Python函数无关!在

参数诊所只是为一个C函数创建一个函数签名。在你的例子中

/*[clinic input]
_ssl._SSLContext.load_cert_chain
certfile: object
keyfile: object = NULL
password: object = NULL

_ssl__SSLContext_load_cert_chain_implC函数将在签名中公开为_ssl._SSLContext.load_cert_chain,三个参数期望pythonobjectcertfile作为位置参数,keyfile, password作为可选参数,默认值为NULL。在


函数的调用方式或调用时间与参数诊所无关。方法声明在Modules/clinic/_ssl.c.h中完成:

^{pr2}$

它作为方法显式添加到Modules/_ssl.c中的_SSLContext类中:

static struct PyMethodDef context_methods[] = {
    [...]
    _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF
    [...]
    {NULL, NULL}        /* sentinel */
};

[...]

static PyTypeObject PySSLContext_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_ssl._SSLContext",                        /*tp_name*/
    sizeof(PySSLContext),                      /*tp_basicsize*/
    [...]
    context_methods,                           /*tp_methods*/
    [...]
};

所以参数诊所不负责将方法分配给类。这是通过在_ssl__SSLContext_load_cert_chain_impl周围使用包装函数_ssl__SSLContext_load_cert_chain完成的,并使用分配给类的PyMethodDef结构分配给类。在

现在你知道没有自动生成的链接,我不知道这是否有助于当你想替换那个函数。我不知道如何在不重新编译Python或将所有相关文件复制到扩展名的情况下轻松做到这一点(参数clinic或no clinic)。在

相关问题 更多 >

    热门问题