Compiled Python

Diane Trout

Compiling Python

Pyrex

I've actually used this one

Extending Python (hard way)


static PyObject *
DA_fix_covariance_matrix(PyObject *self, PyObject *args)
{
  PyObject *po_covar       = NULL; // function parameters
  double **covar           = NULL; // C versions of parameters

  int status = 0;
  int error = 0;
  int num_rows = 0;
  int num_cols    = 0;

  status = PyArg_ParseTuple( args, "O", &po_covar);
  if (status == 0) {
    error = 1;
    goto do_exit;
  }
  covar = DAArray_AsDouble2D( &po_covar, &num_rows, &num_cols);
  
  if (covar == NULL) {
    error = 1;
    goto do_exit;
  }

  status = fix_covariance_matrix(covar, num_rows, num_cols);
  
do_free_covar:   DAArray_Free( po_covar        , covar        );
do_exit:
  if (error == 1)  {  return NULL;  }
  else if (error == 2)  {  return PyErr_NoMemory();  }
  else  { return PyInt_FromLong(status); }
}

Extending Python (hard way)

Extending Python (easy way)


def match(matrix_ref):
  cdef Graph graph
  cdef int size
  cdef long total_weight
 
  matrix_ref = Numeric.asarray(matrix_ref)
  graph = num2graph(&size, matrix_ref, &total_weight)

  score, best_match = weighted_match(&size, graph, &total_weight)
  adjacency_matrix = makeAdjacencyMatrix(matrix_ref, best_match)
  return score, adjacency_matrix

Extending Python (easy way)

Need to declare C data structs


cdef extern from "graphtypes.h":
  cdef struct edge_ent

  cdef struct node_entry:
    int degree
    int label
    int x
    int y
    edge_ent *adj_list
  ctypedef node_entry *Graph

Pyrex

Pyrex Standalone

http://www.freenet.org.nz/python/embeddingpyrex/


# hellopyx.pyx
cdef extern from "Python.h":
  # embedding funcs
  void Py_Initialize()
  void Py_Finalize()
  void PySys_SetArgv(int argc, char **argv)

cdef public void inithellopyx()

Pyrex Standalone

http://www.freenet.org.nz/python/embeddingpyrex/


cdef public int main(int argc, char **argv):
  Py_Initialize()
  PySys_SetArgv(argc, argv)
  inithellopyx()

  # python code
  print "Hello world"

  Py_Finalize()

Pyrex Standalone

http://www.freenet.org.nz/python/embeddingpyrex/


$ pyrexc hellopyx.pyx
$ gcc -o hellopyx -I/usr/include/python2.3 -lpython2.3 hellopyx.c
$ ./hellopyx
Hello world

Others

Ran out of time