Skip to content

Commit

Permalink
Merge pull request #223 from fasiondog/bugfix
Browse files Browse the repository at this point in the history
fixed TradeRecord 在 python 交互环境下打印输出错误
  • Loading branch information
fasiondog committed Apr 5, 2024
2 parents 433ebe4 + f5b9999 commit c698cb1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions hikyuu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def close():
close_ostream_to_python()


if in_interactive_session():
set_python_in_interactive(True)


# 如果是在 jupyter 环境中运行,重定向C++ stdout/stderr输出至python
if in_ipython_frontend():
set_python_in_jupyter(True)
Expand Down
13 changes: 11 additions & 2 deletions hikyuu_cpp/hikyuu/global/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ using json = nlohmann::json;
namespace hku {

std::atomic<int> g_latest_version{0};
std::atomic_bool g_runningInPython{false}; // 是否是在 python 中运行
std::atomic_bool g_pythonInJupyter{false}; // python 是否为交互模式
bool g_runningInPython{false}; // 是否是在 python 中运行
bool g_pythonInInteractive{false}; // python 是否运行在交互模式下
bool g_pythonInJupyter{false}; // python 是否运行在 Jupyter中

bool HKU_API runningInPython() {
return g_runningInPython;
Expand All @@ -36,6 +37,14 @@ void HKU_API setRunningInPython(bool inpython) {
g_runningInPython = inpython;
}

bool HKU_API pythonInInteractive() {
return g_pythonInInteractive;
}

void HKU_API setPythonInInteractive(bool interactive) {
g_pythonInInteractive = interactive;
}

bool HKU_API pythonInJupyter() {
return g_pythonInJupyter;
}
Expand Down
7 changes: 7 additions & 0 deletions hikyuu_cpp/hikyuu/global/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ bool HKU_API runningInPython();
/** 当前是否运行在 Jupyter 环境中 */
bool HKU_API pythonInJupyter();

/** python 是否运行在交互模式下 */
bool HKU_API pythonInInteractive();

/** 设置是否运行在 python 下*/
void HKU_API setRunningInPython(bool inpython);

/** 设置 python 是否运行在交互模式下 */
void HKU_API setPythonInInteractive(bool interactive);

/** 当前是否运行在 Jupyter 环境中 */
void HKU_API setPythonInJupyter(bool injupyter);

Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/trade_manage/TradeRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ string TradeRecord::toString() const {

#if HKU_OS_WINDOWS
return fmt::format("Trade({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})", datetime,
market_code, runningInPython() && pythonInJupyter() ? name : UTF8ToGB(name),
market_code, pythonInInteractive() ? name : UTF8ToGB(name),
getBusinessName(business), planPrice, realPrice, goalPrice, number,
cost.commission, cost.stamptax, cost.transferfee, cost.others,
getSystemPartName(from));
Expand Down
1 change: 1 addition & 0 deletions hikyuu_pywrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ PYBIND11_MODULE(core, m) {
export_io_redirect(m);

m.def("set_python_in_jupyter", setPythonInJupyter);
m.def("set_python_in_interactive", setPythonInInteractive);

m.def("close_spend_time", close_spend_time, "全局关闭 c++ 部分耗时打印");
m.def("open_spend_time", close_spend_time, "全局开启 c++ 部分耗时打印");
Expand Down

0 comments on commit c698cb1

Please sign in to comment.