EXTEST.C file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Python.h"
#define BUFSIZE 10
int FAC (int n) {
if (n < 2)
return 1;
return n * FAC (N-1);
}
Char *reverse (char *s) {
Register char t;
char *p = s;
Char *q = (s + (strlen (s)-1));
while (P < q) {
t = *p;
*p++ = *q;
*q--= t;
}
return s;
}
Static Pyobject *
EXTEST_FAC (Pyobject *self, Pyobject *args) {
int res;
int num;
pyobject* retval;
res = pyarg_parsetuple (args, "I", &num);
if (!res) {
return NULL;
}
RES = FAC (num);
retval = (Pyobject *) py_buildvalue ("I", res);
return retval;
}
Static Pyobject *
Extest_reverse (Pyobject *self, Pyobject *args) {
Char *orignal;
if (! ( Pyarg_parsetuple (args, "s", &orignal))) {
return NULL;
}
Return (Pyobject *) py_buildvalue ("s", Reverse (orignal));
}
Static Pyobject *
Extest_doppel (Pyobject *self, Pyobject *args) {
Char *orignal;
Char *RESV;
Pyobject *retval;
if (! ( Pyarg_parsetuple (args, "s", &orignal))) {
return NULL;
}
retval = (Pyobject *) Py_buildvalue ("ss", Orignal, Resv=reverse (StrDup (orignal)));
Free (RESV);
return retval;
}
Static Pymethoddef
Extestmethods[] = {
{"FAC", EXTEST_FAC, Meth_varargs},
{"Doppel", Extest_doppel, Meth_varargs},
{"Reverse", Extest_reverse, Meth_varargs},
{null, NULL},
};
void Initextest () {
Py_initmodule ("Extest", extestmethods);
}
int main () {
Char S[bufsize];
printf ("4! = =%d\n ", FAC (4));
printf ("8! = =%d\n ", FAC (8));
printf ("12! = =%d\n ", FAC (12));
strcpy (S, "abcdef");
printf ("Reversing ' abcdef ', we get '%s ' \ n", reverse (s));
strcpy (S, "Madam");
printf ("Reversing ' madam ', we get '%s ' \ n", reverse (s));
return 0;
}
Modify the Python installation directory under the lib\distutils\msvc9compiler.py file (if necessary msvccompiler.py the file will also need to make corresponding changes, depending on the system), find Get_build_ The version method directly return 9.0
Then find the Find_vcvarsall method to return the path of the Vcvarsall.bat directly (whichever is the path after the installation of your own machine)
return R ' C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C + + for Python\9.0\vcvarsall.bat ' Both changes are loaded with the substring string, and you can
To create the setup.py file, enter the following:
#!/usr/bin/env python
From Distutils.core import Setup, Extension
MOD = ' Extest '
Setup (Name=mod, Ext_modules=[extension (MOD, sources=[' extest.c ')])
Then enter the Python setup.py build in the corresponding prompt of Visual C + + for Python to build the C and Python version of the library file or python setup.py install
Of course, you can also create a Windows binary package
Python setup.py bdist_wininst
Microsoft Visual C + + Compiler for Python 2.7