18
18
import Darwin
19
19
#elseif canImport(Glibc)
20
20
import Glibc
21
+ #elseif canImport(Musl)
22
+ import Musl
21
23
#elseif os(Windows)
22
24
import CRT
23
25
import WinSDK
@@ -45,13 +47,11 @@ public struct PythonLibrary {
45
47
private static let pythonInitializeSymbolName = " Py_Initialize "
46
48
private static let pythonLegacySymbolName = " PyString_AsString "
47
49
48
- #if canImport(Darwin)
50
+ #if canImport(Darwin)
49
51
private static let defaultLibraryHandle = UnsafeMutableRawPointer ( bitPattern: - 2 ) // RTLD_DEFAULT
50
- #elseif canImport(Glibc)
52
+ #else
51
53
private static let defaultLibraryHandle : UnsafeMutableRawPointer ? = nil // RTLD_DEFAULT
52
- #elseif os(Windows)
53
- private static let defaultLibraryHandle : UnsafeMutableRawPointer ? = nil // Unsupported
54
- #endif
54
+ #endif
55
55
56
56
private static var isPythonLibraryLoaded = false
57
57
private static var _pythonLibraryHandle : UnsafeMutableRawPointer ?
@@ -81,14 +81,14 @@ public struct PythonLibrary {
81
81
82
82
internal static func loadSymbol< T> (
83
83
name: String , legacyName: String ? = nil , type: T . Type = T . self) -> T {
84
- var name = name
85
- if let legacyName = legacyName, self . isLegacyPython {
86
- name = legacyName
84
+ var name = name
85
+ if let legacyName = legacyName, self . isLegacyPython {
86
+ name = legacyName
87
+ }
88
+
89
+ log ( " Loading symbol ' \( name) ' from the Python library... " )
90
+ return unsafeBitCast ( self . loadSymbol ( self . pythonLibraryHandle, name) , to: type)
87
91
}
88
-
89
- log ( " Loading symbol ' \( name) ' from the Python library... " )
90
- return unsafeBitCast ( self . loadSymbol ( self . pythonLibraryHandle, name) , to: type)
91
- }
92
92
}
93
93
94
94
// Methods of `PythonLibrary` required to load the Python library.
@@ -98,30 +98,30 @@ extension PythonLibrary {
98
98
99
99
private static let libraryPathVersionCharacter : Character = " : "
100
100
101
- #if canImport(Darwin)
101
+ #if canImport(Darwin)
102
102
private static var libraryNames = [ " Python.framework/Versions/:/Python " ]
103
103
private static var libraryPathExtensions = [ " " ]
104
104
private static var librarySearchPaths = [ " " , " /opt/homebrew/Frameworks/ " , " /usr/local/Frameworks/ " ]
105
105
private static var libraryVersionSeparator = " . "
106
- #elseif os(Linux)
106
+ #elseif os(Linux)
107
107
private static var libraryNames = [ " libpython: " , " libpython:m " ]
108
108
private static var libraryPathExtensions = [ " .so " ]
109
109
private static var librarySearchPaths = [ " " ]
110
110
private static var libraryVersionSeparator = " . "
111
- #elseif os(Windows)
111
+ #elseif os(Windows)
112
112
private static var libraryNames = [ " python: " ]
113
113
private static var libraryPathExtensions = [ " .dll " ]
114
114
private static var librarySearchPaths = [ " " ]
115
115
private static var libraryVersionSeparator = " "
116
- #endif
116
+ #endif
117
117
118
118
private static let libraryPaths : [ String ] = {
119
119
var libraryPaths : [ String ] = [ ]
120
120
for librarySearchPath in librarySearchPaths {
121
121
for libraryName in libraryNames {
122
122
for libraryPathExtension in libraryPathExtensions {
123
123
let libraryPath =
124
- librarySearchPath + libraryName + libraryPathExtension
124
+ librarySearchPath + libraryName + libraryPathExtension
125
125
libraryPaths. append ( libraryPath)
126
126
}
127
127
}
@@ -131,22 +131,22 @@ extension PythonLibrary {
131
131
132
132
private static func loadSymbol(
133
133
_ libraryHandle: UnsafeMutableRawPointer ? , _ name: String ) -> UnsafeMutableRawPointer ? {
134
- #if canImport(Darwin) || canImport(Glibc )
135
- return dlsym ( libraryHandle, name )
136
- #elseif os(Windows)
137
- guard let libraryHandle = libraryHandle else { return nil }
138
- let moduleHandle = libraryHandle
139
- . assumingMemoryBound ( to: HINSTANCE__ . self)
140
- let moduleSymbol = GetProcAddress ( moduleHandle , name )
141
- return unsafeBitCast ( moduleSymbol , to : UnsafeMutableRawPointer ? . self )
142
- #endif
143
- }
134
+ #if os(Windows )
135
+ guard let libraryHandle = libraryHandle else { return nil }
136
+ let moduleHandle = libraryHandle
137
+ . assumingMemoryBound ( to : HINSTANCE__ . self )
138
+ let moduleSymbol = GetProcAddress ( moduleHandle , name )
139
+ return unsafeBitCast ( moduleSymbol , to: UnsafeMutableRawPointer ? . self)
140
+ #else
141
+ return dlsym ( libraryHandle , name )
142
+ #endif
143
+ }
144
144
145
145
private static func isPythonLibraryLoaded( at pythonLibraryHandle: UnsafeMutableRawPointer ? = nil ) -> Bool {
146
146
let pythonLibraryHandle = pythonLibraryHandle ?? self . defaultLibraryHandle
147
147
return self . loadSymbol ( pythonLibraryHandle, self . pythonInitializeSymbolName) != nil
148
148
}
149
-
149
+
150
150
private static func loadPythonLibrary( ) -> UnsafeMutableRawPointer ? {
151
151
let pythonLibraryHandle : UnsafeMutableRawPointer ?
152
152
if self . isPythonLibraryLoaded ( ) {
@@ -168,7 +168,7 @@ extension PythonLibrary {
168
168
let version = PythonVersion ( major: majorVersion, minor: minorVersion)
169
169
guard let pythonLibraryHandle = loadPythonLibrary (
170
170
at: libraryPath, version: version) else {
171
- continue
171
+ continue
172
172
}
173
173
return pythonLibraryHandle
174
174
}
@@ -179,33 +179,33 @@ extension PythonLibrary {
179
179
180
180
private static func loadPythonLibrary(
181
181
at path: String , version: PythonVersion ) -> UnsafeMutableRawPointer ? {
182
- let versionString = version. versionString
183
-
184
- if let requiredPythonVersion = Environment . version. value {
185
- let requiredMajorVersion = Int ( requiredPythonVersion)
186
- if requiredPythonVersion != versionString,
187
- requiredMajorVersion != version. major {
188
- return nil
182
+ let versionString = version. versionString
183
+
184
+ if let requiredPythonVersion = Environment . version. value {
185
+ let requiredMajorVersion = Int ( requiredPythonVersion)
186
+ if requiredPythonVersion != versionString,
187
+ requiredMajorVersion != version. major {
188
+ return nil
189
+ }
189
190
}
191
+
192
+ let libraryVersionString = versionString
193
+ . split ( separator: PythonVersion . versionSeparator)
194
+ . joined ( separator: libraryVersionSeparator)
195
+ let path = path. split ( separator: libraryPathVersionCharacter)
196
+ . joined ( separator: libraryVersionString)
197
+ return self . loadPythonLibrary ( at: path)
190
198
}
191
-
192
- let libraryVersionString = versionString
193
- . split ( separator: PythonVersion . versionSeparator)
194
- . joined ( separator: libraryVersionSeparator)
195
- let path = path. split ( separator: libraryPathVersionCharacter)
196
- . joined ( separator: libraryVersionString)
197
- return self . loadPythonLibrary ( at: path)
198
- }
199
199
200
200
private static func loadPythonLibrary( at path: String ) -> UnsafeMutableRawPointer ? {
201
201
self . log ( " Trying to load library at ' \( path) '... " )
202
- #if canImport(Darwin) || canImport(Glibc)
202
+ #if os(Windows)
203
+ let pythonLibraryHandle = UnsafeMutableRawPointer ( LoadLibraryA ( path) )
204
+ #else
203
205
// Must be RTLD_GLOBAL because subsequent .so files from the imported python
204
206
// modules may depend on this .so file.
205
207
let pythonLibraryHandle = dlopen ( path, RTLD_LAZY | RTLD_GLOBAL)
206
- #elseif os(Windows)
207
- let pythonLibraryHandle = UnsafeMutableRawPointer ( LoadLibraryA ( path) )
208
- #endif
208
+ #endif
209
209
210
210
if pythonLibraryHandle != nil {
211
211
self . log ( " Library at ' \( path) ' was successfully loaded. " )
@@ -291,11 +291,11 @@ extension PythonLibrary {
291
291
}
292
292
293
293
func set( _ value: String ) {
294
- #if canImport(Darwin) || canImport(Glibc)
295
- setenv ( key, value, 1 )
296
- #elseif os(Windows)
294
+ #if os(Windows)
297
295
_putenv_s ( key, value)
298
- #endif
296
+ #else
297
+ setenv ( key, value, 1 )
298
+ #endif
299
299
}
300
300
}
301
301
}
0 commit comments