|
7 | 7 | CONFIG.libptr = Ptr{Cvoid}(parse(UInt, ENV["PYTHONJL_LIBPTR"]))
|
8 | 8 | # Check Python is initialized
|
9 | 9 | C.Py_IsInitialized() == 0 && error("Python is not already initialized.")
|
| 10 | + CONFIG.isinitialized = CONFIG.preinitialized = true |
10 | 11 | else
|
11 | 12 | # Find Python executable
|
12 | 13 | exepath = something(
|
|
86 | 87 | """)
|
87 | 88 | end
|
88 | 89 |
|
89 |
| - # Check we are not already initialized |
90 |
| - C.Py_IsInitialized() == 0 || error("Python is already initialized.") |
91 |
| - |
92 | 90 | # Initialize
|
93 | 91 | with_gil() do
|
94 |
| - # Find ProgramName and PythonHome |
95 |
| - script = if Sys.iswindows() |
96 |
| - """ |
97 |
| - import sys |
98 |
| - print(sys.executable) |
99 |
| - if hasattr(sys, "base_exec_prefix"): |
100 |
| - sys.stdout.write(sys.base_exec_prefix) |
101 |
| - else: |
102 |
| - sys.stdout.write(sys.exec_prefix) |
103 |
| - """ |
| 92 | + if C.Py_IsInitialized() != 0 |
| 93 | + # Already initialized (maybe you're using PyCall as well) |
104 | 94 | else
|
105 |
| - """ |
106 |
| - import sys |
107 |
| - print(sys.executable) |
108 |
| - if hasattr(sys, "base_exec_prefix"): |
109 |
| - sys.stdout.write(sys.base_prefix) |
110 |
| - sys.stdout.write(":") |
111 |
| - sys.stdout.write(sys.base_exec_prefix) |
112 |
| - else: |
113 |
| - sys.stdout.write(sys.prefix) |
114 |
| - sys.stdout.write(":") |
115 |
| - sys.stdout.write(sys.exec_prefix) |
116 |
| - """ |
117 |
| - end |
118 |
| - CONFIG.pyprogname, CONFIG.pyhome = readlines(python_cmd(["-c", script])) |
| 95 | + # Find ProgramName and PythonHome |
| 96 | + script = if Sys.iswindows() |
| 97 | + """ |
| 98 | + import sys |
| 99 | + print(sys.executable) |
| 100 | + if hasattr(sys, "base_exec_prefix"): |
| 101 | + sys.stdout.write(sys.base_exec_prefix) |
| 102 | + else: |
| 103 | + sys.stdout.write(sys.exec_prefix) |
| 104 | + """ |
| 105 | + else |
| 106 | + """ |
| 107 | + import sys |
| 108 | + print(sys.executable) |
| 109 | + if hasattr(sys, "base_exec_prefix"): |
| 110 | + sys.stdout.write(sys.base_prefix) |
| 111 | + sys.stdout.write(":") |
| 112 | + sys.stdout.write(sys.base_exec_prefix) |
| 113 | + else: |
| 114 | + sys.stdout.write(sys.prefix) |
| 115 | + sys.stdout.write(":") |
| 116 | + sys.stdout.write(sys.exec_prefix) |
| 117 | + """ |
| 118 | + end |
| 119 | + CONFIG.pyprogname, CONFIG.pyhome = readlines(python_cmd(["-c", script])) |
119 | 120 |
|
120 |
| - # Set PythonHome |
121 |
| - CONFIG.pyhome_w = Base.cconvert(Cwstring, CONFIG.pyhome) |
122 |
| - C.Py_SetPythonHome(pointer(CONFIG.pyhome_w)) |
| 121 | + # Set PythonHome |
| 122 | + CONFIG.pyhome_w = Base.cconvert(Cwstring, CONFIG.pyhome) |
| 123 | + C.Py_SetPythonHome(pointer(CONFIG.pyhome_w)) |
123 | 124 |
|
124 |
| - # Set ProgramName |
125 |
| - CONFIG.pyprogname_w = Base.cconvert(Cwstring, CONFIG.pyprogname) |
126 |
| - C.Py_SetProgramName(pointer(CONFIG.pyprogname_w)) |
| 125 | + # Set ProgramName |
| 126 | + CONFIG.pyprogname_w = Base.cconvert(Cwstring, CONFIG.pyprogname) |
| 127 | + C.Py_SetProgramName(pointer(CONFIG.pyprogname_w)) |
127 | 128 |
|
128 |
| - # Start the interpreter and register exit hooks |
129 |
| - C.Py_InitializeEx(0) |
| 129 | + # Start the interpreter and register exit hooks |
| 130 | + C.Py_InitializeEx(0) |
| 131 | + atexit() do |
| 132 | + CONFIG.isinitialized = false |
| 133 | + CONFIG.version < v"3.6" ? C.Py_Finalize() : checkm1(C.Py_FinalizeEx()) |
| 134 | + end |
| 135 | + end |
130 | 136 | CONFIG.isinitialized = true
|
131 | 137 | check(
|
132 | 138 | C.Py_AtExit(
|
133 | 139 | @cfunction(() -> (CONFIG.isinitialized = false; nothing), Cvoid, ())
|
134 | 140 | ),
|
135 | 141 | )
|
136 |
| - atexit() do |
137 |
| - CONFIG.isinitialized = false |
138 |
| - CONFIG.version < v"3.6" ? C.Py_Finalize() : checkm1(C.Py_FinalizeEx()) |
139 |
| - end |
140 | 142 | end
|
141 | 143 | end
|
142 | 144 |
|
|
0 commit comments