Skip to content

Commit 1811cae

Browse files
committed
Fixed sys.executable (issue #228)
1 parent 2ec8f24 commit 1811cae

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include("makefiles/shared.cmake")
1414
# ------------------------------------------------------------------
1515
Set(SOURCEPYTHON_LOADER_HEADERS
1616
loader/loader_main.h
17+
loader/definitions.h
1718
)
1819

1920
Set(SOURCEPYTHON_LOADER_SOURCES

src/core/sp_main.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
//---------------------------------------------------------------------------------
4343
// Definitions
4444
//---------------------------------------------------------------------------------
45-
#define MAX_PATH_LENGTH 1024
46-
#define MSG_PREFIX "[Source.Python] "
45+
#include "../loader/definitions.h"
4746

4847

4948
//---------------------------------------------------------------------------------

src/core/sp_python.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,19 @@ bool CPythonManager::Initialize( void )
102102
wchar_t wszPythonHome[MAX_PATH_LENGTH];
103103
V_strtowcs(szPythonHome, -1, wszPythonHome, MAX_PATH_LENGTH);
104104

105+
// Get the full path to the shared python library
106+
char szProgramName[MAX_PATH_LENGTH];
107+
V_snprintf(szProgramName, MAX_PATH_LENGTH, "%s/%s", GetSourcePythonDir(), PYLIB_NAME);
108+
V_FixSlashes(szProgramName);
109+
DevMsg(1, MSG_PREFIX "sys.executable set to %s\n", szProgramName);
110+
111+
// Convert to wide char for python.
112+
wchar_t wszProgramName[MAX_PATH_LENGTH];
113+
V_strtowcs(szProgramName, -1, wszProgramName, MAX_PATH_LENGTH);
114+
105115
// Set that as the python home directory.
106116
Py_SetPythonHome(wszPythonHome);
107-
Py_SetProgramName(wszPythonHome);
117+
Py_SetProgramName(wszProgramName);
108118
Py_SetPath(wszPythonHome);
109119

110120
// Initialize python and its namespaces.

src/loader/definitions.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* =============================================================================
3+
* Source Python
4+
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
5+
* =============================================================================
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, version 3.0, as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU General Public License along with
17+
* this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* As a special exception, the Source Python Team gives you permission
20+
* to link the code of this program (as well as its derivative works) to
21+
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
22+
* by the Valve Corporation. You must obey the GNU General Public License in
23+
* all respects for all other code used. Additionally, the Source.Python
24+
* Development Team grants this exception to all derivative works.
25+
*/
26+
#ifndef _LOADER_DEFINITIONS_H
27+
#define _LOADER_DEFINITIONS_H
28+
29+
//---------------------------------------------------------------------------------
30+
// Definitions
31+
//---------------------------------------------------------------------------------
32+
#define PYLIB_NAME_WIN32 "Python3/plat-win/python36.dll"
33+
#define PYLIB_NAME_LINUX "Python3/plat-linux/libpython3.6m.so.1.0"
34+
35+
#define CORE_NAME_WIN32 "bin/core.dll"
36+
#define CORE_NAME_LINUX "bin/core.so"
37+
38+
#if defined(_WIN32)
39+
# define CORE_NAME CORE_NAME_WIN32
40+
#else
41+
# define CORE_NAME CORE_NAME_LINUX
42+
#endif
43+
44+
#if defined(_WIN32)
45+
# define PYLIB_NAME PYLIB_NAME_WIN32
46+
# define VCRUNTIME_LIB "Python3/plat-win/vcruntime140.dll"
47+
#elif defined(LINUX)
48+
# define PYLIB_NAME PYLIB_NAME_LINUX
49+
#endif
50+
51+
#define MAX_PATH_LENGTH 1024
52+
#define MAX_ERROR_LENGTH 1024
53+
#define MSG_PREFIX "[Source.Python] "
54+
55+
#endif // _LOADER_DEFINITIONS_H

src/loader/loader_main.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,8 @@
3535
//---------------------------------------------------------------------------------
3636
// Definitions
3737
//---------------------------------------------------------------------------------
38-
#define PYLIB_NAME_WIN32 "Python3/plat-win/python36.dll"
39-
#define PYLIB_NAME_LINUX "Python3/plat-linux/libpython3.6m.so.1.0"
38+
#include "definitions.h"
4039

41-
#define CORE_NAME_WIN32 "bin/core.dll"
42-
#define CORE_NAME_LINUX "bin/core.so"
43-
44-
#if defined(_WIN32)
45-
# define CORE_NAME CORE_NAME_WIN32
46-
#else
47-
# define CORE_NAME CORE_NAME_LINUX
48-
#endif
49-
50-
#if defined(_WIN32)
51-
# define PYLIB_NAME PYLIB_NAME_WIN32
52-
# define VCRUNTIME_LIB "Python3/plat-win/vcruntime140.dll"
53-
#elif defined(LINUX)
54-
# define PYLIB_NAME PYLIB_NAME_LINUX
55-
#endif
56-
57-
#define MAX_PATH_LENGTH 1024
58-
#define MAX_ERROR_LENGTH 1024
59-
#define MSG_PREFIX "[Source.Python] "
6040

6141
//---------------------------------------------------------------------------------
6242
// Purpose: a sample 3rd party plugin class

0 commit comments

Comments
 (0)