Skip to content

Commit

Permalink
Added support for loading Python at runtime (supports both Python 3 a…
Browse files Browse the repository at this point in the history
…nd 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
  • Loading branch information
pvieito authored and rxwei committed Dec 4, 2018
1 parent bcf82dc commit 2214a08
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 288 deletions.
4 changes: 1 addition & 3 deletions stdlib/public/CMakeLists.txt
Expand Up @@ -59,9 +59,7 @@ if(SWIFT_BUILD_STDLIB)
endif()

# SWIFT_ENABLE_TENSORFLOW
find_package(PythonLibs 2.7 EXACT)
if(SWIFT_BUILD_STDLIB AND PYTHONLIBS_FOUND)
add_subdirectory(CPython)
if(SWIFT_BUILD_STDLIB)
add_subdirectory(Python)
endif()

Expand Down
73 changes: 0 additions & 73 deletions stdlib/public/CPython/CMakeLists.txt

This file was deleted.

162 changes: 0 additions & 162 deletions stdlib/public/CPython/module.modulemap

This file was deleted.

7 changes: 2 additions & 5 deletions stdlib/public/Python/CMakeLists.txt
Expand Up @@ -14,9 +14,6 @@
#
#===----------------------------------------------------------------------===#

find_package(PythonLibs 2.7 EXACT REQUIRED)
message(STATUS "Building Python.")

set(SWIFT_PYTHON_EXISTS TRUE PARENT_SCOPE)

set(SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES)
Expand All @@ -29,9 +26,10 @@ endif()

add_swift_target_library(swiftPython ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
Python.swift
PythonLibrary.swift
PythonLibrary+Symbols.swift
NumpyConversion.swift

DEPENDS cpython_modulemap
TARGET_SDKS OSX CYGWIN FREEBSD LINUX HAIKU
SWIFT_MODULE_DEPENDS_IOS Darwin
SWIFT_MODULE_DEPENDS_OSX Darwin
Expand All @@ -42,5 +40,4 @@ add_swift_target_library(swiftPython ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
SWIFT_MODULE_DEPENDS_HAIKU Glibc
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
PRIVATE_LINK_LIBRARIES "${PYTHON_LIBRARIES}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")
2 changes: 1 addition & 1 deletion stdlib/public/Python/NumpyConversion.swift
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down

0 comments on commit 2214a08

Please sign in to comment.