-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Python 3 support. #18645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Python 3 support. #18645
Conversation
# Generating a directory should be idempotent; otherwise, calling | ||
# `add_swift_library` twice in the same stdlib directory (where source | ||
# files are all Swift, no GYB) fails. | ||
IDEMPOTENT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change (adding IDEMPOTENT
) should be upstreamed to master
.
// | ||
// Conditionally adding the linker flags here seems to solve the issue. | ||
// This is robust assuming that toolchain artifacts are not manipulated | ||
// (so that somehow Python.swiftmodule exists while libswiftPython.so | ||
// (so that somehow Python2.swiftmodule exists while libswiftPython2.so | ||
// doesn't). | ||
// | ||
// https://github.com/google/swift/issues/4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't check if the original linker error still persists. Perhaps we can remove this code now?
8b1b522
to
5467eea
Compare
This approach creates two pairs of CPython and Python modules: - `CPython2` and `Python2` - `CPython3` and `Python3` `Python2` and `Python3` compile the same source code, `stdilb/public/Python/Python.swift`. This separation of the Python modules is not ideal for other modules that want to support both Python 2 and 3. Assuming pre-built packages will be released with support for both Python 2 and 3, modules importing Python could be compiled with either a `-DPYTHON2` or `-DPYTHON3` flag. Example: ```swift // Default to Python 2. // Specify -DPYTHON3 to use Python 3. #if canImport(Python2) && PYTHON2 import Python2 #elseif canImport(Python3) import Python3 #else #error("Toolchain does not support Python2 or Python3.") #endif // Use same `PythonObject` APIs. ``` TODO: - Add Python 3 tests.
5467eea
to
985674f
Compare
Todo: make the For symbols defined by Python 2 but not Python 3 (like Instead of this: #if PYTHON2
let pyStringCastFunction = PyString_AsString
#elseif PYTHON3
let pyStringCastFunction = PyUnicode_AsUTF8
#endif
guard let cString = pyStringCastFunction(pyObject) else { Do this: #if PYTHON3
internal let PyString_AsString = PyUnicode_AsUTF8
#endif
guard let cString = PyString_AsString(pyObject) else { |
As a technical curiosity, to which library path will the For example, when using the python.org installer the library is installed at Would we have to rely on DYLD |
@pvieito The Python 3 library path is determined by the following CMake logic: set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
find_package(PythonLibs "${python_version}" REQUIRED) On my Mac with Homebrew Python 3.7, the library path successfully resolves to:
I believe the CMake logic can find the python.org installer path, too ( |
@dan-zheng Yes, but my question is not from a compile-time perspective but from a runtime one. When the prebuilt binaries are distributed they would only be linked to the Python version (and path) that was found at build time. This is not a problem on Python 2, as its path is guaranteed by the OS but it will be problematic for Python 3 as it can be found in various locations and versions. This is one of the problems that could be solved by dynamically linking the Python library at runtime with |
#20674 introduces a flexible, runtime approach for Python 3 support. Hurray! |
Note: This PR should not be merged until Python 3 support is discussed with the community.
This approach for adding Python 3 support creates two pairs of CPython and Python modules:
CPython2
andPython2
CPython3
andPython3
Python2
andPython3
compile the same source code,stdilb/public/Python/Python.swift
.This separation of the Python modules is not ideal for other modules that want
to support both Python 2 and 3. Assuming pre-built packages will be released
with support for both Python 2 and 3, modules importing Python could be
compiled with either a
-DPYTHON2
or-DPYTHON3
flag.Example:
Todo:
RUN
lines.