Skip to content

Commit d5964d0

Browse files
committed
fix module bundle problem
1 parent bdb0a8a commit d5964d0

File tree

6 files changed

+228
-167
lines changed

6 files changed

+228
-167
lines changed

Package.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@ let package = Package(
88
products: [
99
.library(
1010
name: "Python-iOS",
11-
targets: ["libpython3", "libssl", "libcrypto", "libffi", "PythonSupport"]),
11+
targets: [ // order matters!
12+
"LinkPython",
13+
"libpython3", "libssl", "libcrypto", "libffi",
14+
"PythonSupport",
15+
]),
1216
],
1317
targets: [
1418
.binaryTarget(name: "libpython3", url: "https://github.com/kewlbear/Python-iOS/releases/download/0.0.1/libpython3.xcframework.zip", checksum: "245c51a97eda854a7b0c7bd507f24d1dfc2efae38f20aceac91fe0fd99a6eebe"),
1519
.binaryTarget(name: "libssl", url: "https://github.com/kewlbear/Python-iOS/releases/download/0.0.1/libssl.xcframework.zip", checksum: "1fc7dd3e95d5152812bc8d74450fdc45539e4ac53d4008b21b7f0a81d2fc52a9"),
1620
.binaryTarget(name: "libcrypto", url: "https://github.com/kewlbear/Python-iOS/releases/download/0.0.1/libcrypto.xcframework.zip", checksum: "43948ae2aac97bf80445ad0e38dd943b4f903506802633bd82d8b813da0328bf"),
1721
.binaryTarget(name: "libffi", url: "https://github.com/kewlbear/Python-iOS/releases/download/0.0.1/libffi.xcframework.zip", checksum: "0b028a1068f5f085ba1861b73928b99d14970438639d2531c3f31afe4d0e0e3c"),
18-
.target(name: "PythonSupport",
22+
.target(name: "LinkPython",
1923
dependencies: [
2024
"libpython3",
2125
"libssl",
2226
"libcrypto",
2327
"libffi",
2428
],
25-
resources: [.copy("lib")],
2629
linkerSettings: [
2730
.linkedLibrary("z"),
2831
.linkedLibrary("sqlite3"),
2932
]
3033
),
34+
.target(name: "PythonSupport",
35+
dependencies: ["LinkPython"],
36+
resources: [.copy("lib")]),
3137
.testTarget(
3238
name: "PythonTests",
3339
dependencies: ["PythonSupport"]),

Sources/LinkPython/PythonSupport.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// PythonSupport.m
3+
//
4+
// Copyright (c) 2021 Changbeom Ahn
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
//
24+
25+
#import <Python.h>
26+
#include <dlfcn.h>
27+
28+
#import "PythonSupport.h"
29+
30+
int PythonInitialize(int argc, const char **argv, const char *custom_builtin_importer)
31+
{
32+
int ret = 0;
33+
34+
Py_Initialize();
35+
36+
wchar_t** python_argv = PyMem_RawMalloc(sizeof(wchar_t *) *argc);
37+
for (int i = 0; i < argc; i++)
38+
python_argv[i] = Py_DecodeLocale(argv[i], NULL);
39+
PySys_SetArgv(argc, python_argv);
40+
41+
// If other modules are using the thread, we need to initialize them before.
42+
PyEval_InitThreads();
43+
44+
// Add an importer for builtin modules
45+
PyRun_SimpleString(custom_builtin_importer);
46+
47+
return ret;
48+
}
49+
50+
void PythonFinalize()
51+
{
52+
Py_Finalize();
53+
}
54+
55+
void PythonRunSimpleString(const char *string)
56+
{
57+
PyRun_SimpleString(string);
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// PythonSupport.h
3+
//
4+
// Copyright (c) 2021 Changbeom Ahn
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
//
24+
25+
#import <Foundation/Foundation.h>
26+
27+
NS_ASSUME_NONNULL_BEGIN
28+
29+
int PythonInitialize(int argc, const char **argv, const char *);
30+
31+
void PythonFinalize();
32+
33+
void PythonRunSimpleString(const char *);
34+
35+
NS_ASSUME_NONNULL_END

Sources/PythonSupport/PythonSupport.m

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

0 commit comments

Comments
 (0)