Skip to content
This repository was archived by the owner on Apr 21, 2023. It is now read-only.

Commit 0db3f1c

Browse files
authored
Merge pull request pvieito#31 from pvieito/pull/30
Pull/30
2 parents 35c799c + 41f16bc commit 0db3f1c

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

PythonKit/PythonLibrary.swift

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,30 @@ import WinSDK
2929

3030
public struct PythonLibrary {
3131
private static let shared = PythonLibrary()
32+
private static let pythonInitializeSymbolName = "Py_Initialize"
3233
private static let pythonLegacySymbolName = "PyString_AsString"
3334
private static var librarySymbolsLoaded = false
3435

35-
private let pythonLibraryHandle: UnsafeMutableRawPointer
36+
private let pythonLibraryHandle: UnsafeMutableRawPointer?
3637
private let isLegacyPython: Bool
3738

3839
private init() {
39-
guard let pythonLibraryHandle = PythonLibrary.loadPythonLibrary() else {
40-
fatalError("""
41-
Python library not found. Set the \(Environment.library.key) \
42-
environment variable with the path to a Python library.
43-
""")
44-
}
45-
self.pythonLibraryHandle = pythonLibraryHandle
40+
self.pythonLibraryHandle = PythonLibrary.loadPythonLibrary()
4641

4742
// Check if Python is legacy (Python 2)
48-
isLegacyPython = PythonLibrary.loadSymbol(
49-
pythonLibraryHandle,
50-
PythonLibrary.pythonLegacySymbolName) != nil
43+
self.isLegacyPython = Self.loadSymbol(pythonLibraryHandle, PythonLibrary.pythonLegacySymbolName) != nil
5144
if isLegacyPython {
52-
PythonLibrary.log(
53-
"Loaded legacy Python library, using legacy symbols...")
45+
PythonLibrary.log("Loaded legacy Python library, using legacy symbols...")
5446
}
5547
PythonLibrary.librarySymbolsLoaded = true
5648
}
5749

5850
static func loadSymbol(
59-
_ libraryHandle: UnsafeMutableRawPointer, _ name: String) -> UnsafeMutableRawPointer? {
51+
_ libraryHandle: UnsafeMutableRawPointer?, _ name: String) -> UnsafeMutableRawPointer? {
6052
#if canImport(Darwin) || canImport(Glibc)
6153
return dlsym(libraryHandle, name)
6254
#elseif os(Windows)
55+
guard let libraryHandle = libraryHandle else { return nil }
6356
let moduleHandle = libraryHandle
6457
.assumingMemoryBound(to: HINSTANCE__.self)
6558
let moduleSymbol = GetProcAddress(moduleHandle, name)
@@ -75,10 +68,7 @@ public struct PythonLibrary {
7568
}
7669

7770
log("Loading symbol '\(name)' from the Python library...")
78-
return unsafeBitCast(
79-
loadSymbol(PythonLibrary.shared.pythonLibraryHandle, name),
80-
to: type
81-
)
71+
return unsafeBitCast(loadSymbol(PythonLibrary.shared.pythonLibraryHandle, name), to: type)
8272
}
8373
}
8474

@@ -184,11 +174,17 @@ private extension PythonLibrary {
184174
return libraryPaths
185175
}()
186176

177+
static var isPythonLibraryLoaded: Bool {
178+
return self.loadSymbol(nil, self.pythonInitializeSymbolName) != nil
179+
}
180+
187181
static func loadPythonLibrary() -> UnsafeMutableRawPointer? {
188-
if let pythonLibraryPath = Environment.library.value {
189-
return loadPythonLibrary(at: pythonLibraryPath)
182+
if self.isPythonLibraryLoaded {
183+
return nil
184+
}
185+
else if let pythonLibraryPath = Environment.library.value {
186+
return self.loadPythonLibrary(at: pythonLibraryPath)
190187
}
191-
192188
for majorVersion in supportedMajorVersions {
193189
for minorVersion in supportedMinorVersions {
194190
for libraryPath in libraryPaths {
@@ -201,7 +197,10 @@ private extension PythonLibrary {
201197
}
202198
}
203199
}
204-
return nil
200+
fatalError("""
201+
Python library not found. Set the \(Environment.library.key) \
202+
environment variable with the path to a Python library.
203+
""")
205204
}
206205

207206
static func loadPythonLibrary(
@@ -221,7 +220,7 @@ private extension PythonLibrary {
221220
.joined(separator: libraryVersionSeparator)
222221
let path = path.split(separator: libraryPathVersionCharacter)
223222
.joined(separator: libraryVersionString)
224-
return loadPythonLibrary(at: path)
223+
return self.loadPythonLibrary(at: path)
225224
}
226225

227226
static func loadPythonLibrary(at path: String) -> UnsafeMutableRawPointer? {

PythonTool/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ struct PythonTool: ParsableCommand {
1717
}
1818

1919
@Flag(name: .shortAndLong, help: "List installed modules.")
20-
var list: Bool
20+
var list: Bool = false
2121

2222
@Flag(name: .shortAndLong, help: "List Python paths.")
23-
var path: Bool
23+
var path: Bool = false
2424

2525
@Flag(name: .shortAndLong, help: "Verbose mode.")
26-
var verbose: Bool
26+
var verbose: Bool = false
2727

2828
func run() throws {
2929
do {

0 commit comments

Comments
 (0)