Calling stdlib functions from pyrex

Clark C . Evans cce at clarkevans.com
Thu Jul 4 17:34:45 EDT 2002


Hello.  This pyrex stuff looks like a great tool.  I tried to convert
the module that I just posted recently, but I ran into a bit of
difficulty... here is my error message:

    pyrexc tztool.pyx
      Unpickling lexicon...
      Done (0.11 seconds)
    gcc -c -fPIC -I/usr/local/include/python2.2/ tztool.c
      tztool.c:21: redefinition of `struct tm'
      tztool.c:35: conflicting types for `setenv'
      /usr/include/stdlib.h:123: previous declaration of `setenv'
      tztool.c:36: conflicting types for `unsetenv'
      /usr/include/stdlib.h:184: previous declaration of `unsetenv'
      tztool.c:38: conflicting types for `time'
      /usr/include/time.h:132: previous declaration of `time'
      tztool.c:39: conflicting types for `localtime_r'
      /usr/include/time.h:142: previous declaration of `localtime_r'
      *** Error code 1

Any help would be great.  I have a ton of stuff that I'd
like to convert over... 

Best,

Clark
-- 
Clark C. Evans                   Axista, Inc.
http://www.axista.com            800.926.5525
XCOLLA Collaborative Project Management Software

----- BEGIN tztool.pyx
ctypedef long time_t
cdef extern struct tm:
        int     tm_sec
        int     tm_min
        int     tm_hour
        int     tm_mday
        int     tm_mon
        int     tm_year
        int     tm_wday
        int     tm_yday
        int     tm_isdst
        long    tm_gmtoff
        char    *tm_zone
cdef extern void setenv(char *key, char *val, int override)
cdef extern void unsetenv(char *key)
cdef extern void tzset()
cdef extern int  time(time_t *junk)
cdef extern tm * localtime_r(time_t *now, tm *fill)

def offset(zone):
    cdef time_t now
    cdef tm     a
    setenv("TZ",<char*>zone,1)
    tzset()
    now = time(<time_t *>0)
    localtime_r(&now, &a)
    unsetenv("TZ")
    tpl = (a.tm_zone,a.tm_gmtoff/60)
    tzset()
    return tpl

----- BEGIN Makefile
all:
        pyrexc tztool.pyx
        gcc -c -fPIC -I/usr/local/include/python2.2/ tztool.c
        gcc -shared tztool.c -o tztool.so
        python test.py

----- BEGIN tztool.c.old
#include <Python.h>
#include <assert.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
PyObject * offset(PyObject *self,PyObject *args) {
   time_t now; struct tm a; PyObject *pyobj;
   if(1 != PyTuple_Size(args)) { PyErr_BadArgument(); return NULL; }
   pyobj = PyTuple_GetItem(args,0);
   if(!pyobj) return NULL;
   if(!PyString_Check(pyobj)) { PyErr_BadArgument(); return NULL; }
   (void) setenv("TZ",PyString_AsString(pyobj),1); tzset();
   now = time( NULL );
   (void)localtime_r( &now, &a );
   pyobj = Py_BuildValue("(sl)",a.tm_zone,a.tm_gmtoff/60);
   return pyobj;
}
PyMethodDef methods[] = {{"offset",offset,METH_VARARGS},{NULL, NULL}};
void inittztool(void) {
    (void) Py_InitModule("tztool", methods);
}





More information about the Python-list mailing list