Commit 0ae113c9 authored by ussrhero's avatar ussrhero

fix crash with convert bytearry to string on python 3

parent b0fdd53a
......@@ -31,6 +31,9 @@ PyObject* __stdcall PyString_FromString(const char *v);
char* PyString_AsString(PyObject *string);
int PyString_Check(PyObject *o);
int __stdcall PyBytes_Check(PyObject *bytes);
char* PyBytes_AsString(PyObject *bytes);
PyObject* PyImport_Import(PyObject *name);
PyObject* __stdcall PyImport_ImportModule(const char *name);
PyObject* __stdcall PyImport_AddModule(const char *name);
......
......@@ -31,9 +31,19 @@ struct convert_from_python
return std::wstring(&buf[0], len);
}
if ( !IsPy3() && PyString_Check(m_obj) )
if (!IsPy3() )
{
return std::wstring(_bstr_t(PyString_AsString(m_obj)));
if (PyString_Check(m_obj))
{
return std::wstring(_bstr_t(PyString_AsString(m_obj)));
}
}
else
{
if (PyBytes_Check(m_obj))
{
return std::wstring(_bstr_t(PyBytes_AsString(m_obj)));
}
}
throw convert_python_exception("failed convert argument");
......@@ -51,9 +61,19 @@ struct convert_from_python
return std::string(_bstr_t(str.c_str()));
}
if ( !IsPy3() && PyString_Check(m_obj) )
if (!IsPy3())
{
if (PyString_Check(m_obj))
{
return std::string(PyString_AsString(m_obj));
}
}
else
{
return std::string(PyString_AsString(m_obj));
if (PyBytes_Check(m_obj))
{
return std::string(PyBytes_AsString(m_obj));
}
}
throw convert_python_exception("failed convert argument");
......
......@@ -65,6 +65,7 @@ public:
PyObject* PyExc_TypeError;
PyObject* PyUnicode_Type;
PyObject* PyString_Type;
PyObject* PyBytes_Type;
PyThreadState** PyThreadState_Current;
......@@ -105,6 +106,7 @@ public:
void( *PySys_SetArgv_Py3)(int argc, wchar_t **argv);
PyObject* ( *PyString_FromString)(const char *v);
char* ( *PyString_AsString)(PyObject *string);
char* ( *PyBytes_AsString)(PyObject *bytes);
void( *PyErr_Fetch)(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback);
void( *PyErr_NormalizeException)(PyObject**exc, PyObject**val, PyObject**tb);
void( *PyErr_SetString)(PyObject *type, const char *message);
......@@ -416,6 +418,8 @@ PyModule::PyModule(int majorVesion, int minorVersion)
*reinterpret_cast<FARPROC*>(&PyProperty_Type) = GetProcAddress(m_handlePython, "PyProperty_Type");
*reinterpret_cast<FARPROC*>(&PyUnicode_Type) = GetProcAddress(m_handlePython, "PyUnicode_Type");
*reinterpret_cast<FARPROC*>(&PyString_Type) = GetProcAddress(m_handlePython, "PyString_Type");
*reinterpret_cast<FARPROC*>(&PyBytes_Type) = isPy3 ? GetProcAddress(m_handlePython, "PyBytes_Type") : 0;
*reinterpret_cast<FARPROC*>(&Py_None) = GetProcAddress(m_handlePython, "_Py_NoneStruct");
PyExc_SystemExit = *reinterpret_cast<PyObject**>(GetProcAddress(m_handlePython, "PyExc_SystemExit"));
PyExc_TypeError= *reinterpret_cast<PyObject**>(GetProcAddress(m_handlePython, "PyExc_TypeError"));
......@@ -456,6 +460,7 @@ PyModule::PyModule(int majorVesion, int minorVersion)
*reinterpret_cast<FARPROC*>(&PySys_SetArgv_Py3) = isPy3 ? GetProcAddress(m_handlePython, "PySys_SetArgv") : 0;
*reinterpret_cast<FARPROC*>(&PyString_FromString) = GetProcAddress(m_handlePython, "PyString_FromString");
*reinterpret_cast<FARPROC*>(&PyString_AsString) = GetProcAddress(m_handlePython, "PyString_AsString");
*reinterpret_cast<FARPROC*>(&PyBytes_AsString) = isPy3 ? GetProcAddress(m_handlePython, "PyBytes_AsString") : 0;
*reinterpret_cast<FARPROC*>(&PyErr_Fetch) = GetProcAddress(m_handlePython, "PyErr_Fetch");
*reinterpret_cast<FARPROC*>(&PyErr_NormalizeException) = GetProcAddress(m_handlePython, "PyErr_NormalizeException");
*reinterpret_cast<FARPROC*>(&PyErr_SetString) = GetProcAddress(m_handlePython, "PyErr_SetString");
......@@ -754,6 +759,11 @@ char* PyString_AsString(PyObject *string)
return PythonSingleton::get()->currentInterpreter()->m_module->PyString_AsString(string);
}
char* PyBytes_AsString(PyObject *bytes)
{
return PythonSingleton::get()->currentInterpreter()->m_module->PyBytes_AsString(bytes);
}
PyObject* PyUnicode_FromWideChar(const wchar_t *w, size_t size)
{
return PythonSingleton::get()->currentInterpreter()->m_module->PyUnicode_FromWideChar(w, size);
......@@ -922,3 +932,9 @@ int PyUnicode_Check(PyObject *o)
PythonSingleton::get()->currentInterpreter()->m_module->PyUnicode_Type );
}
int PyBytes_Check(PyObject *o)
{
return PythonSingleton::get()->currentInterpreter()->m_module->PyObject_IsInstance(o,
PythonSingleton::get()->currentInterpreter()->m_module->PyBytes_Type);
}
......@@ -164,7 +164,7 @@
<OptimizeReferences>true</OptimizeReferences>
<ModuleDefinitionFile>export.def</ModuleDefinitionFile>
<OutputFile>$(OutDir)pykd$(TargetExt)</OutputFile>
<AdditionalDependencies>comsuppw.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>dbgeng.lib;comsuppw.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
......@@ -221,5 +221,5 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\packages\boost.1.67.0.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\boost.1.67.0.0\build\boost.targets'))" />
</Target>
</Target>
</Project>
\ No newline at end of file
......@@ -3,7 +3,7 @@
#define PYKDEXT_VERSION_MAJOR 2
#define PYKDEXT_VERSION_MINOR 0
#define PYKDEXT_VERSION_SUBVERSION 0
#define PYKDEXT_VERSION_BUILDNO 20
#define PYKDEXT_VERSION_BUILDNO 21
#define __VER_STR2__(x) #x
#define __VER_STR1__(x) __VER_STR2__(x)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment