Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the Python C API, just as in Python, "Creating an object" is just calling its type</p> <p>Unfortunately, doing all the <code>getattr</code> stuff in C is really annoying. Depending on what you want to do from C, you can provide a bunch of helper modules which you pass over to C at import time (see <a href="https://github.com/couchbase/couchbase-python-client/blob/master/couchbase/_bootstrap.py" rel="nofollow">https://github.com/couchbase/couchbase-python-client/blob/master/couchbase/_bootstrap.py</a> for an example).</p> <p>In any event, Python is a really simple language, both inside and out. Thus, let's break this down.</p> <pre><code>socketHandler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT) </code></pre> <p>PEP-7 says C89, but it's easier to show it in C99:</p> <pre><code>PyObject *logging = PyImport_ImportModule("logging"); PyObject *handlers = PyObject_GetAttrString(logging, "handlers"); </code></pre> <p>Now, build the socket handler. From the C API, calling something can be done with PyObject_CallObject, which works on anything that has <code>__call__</code> defined First, though, you need to build thr arguments..</p> <pre><code>PyObject *sh_ctor = PyObject_GetAttrString(handlers, "SocketHandler"); PyObject *args = Py_BuildValue("(si)", "localhost", DEFAULT_LOGGING_PORT) PyObject *sh_instance = PyObject_CallObject(sh_ctor, args); </code></pre> <p>I'm not going to show how to do arg building here, or even proper error checking or refcounting. Note that in the C API, if a function fails, it returns <code>NULL</code> and raises an exception (unless noted otherwise).</p> <p>What I'd recommend in reality is figure out how much of the <code>logging</code> module you actually need to use from C, and wrap that functionality in a simpler subset which you can call from C. See the <code>_bootstrap.py</code> module i linked to above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload