[Pyrex] Pyrex error using C malloc

Georg Grabler ggrabler at gmail.com
Tue May 15 12:34:45 UTC 2007


Hello,

I'm experiencing problems with malloc on a C pointer defined in a cdef.

Below, you can see the complete files i've written, including the
structure, class, base and makefile.
Basically, i get the following error (compiletime):
config.pyx:15:64: 'pyalpm_list' is not a constant, variable or
function identifier
Well, it is a variable. A C variable in this case, or better a C structure.

By adding the line
cdef t_config.pyalpm_list _list
and using _list in the sizeof() call, it seems to work perfectly.
Am i using the cimport a way it should not be used? I'm trying to
implement a own object in pyrex, doing some tasks (parameter checking)
and holding lists for me.

I've the following file setup:

t_config.pxd
cdef struct pyalpm_list:
  char *pkgname
  pyalpm_list *next


config.pxd
cimport t_config

cdef class config:
  cdef t_config.pyalpm_list *NoUpgrade
  cdef t_config.pyalpm_list *IgnorePkg


config.pyx
cdef extern from "stdlib.h":
  ctypedef unsigned long size_t
  void *malloc(int len)
  size_t sizeof(void *)

cdef class config:
  def addIgnorePkg(self, char *in_pkgname):
    cdef t_config.pyalpm_list *ptr

    if self.IgnorePkg:
      ptr = self.IgnorePkg
      while ptr.next:
        ptr = ptr.next

      ptr.next = <t_config.pyalpm_list *> malloc(sizeof(t_config.pyalpm_list))
      ptr = ptr.next
      ptr.pkgname = in_pkgname
    else:
      self.IgnorePkg = <t_config.pyalpm_list *>
malloc(sizeof(t_config.pyalpm_list))
      self.IgnorePkg.pkgname = in_pkgname

    return

  def remIgnorePkg(self, char *pkgname):
    return 1


pyalpmbase.pyx
cimport config
import config

config = config.config()


Makefile:
PYVERSION=2.5
PYPREFIX=/usr
INCLUDES=-I$(PYPREFIX)/include/python$(PYVERSION)

all: pyalpmbase.so config.so

clean:
	rm -f *.o *.so *.c

pyalpmbase.so: pyalpmbase.o
	gcc -shared $^ -o $@

pyalpmbase.o: pyalpmbase.c
	gcc -c -fpic $(INCLUDES) $^

pyalpmbase.c: pyalpmbase.pyx
	pyrexc pyalpmbase.pyx


config.so: config.o
	gcc -shared $^ -o $@

config.o: config.c
	gcc -c -fpic $(INCLUDES) $^

config.c: config.pyx config.pxd t_config.pxd
	pyrexc config.pyx



More information about the Pyrex mailing list