Repository navigation
Add a function to check if the PythonLibrary is valid to avoid Fatal Error during importing of "sys" - #55
Add a function to check if the PythonLibrary is valid to avoid Fatal Error during importing of "sys"#55MichaelPlug wants to merge 2 commits into
Conversation
|
Could you add a test case for that to PythonRuntimeTests.swift? It doesn't mean you have to run the tests in an environment with/without Python 2. You just need to assert that the function compiles and executes without crashing on all platforms. Furthermore, call that function again at the end of an existing test. The dedicated test case might end up always being the first to execute. Thus, it would never validate whether the function crashes after the runtime has been fired up. One more suggestion - what about refactoring the function into a computed get-only property called |
|
In which test function should I add this function? Sorry, it's not clear to me. It's look that functions useLibrary and useVersion are never called. @philipturner |
|
Why is
Just
Those functions are dependent on the computer's configuration and don't always work. For example, PythonKit crashed when I tried calling |
|
Hi! Please see this commit: 6e2c366 There is now a new |
|
The reason I asked to have someone validate my attempt at starting a review (#55 (comment)) has nothing to do with this bug in particular. Could someone on this thread tell me whether they see the review? Edit: I would like both other people to validate, because GitHub's settings might be different for the repository owner. |
|
@philipturner To "publish" your review you have to click in the "Finish Review" button. |
|
|
||
| public static func libraryExists() -> Bool { | ||
| let pythonLibraryHandleTest = Self.loadPythonLibrary() | ||
| return Self.isPythonLibraryLoaded(at: pythonLibraryHandleTest) | ||
| } | ||
|
|
There was a problem hiding this comment.
| public static func libraryExists() -> Bool { | |
| let pythonLibraryHandleTest = Self.loadPythonLibrary() | |
| return Self.isPythonLibraryLoaded(at: pythonLibraryHandleTest) | |
| } | |
| public static var libraryExists: Bool { | |
| let pythonLibraryHandleTest = Self.loadPythonLibrary() | |
| return Self.isPythonLibraryLoaded(at: pythonLibraryHandleTest) | |
| } | |
|
I had no idea that was how you did a review!!! I wish I knew that months ago! Thanks. |
|
Great solution, thank you very much for your work |
|
I'm trying to use the new function With this code I would expect that the final value of |
|
It is working for me: import PythonKit
var pythonIsWorking = false
PythonLibrary.useLibrary(at: "/invalid/path")
do {
try PythonLibrary.loadLibrary()
pythonIsWorking = true
}
catch {
print("Error 1:", error)
PythonLibrary.useLibrary(at: "/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Python")
do {
try PythonLibrary.loadLibrary()
pythonIsWorking = true
}
catch {
print("Error 2:", error)
pythonIsWorking = false
}
}
print("Python is Working: \(pythonIsWorking)")$ PythonTool
Error 1: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
Python is Working: true |
|
I was too hasty, I'm trying to explain the problem better. The correct example is: But if I do this it works: It's seems that for my issue a simple solution is try before the function useLibary() and then useVersion(), but probably this stuff can be improved. Thank you for your help and I'm sorry if I didn't explained the problem clearly, |
|
Gotcha! Since the PYTHON_LIBRARY environment variable takes preference over the PYTHON_VERSION variable, I have just added support for clearing both variables in this commit: PythonLibrary.useVersion(nil)
PythonLibrary.useLibrary(at: nil) |

I added a new function to check if the PythonLibrary is valid or not, to avoid the fatal error during the importing of sys and to try a different path to import PythonLibrary.
I think it is a useful function because from MacOs 12.3 we can't assume that all users have Python 2 on their MacBook at the same path.