Skip to content

Commit c95a712

Browse files
QQ
Q
authored and
Q
committed
init
0 parents  commit c95a712

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python setup.py install --user
Binary file not shown.
36.8 KB
Binary file not shown.

fputsmodule.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <Python.h>
2+
3+
static PyObject *method_fputs(PyObject *self, PyObject *args) {
4+
char *str, *filename = NULL;
5+
int bytes_copied = -1;
6+
7+
/* Parse arguments */
8+
if(!PyArg_ParseTuple(args, "ss", &str, &filename)) {
9+
return NULL;
10+
}
11+
12+
FILE *fp = fopen(filename, "w");
13+
bytes_copied = fputs(str, fp);
14+
fclose(fp);
15+
16+
return PyLong_FromLong(bytes_copied);
17+
}
18+
19+
20+
static PyMethodDef FputsMethods[] = {
21+
{"fputs", method_fputs, METH_VARARGS, "Python interface for fputs C library function"},
22+
{NULL, NULL, 0, NULL}
23+
};
24+
25+
26+
static struct PyModuleDef fputsmodule = {
27+
PyModuleDef_HEAD_INIT,
28+
"fputs",
29+
"Python interface for the fputs C library function",
30+
-1,
31+
FputsMethods
32+
};
33+
34+
35+
PyMODINIT_FUNC PyInit_fputs(void) {
36+
/* Assign module value */
37+
PyObject *module = PyModule_Create(&fputsmodule);
38+
39+
/* Add int constant by name */
40+
PyModule_AddIntConstant(module, "FPUTS_FLAG", 64);
41+
42+
/* Define int macro */
43+
#define FPUTS_MACRO 256
44+
45+
/* Add macro to module */
46+
PyModule_AddIntMacro(module, FPUTS_MACRO);
47+
48+
return module;
49+
}
50+

setup.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from distutils.core import setup, Extension
2+
3+
def main():
4+
setup(name="fputs",
5+
version="1.0.0",
6+
description="Python interface for the fputs C library function",
7+
author="<your name>",
8+
author_email="[email protected]",
9+
ext_modules=[Extension("fputs", ["fputsmodule.c"])])
10+
11+
if __name__ == "__main__":
12+
main()
13+
14+

0 commit comments

Comments
 (0)