Note that there are some explanatory texts on larger screens.

plurals
  1. POcasting PyCFunctionWithKeywords in PyMethodDef
    text
    copied!<p>Recently I've been wrapping a lot of C++ code in python, and I find this block (taken directly from the <a href="http://docs.python.org/extending/extending.html#a-simple-example%5d" rel="nofollow">python documentation</a>) a bit troubling: </p> <pre><code>static PyMethodDef keywdarg_methods[] = { /* The cast of the function is necessary since PyCFunction values * only take two PyObject* parameters, and keywdarg_parrot() takes * three. */ {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS | METH_KEYWORDS, "Print a lovely skit to standard output."}, {NULL, NULL, 0, NULL} /* sentinel */ }; </code></pre> <p>The issue is the line which casts <code>kwarg_parrot</code>, of type <code>PyCFunctionWithKeywords</code> to a <code>PyCFunction</code>. </p> <p>Coming from a C++ background (and given that I am wrapping C++ code), it seems wrong to use a C-style cast. I've tried <code>static_cast</code>, and <code>dynamic_cast</code>, both of which cause the compiler to complain (with good reason, this really is an unsafe cast in the general sense). The only viable C++ option seems to be<code>reinterpret_cast</code>, but so far as I can tell this is a more verbose version of a C-style cast.</p> <p>Granted, the above <em>is</em> wrapped in an <code>extern "C"</code> block, so maybe the C way is the correct way. Does anyone have any better ideas? (What I'd really like to see would be a solution that could automatically generate the doc string based on the keywords.) </p> <p>Unfortunately, solutions like Boost.Python and SWIG are off the table. (I'm working within an ugly framework)</p>
 

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