Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To extract an argument from an invocation of your method, you need to look at the functions documented in <a href="http://docs.python.org/c-api/arg.html#parsing-arguments-and-building-values" rel="noreferrer">Parsing arguments and building values</a>, such as <a href="http://docs.python.org/c-api/arg.html#PyArg_ParseTuple" rel="noreferrer"><code>PyArg_ParseTuple</code></a>. (That's for if you're only taking positional args! There are others for positional-and-keyword args, etc.)</p> <p>The object you get back from <code>PyArg_ParseTuple</code> doesn't have it's reference count increased. For simple C functions, you probably don't need to worry about this. If you're interacting with other Python/C functions, or if you're releasing the global interpreter lock (ie. allowing threading), you need to think very carefully about object ownership.</p> <pre><code>static PyObject * _sayhello_obj(PyObject *self, PyObject *args) { PyObject *obj = NULL; // How can I fill obj? static char fmt_string = "O" // For "object" int parse_result = PyArg_ParseTuple(args, fmt_string, &amp;obj); if(!parse_res) { // Don't worry about using PyErr_SetString, all the exception stuff should be // done in PyArg_ParseTuple() return NULL; } // Of course, at this point you need to do your own verification of whatever // constraints might be on your argument. </code></pre> <p>For calling a method on an object, you need to use either <a href="http://docs.python.org/c-api/object.html#PyObject_CallMethod" rel="noreferrer"><code>PyObject_CallMethod</code></a> or <a href="http://docs.python.org/c-api/object.html#PyObject_CallMethodObjArgs" rel="noreferrer"><code>PyObject_CallMethodObjArgs</code></a>, depending on how you construct the argument list and method name. And see my comment in the code about object ownership!</p> <p>Quick digression just to make sure you're not setting yourself up for a fall later: If you really are just getting the string out to print it, you're better off just getting the object reference and passing it to <a href="http://docs.python.org/c-api/object.html?highlight=print#PyObject_Print" rel="noreferrer"><code>PyObject_Print</code></a>. Of course, maybe this is just for illustration, or you know better than I do what you want to do with the data ;)</p> <pre><code> char s[1024]; // How can I fill s, from obj.getName() ? // Name of the method static char method_name = "getName"; // No arguments? Score! We just need NULL here char method_fmt_string = NULL; PyObject *objname = PyObject_CallMethod(obj, obj_method, method_fmt_string); // This is really important! What we have here now is a Python object with a newly // incremented reference count! This means you own it, and are responsible for // decrementing the ref count when you're done. See below. // If there's a failure, we'll get NULL if(objname == NULL) { // Again, this should just propagate the exception information return NULL; } </code></pre> <p>Now there are a number of functions in the <a href="http://docs.python.org/c-api/string.html#string-bytes-objects" rel="noreferrer">String/Bytes Objects</a> section of the <a href="http://docs.python.org/c-api/concrete.html" rel="noreferrer">Concrete Objects Layer</a> docs; use whichever works best for you.</p> <p>But <strong>do not forget this bit:</strong></p> <pre><code> // Now that we're done with the object we obtained, decrement the reference count Py_XDECREF(objname); // You didn't mention whether you wanted to return a value from here, so let's just // return the "None" singleton. // Note: this macro includes the "return" statement! Py_RETURN_NONE; } </code></pre> <p>Note the use of <a href="http://docs.python.org/c-api/none.html#the-none-object" rel="noreferrer"><code>Py_RETURN_NONE</code></a> there, and <strong>note that it's not <code>return Py_RETURN_NONE</code></strong>!</p> <p>PS. The structure of this code is dictated to a great extent by personal style (eg. early returns, <code>static char</code> format strings inside the function, initialisation to <code>NULL</code>). Hopefully the important information is clear enough apart from stylistic conventions.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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