Skip to content

Commit 2214a08

Browse files
pvieitorxwei
authored andcommitted
Added support for loading Python at runtime (supports both Python 3 and 2) (#20674)
* Added new PythonLibrary struct to load Python library and symbols dynamically at runtime - This adds support for both Python 3 and 2 with the same codebase. - You can force a version of Python with the PYTHON_VERSION environment variable, a Python library path with PYTHON_LIBRARY and enable logging with PYTHON_LOADER_LOGGING. - The implementation comes from the PythonKit Swift package (https://github.com/pvieito/PythonKit) removing the Foundation dependency. * Style and structure changes * Adding support for setting a Python version on-process * Default minor version to nil * Cleaned private method * Update CMakeLists adding new files and removing CPython module * Force setting an non-nil value type for Linux * Redefined Python symbols to avoid libSyntax bug (SR-9316) * Update tests * `init(owning:)` should retain, while `init(borrowing:)` should not. * Use loaded symbol values instead of their addresses as the type argument to `isType(_:type:)`. * Make loaded symbols `let`s since they are themselves the address we need * Minor clean-up. - `()` -> `Void`. - Drop parentheses at the result position from single-result function signatures. - Replace !(==) with != in `isType(_:type:)`. * Delete `_Py_TrueStructb` * Fix ParsableInterface tests
1 parent bcf82dc commit 2214a08

File tree

11 files changed

+453
-288
lines changed

11 files changed

+453
-288
lines changed

stdlib/public/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ if(SWIFT_BUILD_STDLIB)
5959
endif()
6060

6161
# SWIFT_ENABLE_TENSORFLOW
62-
find_package(PythonLibs 2.7 EXACT)
63-
if(SWIFT_BUILD_STDLIB AND PYTHONLIBS_FOUND)
64-
add_subdirectory(CPython)
62+
if(SWIFT_BUILD_STDLIB)
6563
add_subdirectory(Python)
6664
endif()
6765

stdlib/public/CPython/CMakeLists.txt

Lines changed: 0 additions & 73 deletions
This file was deleted.

stdlib/public/CPython/module.modulemap

Lines changed: 0 additions & 162 deletions
This file was deleted.

stdlib/public/Python/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#
1515
#===----------------------------------------------------------------------===#
1616

17-
find_package(PythonLibs 2.7 EXACT REQUIRED)
18-
message(STATUS "Building Python.")
19-
2017
set(SWIFT_PYTHON_EXISTS TRUE PARENT_SCOPE)
2118

2219
set(SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES)
@@ -29,9 +26,10 @@ endif()
2926

3027
add_swift_target_library(swiftPython ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
3128
Python.swift
29+
PythonLibrary.swift
30+
PythonLibrary+Symbols.swift
3231
NumpyConversion.swift
3332

34-
DEPENDS cpython_modulemap
3533
TARGET_SDKS OSX CYGWIN FREEBSD LINUX HAIKU
3634
SWIFT_MODULE_DEPENDS_IOS Darwin
3735
SWIFT_MODULE_DEPENDS_OSX Darwin
@@ -42,5 +40,4 @@ add_swift_target_library(swiftPython ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS
4240
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
4341
SWIFT_MODULE_DEPENDS_HAIKU Glibc
4442
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
45-
PRIVATE_LINK_LIBRARIES "${PYTHON_LIBRARIES}"
4643
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")

stdlib/public/Python/NumpyConversion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

0 commit comments

Comments
 (0)