Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hcaihao committed Feb 2, 2021
1 parent 9684452 commit 021a5db
Show file tree
Hide file tree
Showing 35 changed files with 2,461 additions and 0 deletions.
40 changes: 40 additions & 0 deletions SmartPython.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SmartPython", "SmartPython\SmartPython.vcxproj", "{B12702AD-ABFB-343A-A199-8E24837244A3}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SmartPythonExt", "SmartPythonExt\SmartPythonExt.vcxproj", "{596BB097-852A-48EC-9E24-7C71880C08E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x64.ActiveCfg = Debug|Win32
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x86.ActiveCfg = Debug|Win32
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x86.Build.0 = Debug|Win32
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x64.ActiveCfg = Release|Win32
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x86.ActiveCfg = Release|Win32
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x86.Build.0 = Release|Win32
{596BB097-852A-48EC-9E24-7C71880C08E2}.Debug|x64.ActiveCfg = Debug|x64
{596BB097-852A-48EC-9E24-7C71880C08E2}.Debug|x64.Build.0 = Debug|x64
{596BB097-852A-48EC-9E24-7C71880C08E2}.Debug|x86.ActiveCfg = Debug|Win32
{596BB097-852A-48EC-9E24-7C71880C08E2}.Debug|x86.Build.0 = Debug|Win32
{596BB097-852A-48EC-9E24-7C71880C08E2}.Release|x64.ActiveCfg = Release|x64
{596BB097-852A-48EC-9E24-7C71880C08E2}.Release|x64.Build.0 = Release|x64
{596BB097-852A-48EC-9E24-7C71880C08E2}.Release|x86.ActiveCfg = Release|Win32
{596BB097-852A-48EC-9E24-7C71880C08E2}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3795A254-2403-4B58-83E9-B89DFEF7103E}
Qt5Version = 5.11.3mt
EndGlobalSection
EndGlobal
144 changes: 144 additions & 0 deletions SmartPython/PyLogHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//MIT License
//
//Copyright(c) 2017 Matthias Möller
//https://github.com/TinyTinni/PyLogHook
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

#pragma once

#include <utility>
#include <string>

#ifdef TYTI_PYLOGHOOK_USE_BOOST
#include <boost/python.hpp>
#else
#include <pybind11/pybind11.h>
#endif


namespace tyti {
namespace pylog {
namespace detail {

#ifdef TYTI_PYLOGHOOK_USE_BOOST
template <typename T>
inline boost::python::object LogHookMakeObject(T t)
{
return boost::python::object(boost::python::make_function(
std::forward<T>(t),
boost::python::default_call_policies(),
boost::mpl::vector<void, const char*>() // signature
));
}
#else
template<typename T>
inline pybind11::object LogHookMakeObject(T t)
{
return pybind11::cpp_function(std::forward<T>(t));
}

#endif
template<typename T>
void redirect_syspipe(T t, const char* pipename)
{
assert(Py_IsInitialized());

PyObject* out = PySys_GetObject(pipename);

// in case python couldnt create stdout/stderr
// this happens in some gui application
// just register a new nameless type for this

if (out == NULL || out == Py_None)
{
std::string register_read_write = std::string("import sys\n\
sys.") + pipename + std::string(" = type(\"\",(object,),{\"write\":lambda self, txt: None, \"flush\":lambda self: None})()\n");

PyRun_SimpleString(register_read_write.c_str());
out = PySys_GetObject(pipename);
}
// overwrite write function
PyObject_SetAttrString(out, "write",
detail::LogHookMakeObject(std::forward<T>(t)).ptr());

}


template<typename T>
void redirect_syspipe2(T t, const char* pipename)
{
assert(Py_IsInitialized());

PyObject* in = PySys_GetObject(pipename);

// in case python couldnt create stdout/stderr
// this happens in some gui application
// just register a new nameless type for this

if (in == NULL || in == Py_None)
{
std::string register_read_write = std::string("import sys\n\
sys.") + pipename + std::string(" = type(\"\",(object,),{\"readline\":lambda self, txt: None, \"flush\":lambda self: None})()\n");

PyRun_SimpleString(register_read_write.c_str());
in = PySys_GetObject(pipename);
}
// overwrite write function
PyObject_SetAttrString(in, "readline",
detail::LogHookMakeObject(std::forward<T>(t)).ptr());

}
} // namespace detail

/** \brief redirects sys.stdout
Whenever sys.stdout.write is called, call the given function instead.
Given function has to be the signature "void (const char*)"
Preconditions:
Python has to be initialized.
Calling thread has to hold the GIL. (in case of multi threading)
Exceptions:
Maybe a PyErr occurs. This must be handled by the user.
@param t callbacl function of type "void (const char*)"
*/
template<typename T>
inline void redirect_stdout(T t)
{
detail::redirect_syspipe(std::forward<T>(t), "stdout");
}

template<typename T>
inline void redirect_stderr(T t)
{
detail::redirect_syspipe(std::forward<T>(t), "stderr");
}


template<typename T>
inline void redirect_stdin(T t)
{
detail::redirect_syspipe2(std::forward<T>(t), "stdin");
}
}
}
Binary file added SmartPython/SmartPython.ico
Binary file not shown.
38 changes: 38 additions & 0 deletions SmartPython/SmartPython.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#if defined(UNDER_CE)
#include <winbase.h>
#else
#include <winver.h>
#endif

IDI_ICON1 ICON DISCARDABLE "SmartPython.ico"

//http://msdn.microsoft.com/en-us/library/aa381058%28v=VS.85%29.aspx
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "FileVersion", "1.0.0.0"
VALUE "ProductVersion", "1,0,0,0"
VALUE "ProductName", "SmartPython"
VALUE "LegalCopyright", "[email protected]"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
Loading

0 comments on commit 021a5db

Please sign in to comment.