1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#undef _DEBUG
#include <python.h>
#include <iostream>
int main(){
Py_Initialize();
//FILE *file=fopen("hello.py","r");
//PyRun_SimpleFile(file,"hello.py");
//std::cout <<PyRun_SimpleString("print \"Hello, World!\"\nwait")<<std::endl;
PyObject *str=PyByteArray_FromStringAndSize("Hello,\0W\norld!\n",15),
*filename=PyString_FromString("hello"),
*module=PyImport_Import(filename);
Py_DECREF(filename);
if (!module){
Py_DECREF(str);
return 1;
}
PyObject *function=PyObject_GetAttrString(module,"f"),
*function2=PyObject_GetAttrString(module,"f2"),
*args=PyTuple_New(1);
if (function && PyCallable_Check(function)){
PyTuple_SetItem(args,0,str);
PyObject_CallObject(function,args);
Py_DECREF(args);
Py_DECREF(function);
Py_DECREF(str);
}
if (function && PyCallable_Check(function2)){
for (char a=0;a<10;a++){
PyObject *return_value=PyObject_CallObject(function2,0);
if (return_value){
printf("f() returned %d.\n",PyInt_AsLong(return_value));
Py_DECREF(return_value);
}
}
Py_DECREF(function2);
}
Py_DECREF(module);
Py_Finalize();
//fclose(file);
system("pause"); //Yeah, I know. I was being lazy.
return 0;
}
//hello.py (FIOC makes me want to throw up):
#print "Hello, World!"
def f(a):
for x in a:
print x
call=0
def fibonacci(n):
if n<2:
return 1
a=1
b=1
c=0
for d in range(2,n+1):
c=a+b
a=b
b=c
return c
def f2():
global call
a=fibonacci(call)
call+=1
return a
|