Support both Python 2 and Python 3 in Python extension example. (#2495)

This commit is contained in:
Nikolay Igotti
2018-12-24 20:30:45 +03:00
committed by GitHub
parent 16d8c5e55f
commit 104cbb199b
4 changed files with 38 additions and 5 deletions
+2 -2
View File
@@ -17,13 +17,13 @@ To build and run the sample do the following:
* On macOS copy Kotlin binary to extension's directory and change install name with * On macOS copy Kotlin binary to extension's directory and change install name with
`install_name_tool` tool, i.e. `install_name_tool` tool, i.e.
``` ```
sudo cp libserver.dylib /Library/Python/2.7/site-packages/ sudo cp ./build/libserver.dylib /Library/Python/2.7/site-packages/
sudo install_name_tool /Library/Python/2.7/site-packages/kotlin_bridge.so \ sudo install_name_tool /Library/Python/2.7/site-packages/kotlin_bridge.so \
-change libserver.dylib @loader_path/libserver.dylib -change libserver.dylib @loader_path/libserver.dylib
``` ```
* On Linux copy Kotlin binary in some place where libraries could be loaded from, i.e. * On Linux copy Kotlin binary in some place where libraries could be loaded from, i.e.
``` ```
cp libserver.so /usr/local/lib/ cp ./build/libserver.so /usr/local/lib/
ldconfig ldconfig
``` ```
or modify dynamic loader search path with or modify dynamic loader search path with
+1
View File
@@ -11,6 +11,7 @@ fi
KONAN_USER_DIR=${KONAN_DATA_DIR:-"$HOME/.konan"} KONAN_USER_DIR=${KONAN_DATA_DIR:-"$HOME/.konan"}
KONAN_DEPS="$KONAN_USER_DIR/dependencies" KONAN_DEPS="$KONAN_USER_DIR/dependencies"
# python3 shall work as well.
PYTHON=python PYTHON=python
mkdir -p $DIR/build mkdir -p $DIR/build
@@ -108,6 +108,38 @@ static PyMethodDef kotlin_bridge_funcs[] = {
{ NULL } { NULL }
}; };
#if PY_MAJOR_VERSION >= 3
struct module_state {
server_kref_demo_Server server;
};
static int kotlin_bridge_traverse(PyObject *m, visitproc visit, void *arg) {
return 0;
}
static int kotlin_bridge_clear(PyObject *m) {
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"kotlin_bridge",
NULL,
sizeof(struct module_state),
kotlin_bridge_funcs,
NULL,
kotlin_bridge_traverse,
kotlin_bridge_clear,
NULL
};
PyMODINIT_FUNC PyInit_kotlin_bridge(void) {
PyObject *module = PyModule_Create(&moduledef);
return module;
}
#else
void initkotlin_bridge(void) { void initkotlin_bridge(void) {
Py_InitModule3("kotlin_bridge", kotlin_bridge_funcs, "Kotlin/Native example module"); Py_InitModule3("kotlin_bridge", kotlin_bridge_funcs, "Kotlin/Native example module");
} }
#endif
@@ -9,13 +9,13 @@ import kotlin_bridge
session = kotlin_bridge.open_session(239, 'konan') session = kotlin_bridge.open_session(239, 'konan')
message = kotlin_bridge.greet_server(session) message = kotlin_bridge.greet_server(session)
print "Greet '%s'" % (message) print("Greet '{}'".format(message))
message = kotlin_bridge.concat_server(session, "Coding", "fun") message = kotlin_bridge.concat_server(session, "Coding", "fun")
print "Concat '%s'" % (message) print("Concat '{}'".format(message))
message = kotlin_bridge.add_server(session, 1, 60) message = kotlin_bridge.add_server(session, 1, 60)
print "Sum '%d'" % (message) print("Sum '{}'".format(message))
kotlin_bridge.close_session(session) kotlin_bridge.close_session(session)