Skip to content

Python 3.13, site-packages and SDK 2013 update #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Apr 18, 2025
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
13492ea
First part of Win update
Ayuto Apr 1, 2025
284fe5b
Update Python
Ayuto Apr 1, 2025
e32e72b
Fixed dict usage before Python initialization (thanks to spitice)
Ayuto Apr 15, 2025
a1bf96d
Fix Python initialization
Ayuto Apr 15, 2025
e84647e
Update site packages
Ayuto Apr 16, 2025
8f691e4
Fixes due to updated path library
Ayuto Apr 16, 2025
f3f1647
Fix cmake deprecation warnings
Ayuto Apr 16, 2025
061c063
OB SDK fixes
Ayuto Apr 16, 2025
6ac5afe
Fix deprecated datetime function.
Ayuto Apr 16, 2025
81c5b67
Remove tf2 patches
Ayuto Apr 16, 2025
a45cd6e
Remove hl2dm patches
Ayuto Apr 16, 2025
30b818b
Remove patched tier1 for l4d2
Ayuto Apr 16, 2025
977f87c
Fix SDK link libs
Ayuto Apr 16, 2025
7e9a70d
Fix gmod
Ayuto Apr 16, 2025
91691e6
Remove bms patch and fix compilation
Ayuto Apr 16, 2025
28750cd
Remove blade patch
Ayuto Apr 16, 2025
685d27c
Update documentation
Ayuto Apr 16, 2025
2d7e65c
Updated docs for building
Ayuto Apr 16, 2025
732b121
Add static AsmJit lib for Linux
Ayuto Apr 17, 2025
5b9d787
Add static DynamicHooks lib for Linux
Ayuto Apr 17, 2025
740e098
Python update linux
Ayuto Apr 17, 2025
835bc69
32bit Python...
Ayuto Apr 17, 2025
a2e6639
Added Boost libs
Ayuto Apr 17, 2025
cee5da7
Fix link libraries
Ayuto Apr 17, 2025
76171e7
Final fixes
Ayuto Apr 17, 2025
5499de4
Fix path to sqlite lib
Ayuto Apr 17, 2025
081c601
Add sysconfigdata
Ayuto Apr 17, 2025
5f50c5b
Merge branch 'master' into py313
Ayuto Apr 18, 2025
c1836ae
Fix some Sphinx warnings
Ayuto Apr 18, 2025
2b6018a
Add requirements.txt
Ayuto Apr 18, 2025
4b63c1a
Accidentally installed the wrong arch...
Ayuto Apr 18, 2025
3cfe784
Add Linux site-packages
Ayuto Apr 18, 2025
092a33c
Fix documentation
Ayuto Apr 18, 2025
0eea28b
Use c++17
Ayuto Apr 18, 2025
3ba852d
Patches for bms and gmod (use V_swap instead of swap)
Ayuto Apr 18, 2025
0ee352f
PRs have been merged already
Ayuto Apr 18, 2025
720c2df
Use devenv to build Win from cmd
Ayuto Apr 18, 2025
2cba278
Also clear site-packages when updating
Ayuto Apr 18, 2025
a1b20f8
Updated requirements doc
Ayuto Apr 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix Python initialization
  • Loading branch information
Ayuto committed Apr 15, 2025
commit a1bf96d7597b16f2319bb974318899aaf91911e3
124 changes: 68 additions & 56 deletions src/core/sp_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,26 @@ const char *GetSourcePythonDir()
//---------------------------------------------------------------------------------
// Adds a path to sys.path (relative to GetSourcePythonDir()).
//---------------------------------------------------------------------------------
void AddToSysPath( const char* path )
bool AddToSysPath( PyConfig& config, const char* path )
{
PyStatus status;
char szFolderPath[MAX_PATH_LENGTH];
wchar_t wszFolderPath[MAX_PATH_LENGTH];

V_snprintf(szFolderPath, MAX_PATH_LENGTH, "%s%s", GetSourcePythonDir(), path);
V_FixSlashes(szFolderPath);
V_strtowcs(szFolderPath, -1, wszFolderPath, MAX_PATH_LENGTH);

DevMsg(1, MSG_PREFIX "Adding %s to path\n", szFolderPath);
std::string szCommandString = "sys.path.append(r\"" + std::string(szFolderPath) + "\")";
PyRun_SimpleString(szCommandString.c_str());
DevMsg(1, MSG_PREFIX "Adding %s to path...\n", szFolderPath);
config.module_search_paths_set = 1;
status = PyWideStringList_Append(&config.module_search_paths, wszFolderPath);
if (PyStatus_Exception(status)) {
Msg(MSG_PREFIX "Failed to add %s to sys.path.\n", szFolderPath);
PyConfig_Clear(&config);
return false;
}

return true;
}


Expand All @@ -93,100 +104,101 @@ void AddToSysPath( const char* path )
//---------------------------------------------------------------------------------
bool CPythonManager::Initialize( void )
{
// Construct a path to the python engine directory.
char szPythonHome[MAX_PATH_LENGTH];
wchar_t wszPythonHome[MAX_PATH_LENGTH];
char szProgramName[MAX_PATH_LENGTH];
wchar_t wszProgramName[MAX_PATH_LENGTH];
PyStatus status;
PyConfig config;

V_snprintf(szPythonHome, MAX_PATH_LENGTH, "%s/Python3", GetSourcePythonDir());
V_FixSlashes(szPythonHome);
DevMsg(1, MSG_PREFIX "Python home path set to %s\n", szPythonHome);

// Convert to wide char for python.
wchar_t wszPythonHome[MAX_PATH_LENGTH];
V_strtowcs(szPythonHome, -1, wszPythonHome, MAX_PATH_LENGTH);
DevMsg(1, MSG_PREFIX "Python home path set to %s.\n", szPythonHome);

// Get the full path to the shared python library
char szProgramName[MAX_PATH_LENGTH];
V_snprintf(szProgramName, MAX_PATH_LENGTH, "%s/%s", GetSourcePythonDir(), PYLIB_NAME);
V_FixSlashes(szProgramName);
DevMsg(1, MSG_PREFIX "sys.executable set to %s\n", szProgramName);

// Convert to wide char for python.
wchar_t wszProgramName[MAX_PATH_LENGTH];
V_strtowcs(szProgramName, -1, wszProgramName, MAX_PATH_LENGTH);
DevMsg(1, MSG_PREFIX "sys.executable set to %s\n", szProgramName);

PyStatus status;

PyConfig config;
PyConfig_InitPythonConfig(&config);
PyConfig_InitIsolatedConfig(&config);

status = PyConfig_SetString(&config, &config.program_name, wszProgramName);
if (PyStatus_Exception(status)) {
Msg(MSG_PREFIX "Failed to set Python program name.\n");
PyConfig_Clear(&config);
Py_ExitStatusException(status);
return false;
}

status = PyConfig_SetString(&config, &config.home, wszPythonHome);
if (PyStatus_Exception(status)) {
Msg(MSG_PREFIX "Failed to set Python home.\n");
PyConfig_Clear(&config);
Py_ExitStatusException(status);
return false;
}

// Set that as the python home directory.
//Py_SetPath(wszPythonHome);
if (!AddToSysPath(config, "/Python3"))
return false;

status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
if (!AddToSysPath(config, "/packages/source-python"))
return false;

// Print some information
DevMsg(1, MSG_PREFIX "Python version %s initialized!\n", Py_GetVersion());

// Set sys.argv and update sys.path
// Add operating system specific paths.
#if defined(WIN32)
if (!AddToSysPath(config, "/Python3/plat-win"))
return false;
#else
if (!AddToSysPath(config, "/Python3/plat-linux"))
return false;

if (!AddToSysPath(config, "/Python3/lib-dynload"))
return false;
#endif

if (!AddToSysPath(config, "/packages/site-packages"))
return false;

if (!AddToSysPath(config, "/packages/custom"))
return false;

if (!AddToSysPath(config, "/plugins"))
return false;

// Set sys.argv
DevMsg(1, MSG_PREFIX "Setting sys.argv...\n");
ICommandLine* pCommandLine = CommandLine();

int iParamCount = pCommandLine->ParmCount();
wchar_t** argv = new wchar_t*[iParamCount];
for (int i=0; i < iParamCount; i++)
wchar_t** argv = new wchar_t* [iParamCount];
for (int i = 0; i < iParamCount; i++)
{
const char* szParam = pCommandLine->GetParm(i);
int iParamLength = strlen(szParam);

wchar_t* wszParam = new wchar_t[iParamLength+1];
wchar_t* wszParam = new wchar_t[iParamLength + 1];
// Not sure what's wrong with V_strtowcs, but it seems like it
// doesn't convert the string correctly on Linux
mbstowcs(wszParam, szParam, iParamLength+1);
mbstowcs(wszParam, szParam, iParamLength + 1);

argv[i] = wszParam;
}
PySys_SetArgv(iParamCount, argv);

// Make sure sys is imported.
PyRun_SimpleString("import sys");

// Add the Python API path.
AddToSysPath("/packages/source-python");

// Add operating system specific paths.
#if defined(WIN32)
AddToSysPath("/Python3/plat-win");
#else
AddToSysPath("/Python3/plat-linux");

// We've got a bunch of linux shared objects here we need to load.
AddToSysPath("/Python3/lib-dynload");
#endif

// Site packages for any extra packages...
AddToSysPath("/packages/site-packages");
status = PyConfig_SetArgv(&config, iParamCount, argv);
if (PyStatus_Exception(status)) {
Msg(MSG_PREFIX "Failed to set sys.argv.\n");
return false;
}

// Add the custom packages path.
AddToSysPath("/packages/custom");
status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
if (PyStatus_Exception(status)) {
Msg(MSG_PREFIX "Failed to initialize Python.\n");
return false;
}

// And of course, the plugins directory for script imports.
AddToSysPath("/plugins");
// Print some information
DevMsg(1, MSG_PREFIX "Python version %s initialized!\n", Py_GetVersion());

// Enable circular references traversal
EnableDictTraversal();
Expand Down Expand Up @@ -219,7 +231,7 @@ bool CPythonManager::Initialize( void )
// Only reconnect the streams if the server was launched with a console (issue #392).
if (pCommandLine->FindParm("-console")) {
object io_open = python::import("io").attr("open");

object stdin_ = sys.attr("stdin");
if (stdin_.is_none())
{
Expand Down