1818import Darwin
1919#elseif canImport(Glibc)
2020import Glibc
21+ #elseif canImport(Musl)
22+ import Musl
2123#elseif os(Windows)
2224import CRT
2325import WinSDK
@@ -45,13 +47,11 @@ public struct PythonLibrary {
4547 private static let pythonInitializeSymbolName = " Py_Initialize "
4648 private static let pythonLegacySymbolName = " PyString_AsString "
4749
48- #if canImport(Darwin)
50+ #if canImport(Darwin)
4951 private static let defaultLibraryHandle = UnsafeMutableRawPointer ( bitPattern: - 2 ) // RTLD_DEFAULT
50- #elseif canImport(Glibc)
52+ #else
5153 private static let defaultLibraryHandle : UnsafeMutableRawPointer ? = nil // RTLD_DEFAULT
52- #elseif os(Windows)
53- private static let defaultLibraryHandle : UnsafeMutableRawPointer ? = nil // Unsupported
54- #endif
54+ #endif
5555
5656 private static var isPythonLibraryLoaded = false
5757 private static var _pythonLibraryHandle : UnsafeMutableRawPointer ?
@@ -81,14 +81,14 @@ public struct PythonLibrary {
8181
8282 internal static func loadSymbol< T> (
8383 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)
8791 }
88-
89- log ( " Loading symbol ' \( name) ' from the Python library... " )
90- return unsafeBitCast ( self . loadSymbol ( self . pythonLibraryHandle, name) , to: type)
91- }
9292}
9393
9494// Methods of `PythonLibrary` required to load the Python library.
@@ -98,30 +98,30 @@ extension PythonLibrary {
9898
9999 private static let libraryPathVersionCharacter : Character = " : "
100100
101- #if canImport(Darwin)
101+ #if canImport(Darwin)
102102 private static var libraryNames = [ " Python.framework/Versions/:/Python " ]
103103 private static var libraryPathExtensions = [ " " ]
104104 private static var librarySearchPaths = [ " " , " /opt/homebrew/Frameworks/ " , " /usr/local/Frameworks/ " ]
105105 private static var libraryVersionSeparator = " . "
106- #elseif os(Linux)
106+ #elseif os(Linux)
107107 private static var libraryNames = [ " libpython: " , " libpython:m " ]
108108 private static var libraryPathExtensions = [ " .so " ]
109109 private static var librarySearchPaths = [ " " ]
110110 private static var libraryVersionSeparator = " . "
111- #elseif os(Windows)
111+ #elseif os(Windows)
112112 private static var libraryNames = [ " python: " ]
113113 private static var libraryPathExtensions = [ " .dll " ]
114114 private static var librarySearchPaths = [ " " ]
115115 private static var libraryVersionSeparator = " "
116- #endif
116+ #endif
117117
118118 private static let libraryPaths : [ String ] = {
119119 var libraryPaths : [ String ] = [ ]
120120 for librarySearchPath in librarySearchPaths {
121121 for libraryName in libraryNames {
122122 for libraryPathExtension in libraryPathExtensions {
123123 let libraryPath =
124- librarySearchPath + libraryName + libraryPathExtension
124+ librarySearchPath + libraryName + libraryPathExtension
125125 libraryPaths. append ( libraryPath)
126126 }
127127 }
@@ -131,22 +131,22 @@ extension PythonLibrary {
131131
132132 private static func loadSymbol(
133133 _ 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+ }
144144
145145 private static func isPythonLibraryLoaded( at pythonLibraryHandle: UnsafeMutableRawPointer ? = nil ) -> Bool {
146146 let pythonLibraryHandle = pythonLibraryHandle ?? self . defaultLibraryHandle
147147 return self . loadSymbol ( pythonLibraryHandle, self . pythonInitializeSymbolName) != nil
148148 }
149-
149+
150150 private static func loadPythonLibrary( ) -> UnsafeMutableRawPointer ? {
151151 let pythonLibraryHandle : UnsafeMutableRawPointer ?
152152 if self . isPythonLibraryLoaded ( ) {
@@ -168,7 +168,7 @@ extension PythonLibrary {
168168 let version = PythonVersion ( major: majorVersion, minor: minorVersion)
169169 guard let pythonLibraryHandle = loadPythonLibrary (
170170 at: libraryPath, version: version) else {
171- continue
171+ continue
172172 }
173173 return pythonLibraryHandle
174174 }
@@ -179,33 +179,33 @@ extension PythonLibrary {
179179
180180 private static func loadPythonLibrary(
181181 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+ }
189190 }
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)
190198 }
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- }
199199
200200 private static func loadPythonLibrary( at path: String ) -> UnsafeMutableRawPointer ? {
201201 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
203205 // Must be RTLD_GLOBAL because subsequent .so files from the imported python
204206 // modules may depend on this .so file.
205207 let pythonLibraryHandle = dlopen ( path, RTLD_LAZY | RTLD_GLOBAL)
206- #elseif os(Windows)
207- let pythonLibraryHandle = UnsafeMutableRawPointer ( LoadLibraryA ( path) )
208- #endif
208+ #endif
209209
210210 if pythonLibraryHandle != nil {
211211 self . log ( " Library at ' \( path) ' was successfully loaded. " )
@@ -291,11 +291,11 @@ extension PythonLibrary {
291291 }
292292
293293 func set( _ value: String ) {
294- #if canImport(Darwin) || canImport(Glibc)
295- setenv ( key, value, 1 )
296- #elseif os(Windows)
294+ #if os(Windows)
297295 _putenv_s ( key, value)
298- #endif
296+ #else
297+ setenv ( key, value, 1 )
298+ #endif
299299 }
300300 }
301301}
0 commit comments