File: class/Workbook/Examples/Lecture15/environ.c

#include <Python.h>
#include <stdlib.h>


/* Functions */
static PyObject *                     /* returns object */
wrap_getenv(PyObject *self, PyObject *args)
{                                     /* args from python */
   char *varName, *varValue;
   PyObject *returnObj = NULL;        /* null=exception */

   if (PyArg_Parse(args, "s", &varName)) {          /*P->C*/
      varValue = getenv(varName);
      if (varValue != NULL)
          returnObj = Py_BuildValue("s", varValue); /*C->P*/
      else
          PyErr_SetString(PyExc_SystemError, "bad getenv");
   }
   return returnObj;
}


/* Registration */
static struct PyMethodDef environ_methods[] = {
    {"getenv", wrap_getenv},        /* name, address */
    {NULL, NULL}
};


/* Initialization */
void initenviron()                  /* called by Python  */
{                                   /* on first import   */
    (void) Py_InitModule("environ", environ_methods);
}



[Home page] Books Code Blog Python Author Train Find ©M.Lutz