@@ -27,6 +27,12 @@ import WinSDK
27
27
// The `PythonLibrary` struct that loads Python symbols at runtime.
28
28
//===----------------------------------------------------------------------===//
29
29
30
+ #if canImport(Darwin)
31
+ let RTLD_DEFAULT = UnsafeMutableRawPointer ( bitPattern: - 2 )
32
+ #else
33
+ let RTLD_DEFAULT : UnsafeMutableRawPointer ? = nil
34
+ #endif
35
+
30
36
public struct PythonLibrary {
31
37
private static let shared = PythonLibrary ( )
32
38
private static let pythonInitializeSymbolName = " Py_Initialize "
@@ -160,6 +166,10 @@ private extension PythonLibrary {
160
166
static var libraryVersionSeparator = " "
161
167
#endif
162
168
169
+ private static var isPythonLibraryPreloaded : Bool {
170
+ return self . loadSymbol ( RTLD_DEFAULT, self . pythonInitializeSymbolName) != nil
171
+ }
172
+
163
173
static let libraryPaths : [ String ] = {
164
174
var libraryPaths : [ String ] = [ ]
165
175
for librarySearchPath in librarySearchPaths {
@@ -173,14 +183,10 @@ private extension PythonLibrary {
173
183
}
174
184
return libraryPaths
175
185
} ( )
176
-
177
- static var isPythonLibraryLoaded : Bool {
178
- return self . loadSymbol ( nil , self . pythonInitializeSymbolName) != nil
179
- }
180
-
186
+
181
187
static func loadPythonLibrary( ) -> UnsafeMutableRawPointer ? {
182
- if self . isPythonLibraryLoaded {
183
- return nil
188
+ if self . isPythonLibraryPreloaded {
189
+ return RTLD_DEFAULT
184
190
}
185
191
else if let pythonLibraryPath = Environment . library. value {
186
192
return self . loadPythonLibrary ( at: pythonLibraryPath)
0 commit comments