Skip to content

Commit f073082

Browse files
committed
Set up PYTHONHOME dynamically.
1 parent 33e3f43 commit f073082

11 files changed

+185
-64
lines changed

.appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ install:
3535
# Build and install xeus-python
3636
- mkdir build
3737
- cd build
38-
- cmake -G "NMake Makefiles" -D CMAKE_INSTALL_PREFIX=%MINICONDA%\\Library -D XEXTRA_JUPYTER_DATA_DIR=%MINICONDA%\\share\\jupyter -D BUILD_TESTS=ON ..
38+
- cmake -G "NMake Makefiles" -D CMAKE_INSTALL_PREFIX=%MINICONDA%\\Library -D XEXTRA_JUPYTER_DATA_DIR=%MINICONDA%\\share\\jupyter -D BUILD_TESTS=ON -D XEUS_PYTHONHOME_RELPATH=..\\ ..
3939
- nmake
4040
- nmake install
4141
# Install test dependencies

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ install:
6464
# Build and install xeus-python
6565
- mkdir build
6666
- cd build
67-
- cmake -D CMAKE_INSTALL_PREFIX=$HOME/miniconda -D DOWNLOAD_GTEST=ON -D PYTHON_EXECUTABLE=`which python` -D XEUS_PYTHONHOME="$HOME/miniconda" -D CMAKE_INSTALL_LIBDIR=lib -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX ..
67+
- cmake -D CMAKE_INSTALL_PREFIX=$HOME/miniconda -D DOWNLOAD_GTEST=ON -D PYTHON_EXECUTABLE=`which python` -D CMAKE_INSTALL_LIBDIR=lib -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX ..
6868
- make -j2 install
6969
script:
7070
# For Python 2, activate the kernel_test Python3 environment
7171
- if [[ "$PY" == "2" ]]; then
7272
source activate kernel_test;
7373
jupyter kernelspec install --sys-prefix ../share/jupyter/kernels/xpython;
74+
source activate root;
7475
fi
7576
- cd test
7677
- ./test_xeus_python

CMakeLists.txt

+14-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ set(XEUS_PYTHON_SRC
105105
src/xlinecache.hpp
106106
src/xptvsd_client.cpp
107107
src/xptvsd_client.hpp
108-
src/xpythonhome.cpp
109108
src/xstream.cpp
110109
src/xstream.hpp
111110
src/xtraceback.cpp
@@ -118,14 +117,21 @@ set(XEUS_PYTHON_HEADERS
118117
include/xeus-python/xdebugger.hpp
119118
include/xeus-python/xeus_python_config.hpp
120119
include/xeus-python/xinterpreter.hpp
121-
include/xeus-python/xpythonhome.hpp
120+
)
121+
122+
set(XPYTHON_SRC
123+
src/main.cpp
124+
src/xpythonhome.hpp
125+
src/xpythonhome.cpp
126+
src/xpaths.hpp
127+
src/xpaths.cpp
122128
)
123129

124130
# xeus-python is the target for the library
125131
add_library(xeus-python SHARED ${XEUS_PYTHON_SRC} ${XEUS_PYTHON_HEADERS})
126132

127133
# xpython is the target for the kernel executable
128-
add_executable(xpython src/main.cpp)
134+
add_executable(xpython ${XPYTHON_SRC})
129135
set_target_properties(xpython PROPERTIES ENABLE_EXPORTS 1)
130136
target_link_libraries(xpython PRIVATE pybind11::pybind11 xeus-python INTERFACE pybind11_json)
131137

@@ -158,7 +164,11 @@ set_target_properties(xeus-python PROPERTIES
158164
SOVERSION ${XPYT_VERSION_MAJOR}
159165
OUTPUT_NAME "libxeus-python")
160166

161-
target_compile_definitions(xeus-python PUBLIC "XEUS_PYTHON_EXPORTS" XEUS_PYTHONHOME="${PYTHON_PREFIX}")
167+
target_compile_definitions(xeus-python PUBLIC "XEUS_PYTHON_EXPORTS")
168+
169+
if (XEUS_PYTHONHOME_RELPATH)
170+
target_compile_definitions(xpython PRIVATE XEUS_PYTHONHOME_RELPATH=${XEUS_PYTHONHOME_RELPATH})
171+
endif()
162172

163173
#########
164174
# Tests #

include/xeus-python/xpythonhome.hpp

-40
This file was deleted.

src/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
#include "pybind11/pybind11.h"
2121

2222
#include "xeus-python/xinterpreter.hpp"
23-
#include "xeus-python/xpythonhome.hpp"
2423
#include "xeus-python/xdebugger.hpp"
2524

25+
#include "xpythonhome.hpp"
26+
2627
namespace py = pybind11;
2728

2829
std::string extract_filename(int& argc, char* argv[])

src/xpaths.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay and *
3+
* Wolf Vollprecht *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#include "xpaths.hpp"
11+
12+
#include <string>
13+
#include <cstring>
14+
15+
#if defined(__linux__)
16+
# include <unistd.h>
17+
#endif
18+
#if defined(_WIN32)
19+
# if defined(NOMINMAX)
20+
# include <windows.h>
21+
# else
22+
# define NOMINMAX
23+
# include <windows.h>
24+
# undef NOMINMAX
25+
# endif
26+
#endif
27+
#ifdef __APPLE__
28+
# include <cstdint>
29+
# include <mach-o/dyld.h>
30+
#endif
31+
#if defined(__sun)
32+
# include <stdlib.h>
33+
#endif
34+
35+
namespace xpyt
36+
{
37+
std::string executable_path()
38+
{
39+
std::string path;
40+
char buffer[1024];
41+
std::memset(buffer, '\0', sizeof(buffer));
42+
#if defined(__linux__)
43+
if (readlink("/proc/self/exe", buffer, sizeof(buffer)) != -1)
44+
{
45+
path = buffer;
46+
}
47+
else
48+
{
49+
// failed to determine run path
50+
}
51+
#elif defined (_WIN32)
52+
if (GetModuleFileName(nullptr, buffer, sizeof(buffer)) != 0)
53+
{
54+
path = buffer;
55+
}
56+
else
57+
{
58+
// failed to determine run path
59+
}
60+
#elif defined (__APPLE__)
61+
std::uint32_t size = sizeof(buffer);
62+
if(_NSGetExecutablePath(buffer, &size) == 0)
63+
{
64+
path = buffer;
65+
}
66+
else
67+
{
68+
// failed to determine run path
69+
}
70+
#elif defined (__FreeBSD__)
71+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
72+
if (sysctl(mib, 4, buffer, sizeof(buffer), NULL, 0) != -1)
73+
{
74+
path = buffer;
75+
}
76+
else
77+
{
78+
// failed to determine run path
79+
}
80+
#elif defined(__sun)
81+
path = getexecname();
82+
#endif
83+
return path;
84+
}
85+
86+
std::string prefix_path()
87+
{
88+
std::string path = executable_path();
89+
#if defined (_WIN32)
90+
char separator = '\\';
91+
#else
92+
char separator = '/';
93+
#endif
94+
return path.substr(0, path.find_last_of("/\\")) + separator + ".." + separator;
95+
}
96+
}

src/xpaths.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3+
* Copyright (c) QuantStack *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#ifndef XPYT_PATHS_HPP
11+
#define XPYT_PATHS_HPP
12+
13+
#include <string>
14+
15+
namespace xpyt
16+
{
17+
/*******************
18+
* executable_path *
19+
*******************/
20+
21+
std::string executable_path();
22+
23+
/*******************
24+
* prefix_path *
25+
*******************/
26+
27+
std::string prefix_path();
28+
}
29+
#endif

src/xpythonhome.cpp

+22-11
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,33 @@
77
* The full license is in the file LICENSE, distributed with this software. *
88
****************************************************************************/
99

10-
#include "xeus-python/xpythonhome.hpp"
10+
#include "pybind11/pybind11.h"
11+
12+
#include <iostream>
13+
#include <string>
14+
15+
#include "xeus-python/xeus_python_config.hpp"
16+
17+
#include "xpythonhome.hpp"
18+
#include "xpaths.hpp"
1119

1220
namespace xpyt
1321
{
14-
// This functino must not be inlined in the header.
15-
// get_pythonhome and set_pythonhome must remain
16-
// in different compilation units to avoid compiler
17-
// optimization in set_pythonhome that results
18-
// in a relocation issue with conda.
19-
const char* get_pythonhome()
22+
void set_pythonhome()
2023
{
21-
#ifdef XEUS_PYTHONHOME
22-
return XEUS_PYTHONHOME;
24+
// The XEUS_PYTHONHOME_RELPATH compile-time definition can be used.
25+
// To specify the PYTHONHOME location as a relative path to the PREFIX.
26+
#if defined(XEUS_PYTHONHOME_RELPATH)
27+
static const std::string pythonhome = prefix_path() + XPYT_STRINGIFY(XEUS_PYTHONHOME_RELPATH);
2328
#else
24-
return "";
29+
static const std::string pythonhome = prefix_path();
30+
#endif
31+
32+
#if PY_MAJOR_VERSION == 2
33+
Py_SetPythonHome(const_cast<char*>(pythonhome.c_str()));
34+
#else
35+
static const std::wstring wstr(pythonhome.cbegin(), pythonhome.cend());;
36+
Py_SetPythonHome(const_cast<wchar_t*>(wstr.c_str()));
2537
#endif
2638
}
2739
}
28-

src/xpythonhome.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay and *
3+
* Wolf Vollprecht *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#ifndef XEUS_PYTHONHOME_HPP
11+
#define XEUS_PYTHONHOME_HPP
12+
13+
namespace xpyt
14+
{
15+
void set_pythonhome();
16+
}
17+
#endif
18+

test/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ find_package(Threads)
7777
include_directories(${GTEST_INCLUDE_DIRS} SYSTEM)
7878

7979
set(XEUS_PYTHON_TESTS
80-
../src/xpythonhome.cpp
8180
../src/xutils.cpp
8281
test_debugger.cpp
8382
xeus_client.hpp

test/test_debugger.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,7 @@ void start_kernel()
779779
dump_connection_file();
780780
std::thread kernel([]()
781781
{
782-
#if WIN32
783-
std::string cmd = ".\\..\\xpython -f " + KERNEL_JSON + "&";
784-
#else
785-
std::string cmd = "./../xpython -f " + KERNEL_JSON + "&";
786-
#endif
782+
std::string cmd = "xpython -f " + KERNEL_JSON + "&";
787783
int ret2 = std::system(cmd.c_str());
788784
});
789785
std::this_thread::sleep_for(2s);

0 commit comments

Comments
 (0)