Skip to content

Commit 38fb84c

Browse files
committed
feat: python312
移除在python3.12弃用的一些函数(PyUnicode_AsUnicode) 回退了部分C++20才支持的参数
1 parent b4d2e8b commit 38fb84c

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

Dice/DiceAttrVar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ double AttrVar::to_num()const {
437437
case Type::GBString:
438438
case Type::U8String:
439439
if (double num = 0;
440-
std::from_chars(text.c_str(), text.c_str() + text.length(), num, std::chars_format::general).ec == std::errc{}) {
440+
std::from_chars(text.c_str(), text.c_str() + text.length(), num).ec == std::errc{}) {
441441
return num;
442442
}
443443
default:

Dice/DicePython.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* Dice! QQ Dice Robot for TRPG
1010
* Copyright (C) 2018-2021 w4123溯洄
11-
* Copyright (C) 2019-2024 String.Empty
11+
* Copyright (C) 2019-2025 String.Empty
1212
*
1313
* This program is free software: you can redistribute it and/or modify it under the terms
1414
* of the GNU Affero General Public License as published by the Free Software Foundation,
@@ -58,7 +58,8 @@ static string py_to_string(PyObject* o) {
5858
static string py_to_native_string(PyObject* o) {
5959
auto t{ Py_TYPE(o) };
6060
#ifdef _WIN32
61-
return t == &PyUnicode_Type ? UtoGBK(PyUnicode_AsUnicode(o)) : empty;
61+
Py_ssize_t l = 0;
62+
return t == &PyUnicode_Type ? UtoGBK(PyUnicode_AsWideString(o, l)) : empty;
6263
#else
6364
return t == &PyUnicode_Type ? PyUnicode_AsUTF8(o) : empty;
6465
#endif
@@ -1158,43 +1159,55 @@ PyMODINIT_FUNC PyInit_DiceMaid(){
11581159
return mod;
11591160
}
11601161
PyGlobal::PyGlobal() {
1161-
PyStatus status;
1162+
if (Py_IsInitialized())return;
1163+
PyStatus status;
1164+
PyPreConfig preconfig;
1165+
PyPreConfig_InitIsolatedConfig(&preconfig);
1166+
preconfig.utf8_mode = 1;
1167+
//preconfig.legacy_windows_fs_encoding = 1;
1168+
if (PyStatus_Exception(status = Py_PreInitialize(&preconfig))) {
1169+
console.log("python预初始化失败:" + string(status.err_msg), 0b1);
1170+
return;
1171+
}
11621172
PyConfig config;
1163-
PyConfig_InitPythonConfig(&config);
1173+
PyConfig_InitIsolatedConfig(&config);
1174+
config._init_main = 0;
11641175
if (std::filesystem::path dirPy{ dirExe / "bin" };
11651176
std::filesystem::exists(dirPy / "python3.dll")
11661177
|| std::filesystem::exists(dirPy / "python3.so")
11671178
|| std::filesystem::exists(dirPy = dirExe / "python")
1168-
|| std::filesystem::exists(dirPy = dirExe / "python311")
1169-
|| std::filesystem::exists(dirPy = dirExe / "py311")
1179+
|| std::filesystem::exists(dirPy = dirExe / "python312")
1180+
|| std::filesystem::exists(dirPy = dirExe / "py312")
11701181
|| std::filesystem::exists((dirPy = dirExe) / "python3.dll")) {
11711182
//Py_SetPythonHome(dirPy.wstring().c_str());
11721183
PyConfig_SetString(&config, &config.home, dirPy.wstring().c_str());
11731184
//Py_SetProgramName(L"DiceMaid");
11741185
PyConfig_SetString(&config, &config.program_name, L"DiceMaid");
1175-
//Py_SetPath((dirPy / "python311.zip").wstring().c_str());
1176-
PyWideStringList_Append(&config.module_search_paths, (dirPy / "python311.zip").wstring().c_str());
1186+
PyWideStringList_Append(&config.module_search_paths, (dirPy / "python312.zip").wstring().c_str());
11771187
}
1178-
status = PyConfig_Read(&config);
11791188
try {
11801189
static auto import_dice = PyImport_AppendInittab(DiceModuleName, PyInit_DiceMaid);
11811190
if (import_dice) {
11821191
console.log("预载dicemaid模块失败!", 0b1000);
11831192
}
1184-
PyWideStringList_Append(&config.module_search_paths, ((DiceDir / "plugin").wstring() + L"/").c_str());
1185-
PyWideStringList_Append(&config.module_search_paths, ((dirExe / "Diceki" / "py").wstring() + L"/").c_str());
1193+
PyWideStringList_Append(&config.module_search_paths, ((DiceDir / "plugin").wstring()).c_str());
1194+
PyWideStringList_Append(&config.module_search_paths, ((dirExe / "Diceki" / "py").wstring()).c_str());
11861195
config.module_search_paths_set = 1;
1187-
if (!Py_IsInitialized()){//Py_Initialize();
1188-
status = Py_InitializeFromConfig(&config);
1196+
if (!PyStatus_Exception(status = PyConfig_Read(&config))) {
1197+
if (PyStatus_Exception(status = Py_InitializeFromConfig(&config)))
1198+
console.log("python初始化失败:" + string(status.err_msg), 0b1);
1199+
else console.log("Python.Initialized", 0);
11891200
PyRun_SimpleString("import sys");
1190-
PyRun_SimpleString("from dicemaid import *");
1201+
//PyRun_SimpleString("from dicemaid import *");
1202+
status = _Py_InitializeMain();
1203+
}
1204+
else {
1205+
console.log("PyConfig_Read error:" + string(status.err_msg), 0b1);
11911206
}
11921207
}
11931208
catch (std::exception& e) {
11941209
console.log("python初始化失败:" + string(e.what()), 0b1);
11951210
}
1196-
if (PyStatus_Exception(status))console.log("python初始化失败:" + string(status.err_msg), 0b1);
1197-
else console.log("Python.Initialized", 0);
11981211
PyConfig_Clear(&config);
11991212
}
12001213
dict<std::pair<PyObject*, std::filesystem::file_time_type>> Py_FileScripts;

0 commit comments

Comments
 (0)