Skip to content

Commit d6ade58

Browse files
committed
SimpleString->SimpleStringFull, SimpleStringN -> SimpleString, docs, widechar support, removed debug
1 parent f86c8dd commit d6ade58

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

c/helloworld.c

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@
2020
#include <stdlib.h>
2121
*/
2222

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
2329
int Initialize() {
2430
Py_Initialize();
31+
mainModule = PyImport_AddModule("__main__");
2532
return ZF_SUCCESS;
2633
}
2734

2835
int Finalize() {
36+
Py_DECREF(mainModule);
2937
Py_Finalize();
3038
return ZF_SUCCESS;
3139
}
3240

41+
42+
// Test method, returns random double
3343
int GetRandom(double* random) {
3444

3545
Py_Initialize();
@@ -56,6 +66,7 @@ int GetRandom(double* random) {
5666
return ZF_SUCCESS; // set the exit status code
5767
}
5868

69+
// Test method, returns random double
5970
int GetRandomSimple(double* random) {
6071

6172
Py_Initialize();
@@ -68,11 +79,11 @@ int GetRandomSimple(double* random) {
6879
*random = PyFloat_AsDouble(var);
6980
Py_Finalize();
7081

71-
// set value to be returned by the $ZF function call
72-
return ZF_SUCCESS; // set the exit status code
82+
return ZF_SUCCESS;
7383
}
7484

75-
int SimpleString(char *command, double* result) {
85+
// Does complete initialization, executes code and finalizes environment
86+
int SimpleStringFull(char *command, double* result) {
7687

7788
Py_Initialize();
7889
PyRun_SimpleString(command);
@@ -83,58 +94,50 @@ int SimpleString(char *command, double* result) {
8394
*result = PyFloat_AsDouble(var);
8495
Py_Finalize();
8596

86-
// set value to be returned by the $ZF function call
87-
return ZF_SUCCESS; // set the exit status code
97+
return ZF_SUCCESS;
8898
}
8999

90-
int SimpleStringN(char *command, char *resultVar, char* result) {
100+
// Assumes initialized environment
101+
int SimpleString(char *command, char *resultVar, char* result) {
91102
PyRun_SimpleString(command);
92103

93-
PyObject *mainModule = PyImport_AddModule("__main__");
94-
95-
//char varName = *resultVar;
96-
97104
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);
103105

104-
sprintf(result,"%s",s);
106+
//PyObject* varStr = PyObject_Repr(var);
107+
PyObject* varStr = PyObject_Str(var);
108+
char* str = PyUnicode_AsUTF8(varStr);
105109

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);
110111

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;
114115
}
115116

117+
// Code for testing and debugging as an executable
116118
int main(int argc, char **argv) {
117119
printf("X: ");
118-
//exec_interactive_interpreter(argc, argv);
120+
119121
//double random = 0;
120122
//GetRandom(&random);
121123
//GetRandomSimple(&random);
122-
// printf("%lf", random);
123-
char* result = malloc(sizeof(char) * 1024);
124+
//printf("%lf", random);
124125

126+
char* result = malloc(sizeof(char) * 1024);
125127

126128
Initialize();
127-
SimpleStringN("x=2", "x", result);
129+
SimpleString("x=2", "x", result);
128130
Finalize();
129131

130132
printf(result);
131-
return 0;
133+
return EXIT_SUCCESS;
132134
}
133135

134136
ZFBEGIN
135137
ZFENTRY("Initialize","",Initialize)
136138
ZFENTRY("Finalize","",Finalize)
137139
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)
140143
ZFEND

isc/py/Callout.cls

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,44 @@ ClassMethod GetRandom() As %Double
2020
quit random
2121
}
2222

23+
/// Test method. Get random number
24+
/// w ##class(isc.py.Callout).GetRandomSimple()
25+
ClassMethod GetRandomSimple() As %Double
26+
{
27+
set path = ..#DLL
28+
set random = $ZF(-3, path, "GetRandomSimple")
29+
quit random
30+
}
31+
2332
/// Init, eval code and return variable x.
24-
/// w ##class(isc.py.Callout).SimpleString()
25-
ClassMethod SimpleString(code = {"import random;" _ $$$NL _ "x=random.random();"}) As %Double
33+
/// w ##class(isc.py.Callout).SimpleStringFull()
34+
ClassMethod SimpleStringFull(code = {"import random;" _ $$$NL _ "x=random.random();"}) As %Double
2635
{
36+
set converted = $$$NO
37+
if $ZISWIDE(code) {
38+
set code = $zcvt(code, "O", "UTF8")
39+
set converted = $$$YES
40+
}
2741
set path = ..#DLL
28-
set result = $ZF(-3, path, "SimpleString", code)
42+
set result = $ZF(-3, path, "SimpleStringFull", code)
43+
set:converted result = $zcvt(result, "I", "UTF8")
2944
quit result
3045
}
3146

3247
/// Eval code vaiable in initialized context and
33-
/// return value of variable (currently limited to double and similar)
34-
/// w ##class(isc.py.Callout).SimpleStringN()
35-
ClassMethod SimpleStringN(code As %String = {"import random;" _ $$$NL _ "x=random.random();"}, variable As %String = "") As %Double
48+
/// return value of variable str evaluation
49+
/// w ##class(isc.py.Callout).SimpleString()
50+
ClassMethod SimpleString(code As %String = {"import random;" _ $$$NL _ "x=random.random();"}, variable As %String = "x") As %Double
3651
{
52+
set converted = $$$NO
53+
if $ZISWIDE(code) {
54+
set code = $zcvt(code, "O", "UTF8")
55+
set converted = $$$YES
56+
}
3757
set path = ..#DLL
38-
set result = $ZF(-3, path, "SimpleStringN", code, variable)
58+
set result = $ZF(-3, path, "SimpleString", code, variable)
59+
60+
set:converted result = $zcvt(result, "I", "UTF8")
3961
quit result
4062
}
4163

@@ -48,7 +70,7 @@ ClassMethod Finalize() As %Integer
4870
}
4971

5072
/// Unload library
51-
/// write ##class(Utils.Callout).Unload()
73+
/// write ##class(isc.py.Callout).Unload()
5274
ClassMethod Unload() As %Integer
5375
{
5476
set result = $ZF(-3, "")

0 commit comments

Comments
 (0)