20
20
#include <stdlib.h>
21
21
*/
22
22
23
+
24
+ // Reference to scope in which top-level code executes.
25
+ PyObject * mainModule ;
26
+
27
+ // Initializes Python environment
28
+ // and obtains reference to the main module, to be used by
23
29
int Initialize () {
24
30
Py_Initialize ();
31
+ mainModule = PyImport_AddModule ("__main__" );
25
32
return ZF_SUCCESS ;
26
33
}
27
34
28
35
int Finalize () {
36
+ Py_DECREF (mainModule );
29
37
Py_Finalize ();
30
38
return ZF_SUCCESS ;
31
39
}
32
40
41
+
42
+ // Test method, returns random double
33
43
int GetRandom (double * random ) {
34
44
35
45
Py_Initialize ();
@@ -56,6 +66,7 @@ int GetRandom(double* random) {
56
66
return ZF_SUCCESS ; // set the exit status code
57
67
}
58
68
69
+ // Test method, returns random double
59
70
int GetRandomSimple (double * random ) {
60
71
61
72
Py_Initialize ();
@@ -68,11 +79,11 @@ int GetRandomSimple(double* random) {
68
79
* random = PyFloat_AsDouble (var );
69
80
Py_Finalize ();
70
81
71
- // set value to be returned by the $ZF function call
72
- return ZF_SUCCESS ; // set the exit status code
82
+ return ZF_SUCCESS ;
73
83
}
74
84
75
- int SimpleString (char * command , double * result ) {
85
+ // Does complete initialization, executes code and finalizes environment
86
+ int SimpleStringFull (char * command , double * result ) {
76
87
77
88
Py_Initialize ();
78
89
PyRun_SimpleString (command );
@@ -83,58 +94,50 @@ int SimpleString(char *command, double* result) {
83
94
* result = PyFloat_AsDouble (var );
84
95
Py_Finalize ();
85
96
86
- // set value to be returned by the $ZF function call
87
- return ZF_SUCCESS ; // set the exit status code
97
+ return ZF_SUCCESS ;
88
98
}
89
99
90
- int SimpleStringN (char * command , char * resultVar , char * result ) {
100
+ // Assumes initialized environment
101
+ int SimpleString (char * command , char * resultVar , char * result ) {
91
102
PyRun_SimpleString (command );
92
103
93
- PyObject * mainModule = PyImport_AddModule ("__main__" );
94
-
95
- //char varName = *resultVar;
96
-
97
104
PyObject * var = PyObject_GetAttrString (mainModule , resultVar );
98
- //*result = PyFloat_AsDouble(var);
99
-
100
- //PyObject* objectsRepresentation = PyObject_Repr(var);
101
- PyObject * objectsRepresentation = PyObject_Str (var );
102
- char * s = PyUnicode_AsUTF8 (objectsRepresentation );
103
105
104
- sprintf (result ,"%s" ,s );
106
+ //PyObject* varStr = PyObject_Repr(var);
107
+ PyObject * varStr = PyObject_Str (var );
108
+ char * str = PyUnicode_AsUTF8 (varStr );
105
109
106
- //strcpy (result, s);
107
- //result = s;
108
- // TODO
109
- // https://stackoverflow.com/questions/5356773/python-get-string-representation-of-pyobject
110
+ sprintf (result , "%s" , str );
110
111
111
-
112
- // set value to be returned by the $ZF function call
113
- return ZF_SUCCESS ; // set the exit status code
112
+ Py_DECREF ( varStr );
113
+ Py_DECREF ( var );
114
+ return ZF_SUCCESS ;
114
115
}
115
116
117
+ // Code for testing and debugging as an executable
116
118
int main (int argc , char * * argv ) {
117
119
printf ("X: " );
118
- //exec_interactive_interpreter(argc, argv);
120
+
119
121
//double random = 0;
120
122
//GetRandom(&random);
121
123
//GetRandomSimple(&random);
122
- // printf("%lf", random);
123
- char * result = malloc (sizeof (char ) * 1024 );
124
+ //printf("%lf", random);
124
125
126
+ char * result = malloc (sizeof (char ) * 1024 );
125
127
126
128
Initialize ();
127
- SimpleStringN ("x=2" , "x" , result );
129
+ SimpleString ("x=2" , "x" , result );
128
130
Finalize ();
129
131
130
132
printf (result );
131
- return 0 ;
133
+ return EXIT_SUCCESS ;
132
134
}
133
135
134
136
ZFBEGIN
135
137
ZFENTRY ("Initialize" ,"" ,Initialize )
136
138
ZFENTRY ("Finalize" ,"" ,Finalize )
137
139
ZFENTRY ("GetRandom" ,"D" ,GetRandom )
138
- ZFENTRY ("SimpleString" ,"cD" ,SimpleString )
139
- ZFENTRY ("SimpleStringN" ,"ccC" ,SimpleStringN )
140
+ ZFENTRY ("GetRandomSimple" ,"D" ,GetRandomSimple )
141
+ ZFENTRY ("SimpleStringFull" ,"cD" ,SimpleStringFull )
142
+ ZFENTRY ("SimpleString" ,"ccC" ,SimpleString )
140
143
ZFEND
0 commit comments