PyListObject & C Modules

Joshua Gramlich jggramlich at yahoo.com
Wed Sep 4 12:23:28 EDT 2002


Thank you for your replies, here is what I have so far:

############################code block#############################

#include "Python.h"
#include <stdio.h>

PyObject*
get_first_elem(PyObject *unused, PyObject *args)
{
  PyObject *list;
  PyObject *first;
  PyObject *result;
  if(!PyArg_ParseTuple("!0", &list))
    return NULL;

  if(!PyList_Check(list)) {
    PyErr_SetString(PyExc_TypeError, "get_first_elem expects a list");
    return NULL;
  }

  if(PyList_Size(list) == 0){
    PyErr_SetString(PyExc_ValueError, "empty lists not allowed");
    return NULL;
  }

  first = PyList_GetItem(list, 0);
  if (first == NULL) return NULL;
  result = PyList_New(1);
  if (!result) return NULL;
  Py_ADDREF(first);
  if (PyList_SetItem(result, 0, first) != 0) {
    Py_DECREF(first);
    Py_DECREF(result);
    result = NULL;
  }
  return result;
}

####################################################################

Now, when I compile that, I get:

make

gcc -I/fnal/ups/prd/python/v2_1/SunOS+5/include/python2.1/ -c slist.c
slist.c: In function `get_first_elem':
slist.c:10: warning: passing arg 1 of `PyArg_ParseTuple' from
incompatible pointer type
slist.c:10: warning: passing arg 2 of `PyArg_ParseTuple' from
incompatible pointer type
gcc -G slist.o -o slist.so

[d0ora3 ~/work/grid_query/test]$ python hello.py 
Traceback (most recent call last):
  File "hello.py", line 2, in ?
    import slist
ImportError: ld.so.1: python: fatal: relocation error: file
./slist.so: symbol Py_ADDREF: referenced symbol not found
[d0ora3 ~/work/grid_query/test]$ 

Commenting PyADDREF out of the code causes the shared object not to
build at all.  My hello.py looks like this:


import slist

stuff = [2, 3, 8, 23]
print(slist.get_first_elem(stuff))



More information about the Python-list mailing list