Skip to content

Commit d43e5e9

Browse files
committed
Initialization is now done automatically on C side of things
1 parent b3c6af1 commit d43e5e9

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

c/iscpython.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <cdzf.h>
1414
#include <Python.h>
15+
#include <stdbool.h>
1516

1617
#undef ERROR
1718
/*
@@ -24,17 +25,28 @@
2425
// Reference to scope in which top-level code executes.
2526
PyObject *mainModule;
2627

28+
// Current state of Python Environment.
29+
// Should be managed by C code automatically.
30+
// Relevant for SimpleString method
31+
bool isInitialized = false;
32+
2733
// Initializes Python environment
2834
// and obtains reference to the main module, to be used by
2935
int Initialize() {
30-
Py_Initialize();
31-
mainModule = PyImport_AddModule("__main__");
36+
if (isInitialized == false) {
37+
Py_Initialize();
38+
mainModule = PyImport_AddModule("__main__");
39+
isInitialized = true;
40+
}
3241
return ZF_SUCCESS;
3342
}
3443

3544
int Finalize() {
36-
Py_DECREF(mainModule);
37-
Py_Finalize();
45+
if (isInitialized) {
46+
isInitialized = false;
47+
Py_DECREF(mainModule);
48+
Py_Finalize();
49+
}
3850
return ZF_SUCCESS;
3951
}
4052

@@ -99,6 +111,11 @@ int SimpleStringFull(char *command, double* result) {
99111

100112
// Assumes initialized environment
101113
int SimpleString(char *command, char *resultVar, char* result) {
114+
115+
if (isInitialized == false) {
116+
Initialize();
117+
}
118+
102119
PyRun_SimpleString(command);
103120

104121
int exists = PyObject_HasAttrString(mainModule, resultVar);
@@ -139,10 +156,10 @@ int main(int argc, char **argv) {
139156
}
140157

141158
ZFBEGIN
142-
ZFENTRY("Initialize","",Initialize)
143-
ZFENTRY("Finalize","",Finalize)
144-
ZFENTRY("GetRandom","D",GetRandom)
145-
ZFENTRY("GetRandomSimple","D",GetRandomSimple)
146-
ZFENTRY("SimpleStringFull","cD",SimpleStringFull)
147-
ZFENTRY("SimpleString","ccC",SimpleString)
159+
ZFENTRY("Initialize","",Initialize)
160+
ZFENTRY("Finalize","",Finalize)
161+
ZFENTRY("GetRandom","D",GetRandom)
162+
ZFENTRY("GetRandomSimple","D",GetRandomSimple)
163+
ZFENTRY("SimpleStringFull","cD",SimpleStringFull)
164+
ZFENTRY("SimpleString","ccC",SimpleString)
148165
ZFEND

isc/py/Callout.cls

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/// Main callout wrapper.
22
/// To use:
33
/// 1. Call: do ##class(isc.py.Callout).Setup() once per systems start
4-
/// 2. Call: do ##class(isc.py.Callout).Initialize() once per process
5-
/// 3. Call main method (can be called many times, context persists): write ##class(isc.py.Callout).SimpleString(code, data)
6-
/// 4. Call: do ##class(isc.py.Callout).Finalize() to free Python context
7-
/// 5. Call: write ##class(isc.py.Callout).Unload() to free callout library
4+
/// 2. Call main method (can be called many times, context persists): write ##class(isc.py.Callout).SimpleString(code, data)
5+
/// 3. Call: do ##class(isc.py.Callout).Finalize() to clear Python context
6+
/// 4. Call: write ##class(isc.py.Callout).Unload() to free callout library
87
Class isc.py.Callout
98
{
109

@@ -22,6 +21,8 @@ Parameter SimpleStringFull As Integer = 5;
2221

2322
Parameter SimpleString As Integer = 6;
2423

24+
/// Get path to DLL.
25+
/// DLL assumed to be in bin folder, unless specified otherwise
2526
ClassMethod GetDLL() [ CodeMode = expression ]
2627
{
2728
$g(^isc.py.Callout, $g(^%SYS("bindir")) _ "iscpython." _ $select($$$isWINDOWS:"dll", 1:"so"))
@@ -38,12 +39,14 @@ ClassMethod Setup()
3839
}
3940

4041
/// Init Python context. Idempotent.
42+
/// Currently this is done automatically on a C side of things.
43+
/// Left for debugging/dev purposes.
4144
/// do ##class(isc.py.Callout).Initialize()
4245
ClassMethod Initialize(debug As %Boolean = {$$$NO}) As %Integer
4346
{
4447
if debug {
45-
set sc = $ZF(-4,4,..#PyLibId) // unload current copy of inputlib
46-
set sc = $ZF(-4,8) // delete existing process index, if any
48+
set sc = $ZF(-4,4,..#PyLibId) // unload current copy of inputlib
49+
set sc = $ZF(-4,8) // delete existing process index, if any
4750
set sc = $ZF(-4,7,..#PyLibId, ..GetDLL()) // override system index
4851
}
4952
do $ZF(-6, ..#PyLibId, ..#Initialize)
@@ -81,6 +84,7 @@ ClassMethod SimpleStringFull(code = {"import random;" _ $$$NL _ "x=random.random
8184

8285
/// Eval code vaiable in initialized context and
8386
/// return value of variable str evaluation
87+
/// TODO determine wide variable value.
8488
/// write ##class(isc.py.Callout).SimpleString()
8589
ClassMethod SimpleString(code As %String = {"import random;" _ $$$NL _ "x=random.random();"}, variable As %String = "x") As %Double
8690
{

isc/py/ens/OutboundAdapter.cls

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
Class isc.py.ens.OutboundAdapter Extends Ens.OutboundAdapter
22
{
33

4-
/// Do requred in-process initialization.
5-
/// System-wide initialization: do ##class(isc.py.Callout).Setup()
6-
/// assumed to be done before this point.
7-
Method OnInit() As %Status
8-
{
9-
do ##class(isc.py.Callout).Initialize()
10-
quit $$$OK
11-
}
12-
134
/// Free Python library
145
Method OnTearDown() As %Status
156
{

0 commit comments

Comments
 (0)