/* * File: fileiomod.c * Author: Chris * * Created on February 4, 2009, 5:17 PM */ #include #include #include /* * parse args into PyObject * check type for PyString or PyFile * **/ int BUFSIZE = 30; //4096 // prints the first BUFSIZE characters from a file or file/string IO object static PyObject * print_from_file(PyObject *self, PyObject * args) { int res; char buffer[BUFSIZE]; FILE * infile; PyObject * py_tmp = NULL; //PyFileObject * py_infile; //PyStringObject * py_infilename; if(!PyArg_ParseTuple(args, "O", &py_tmp)) return NULL; if(PyFile_Check(py_tmp)) { // it's a file IO //py_infile = py_tmp; infile = PyFile_AsFile(py_tmp); }else if(PyString_Check(py_tmp)) { // it's a string //py_infilename = py_tmp; char* fname = PyString_AsString(py_tmp); infile = fopen(fname, "r"); }else { return NULL; } res=fread(buffer, 1, BUFSIZE, infile); if(!res) return NULL; printf("%s\n", buffer); fclose(infile); return Py_None; } static PyMethodDef FileIOMeths[] = { {"printbuf", print_from_file, METH_VARARGS, "Print some text from the file/fileio."}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initfileiomod(void) { (void) Py_InitModule("fileiomod", FileIOMeths); }