How can I create a new function for a python module when "clinic" is used?

MichaelW

For certain reasons I have to extend python's ssl module. In particular I need a second version of load_cert_chain() in C.

My problem is not related to openssl but rather how to cope with "clinic": In front of the original function there is a clinic-input:

/*[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;
    ...

So far I know, that the _ssl__SSLContext_load_cert_chain_impl is the implementation of load_cert_chain and called within _ssl__SSLContext_load_cert_chain, which is defined within the header file _ssl.c.h This, however, is auto-generated by clinic- isn't it?

So where do I start to define my new function load_cert_chain2 as a copy of the original, since everything is auto-created?

MSeifert

To clear some misunderstanding:

No, the argument clinic has nothing to do with linking C functions to Python functions!

The argument clinic just creates a function signature for a C function. In your example

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

The _ssl__SSLContext_load_cert_chain_impl C function will be exposed in the signature as _ssl._SSLContext.load_cert_chain with the three arguments expecting a python object: certfile as positional argument and keyfile, password as optional arguments with a default of NULL.


How or when the function is called isn't linked to the argument clinic. The method declaration is done in Modules/clinic/_ssl.c.h:

#define _SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF    \
    {"load_cert_chain", (PyCFunction)_ssl__SSLContext_load_cert_chain, METH_FASTCALL, _ssl__SSLContext_load_cert_chain__doc__}

static PyObject *
_ssl__SSLContext_load_cert_chain(PySSLContext *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
{
    [...]
    return_value = _ssl__SSLContext_load_cert_chain_impl(self, certfile, keyfile, password);
}

and it's explicitly added as method to the _SSLContext class in Modules/_ssl.c:

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*/
    [...]
};

So the argument clinic isn't responsible for assigning the method to a class. That's done with a wrapper function _ssl__SSLContext_load_cert_chain around _ssl__SSLContext_load_cert_chain_impl and assigned to the class using a PyMethodDef struct that's assigned to the class.

Now you know that there is no auto-generated linking, I don't know if that helps when you want to replace that function. I don't know how you could easily do that (argument clinic or no clinic) without recompiling Python or copying all relevant files into your extension.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find the function of a python module?

From Dev

How can I prevent new instances of background processes when calling adb in python subprocess module

From Dev

APScheduler callback function - how can I call some funtion/module in python when a job is completed?

From Dev

Which branch is used when I create a new branch from an issue on Gitlab? Can I choose the source branch?

From Dev

How can I create a new thread AddressOf a function with parameters in VB?

From Dev

How can I create a new Graph Editor in Maya using Python?

From Dev

How can I create a new project with tasks in Asana using Python?

From Dev

How can I use pickle in python 3 to create a save function

From Dev

How can I create a constexpr function that returns a type (to be used in a template parameter)

From Dev

How can I create a constexpr function that returns a type (to be used in a template parameter)

From Dev

How can I restrict which functions are callable from an imported module? Can I make a function private? (Python)

From Dev

Error when I tried create new maven module in IntellijIDE 15

From Dev

How can I create a template version of this method that is used when template parameter is std::vector

From Dev

How can I create a template version of this method that is used when template parameter is std::vector

From Dev

Can I create a function that must only be used with defer?

From Dev

How can I create a new socket in /dev?

From Dev

How can I create a new instance of ImmutableDictionary?

From Dev

How can I create a new socket in /dev?

From Dev

How can I create new shell commands?

From Dev

How can i modify the styles of this text when used with a function in angular js?

From Dev

Given a module name atom, how can I create the struct for that module?

From Dev

How Can I "Inherit" a Ruby Module to Create Another Module?

From Dev

How can I tell if a Perl module is actually used in my program?

From Dev

Can I used a module inside of a module in Ruby?

From Dev

How can I best create a new df using index values from another df that are used to retrieve multiple values?

From Dev

How can i Create a new folder when a user is created just for the user

From Dev

how can I tell if a function can be used in a context manager?

From Dev

How do I create a function with a function in Python?

From Dev

Lua How to create custom function that can be used on variables follow up

Related Related

  1. 1

    How can I find the function of a python module?

  2. 2

    How can I prevent new instances of background processes when calling adb in python subprocess module

  3. 3

    APScheduler callback function - how can I call some funtion/module in python when a job is completed?

  4. 4

    Which branch is used when I create a new branch from an issue on Gitlab? Can I choose the source branch?

  5. 5

    How can I create a new thread AddressOf a function with parameters in VB?

  6. 6

    How can I create a new Graph Editor in Maya using Python?

  7. 7

    How can I create a new project with tasks in Asana using Python?

  8. 8

    How can I use pickle in python 3 to create a save function

  9. 9

    How can I create a constexpr function that returns a type (to be used in a template parameter)

  10. 10

    How can I create a constexpr function that returns a type (to be used in a template parameter)

  11. 11

    How can I restrict which functions are callable from an imported module? Can I make a function private? (Python)

  12. 12

    Error when I tried create new maven module in IntellijIDE 15

  13. 13

    How can I create a template version of this method that is used when template parameter is std::vector

  14. 14

    How can I create a template version of this method that is used when template parameter is std::vector

  15. 15

    Can I create a function that must only be used with defer?

  16. 16

    How can I create a new socket in /dev?

  17. 17

    How can I create a new instance of ImmutableDictionary?

  18. 18

    How can I create a new socket in /dev?

  19. 19

    How can I create new shell commands?

  20. 20

    How can i modify the styles of this text when used with a function in angular js?

  21. 21

    Given a module name atom, how can I create the struct for that module?

  22. 22

    How Can I "Inherit" a Ruby Module to Create Another Module?

  23. 23

    How can I tell if a Perl module is actually used in my program?

  24. 24

    Can I used a module inside of a module in Ruby?

  25. 25

    How can I best create a new df using index values from another df that are used to retrieve multiple values?

  26. 26

    How can i Create a new folder when a user is created just for the user

  27. 27

    how can I tell if a function can be used in a context manager?

  28. 28

    How do I create a function with a function in Python?

  29. 29

    Lua How to create custom function that can be used on variables follow up

HotTag

Archive