Checking the type of a PyObject

Fredrik Lundh fredrik at pythonware.com
Sun Aug 12 13:34:57 CEST 2001


wim delvaux wrote:
> in C  (Making abstraction of a lot of other stuff )
>
> Mod_Function( PyObject *self, PyObject *args )  {
>
>           PyObject * FirstArg;
>
>            if (!PyArg_ParseTuple(args, "o", &FirstArg ))
>                return NULL;

    if (!PyArg_ParseTuple(args, "O", &FirstArg ))
        return NULL;

>
>           /* And then something like */
>
>           if( Py_GetType( FirstArg ) == String ) {

    if (PyString_Check(FirstArg)) {

>               char * String;
>               PyArg_ParseTuple( args, "s", &String );

    String = PyString_AS_STRING(FirstArg)

>           } else if ( Py_GetType( FirstArg ) == Integer ) {

    } if (PyInt_Check(FirstArg)) {

>               long IVal;
>               PyArg_ParseTuple( args, "i", &IVal);

    IVal = PyInt_AsLong(FirstArg)

>           } else {
>               return NULL;

    PyErr_SetString(PyExc_TypeError, "expected string or integer");
    return NULL;

>           }
> }
>
> What would be the correct Python Library code to implement the above
> pseudo code

taking another look at the Extension/Embedding and C API docs
might help.  if you need sample code, look under Modules in the
source distribution.

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list