LJ Archive

Listing 2. Python Module Written in C

#include "Python.h"
#include <sys/types.h>
extern char * crypt();
static PyObject *
crypt_crypt(PyObject *self,
       PyObject *args)
{
  char *word, *salt;
  // parse the incoming arguments
  if (!PyArg_Parse(args, "(ss)", &word, &salt))
    {
      return NULL;
    }
  // return the hashed string
  return PyString_FromString(crypt(word, salt));
}
// declare the methods that crypt module exports
static PyMethodDef
crypt_methods[] =
{
  {"crypt", crypt_crypt},
  {NULL, NULL}
};
// expose module entry point
DL_EXPORT(void)
initcrypt()
{
  Py_InitModule("crypt", crypt_methods);
}
LJ Archive