Skip to content

Commit

Permalink
initial try of build adaptations for python 3 support, see #36 on blo…
Browse files Browse the repository at this point in the history
…ckage, due to missing boost python3 nuget package
  • Loading branch information
chcg committed May 27, 2018
1 parent cec4d36 commit 5f9ca93
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 36 deletions.
4 changes: 2 additions & 2 deletions PythonScript/project/PythonSettings_appveyor.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<PropertyGroup Label="UserMacros">
<BoostBase></BoostBase>
<BoostPythonLibPath></BoostPythonLibPath>
<PythonBase>$(SolutionDir)/packages/python2x86.2.7.15/tools</PythonBase>
<PythonBaseX64>$(SolutionDir)/packages/python2.2.7.15/tools</PythonBaseX64>
<PythonBase>$(SolutionDir)/packages/pythonx86.3.6.5/tools</PythonBase>
<PythonBaseX64>$(SolutionDir)/packages/python.3.6.5/tools</PythonBaseX64>
<PythonLibPath>$(PythonBase)\libs</PythonLibPath>
<PythonLibPathX64>$(PythonBaseX64)\libs</PythonLibPathX64>
<HtmlHelpBase>C:\Program Files (x86)\HTML Help Workshop</HtmlHelpBase>
Expand Down
4 changes: 2 additions & 2 deletions PythonScript/project/packages_appveyor.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<package id="boost" version="1.66.0.0" targetFramework="native" />
<package id="boost_python-vc140" version="1.66.0.0" targetFramework="native" />
<package id="boost_regex-vc140" version="1.66.0.0" targetFramework="native" />
<package id="python2" version="2.7.15" targetFramework="native" />
<package id="python2x86" version="2.7.15" targetFramework="native" />
<package id="python" version="3.6.5" targetFramework="native" />
<package id="pythonx86" version="3.6.5" targetFramework="native" />
</packages>
4 changes: 2 additions & 2 deletions PythonScript/src/Match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ boost::python::str Match::py_group_name(boost::python::str pyGroupName)

boost::python::str Match::getGroup(boost::python::object groupIdentifier)
{
if (PyInt_Check(groupIdentifier.ptr()))
if (PyLong_Check(groupIdentifier.ptr()))
{
return py_group_number(boost::python::extract<int>(groupIdentifier));
}
else if (PyString_Check(groupIdentifier.ptr()))
else if (PyUnicode_Check(groupIdentifier.ptr()))
{
return py_group_name(boost::python::extract<boost::python::str>(groupIdentifier));
}
Expand Down
31 changes: 4 additions & 27 deletions PythonScript/src/PythonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,40 +292,17 @@ void PythonHandler::runScriptWorker(const std::shared_ptr<RunScriptArgs>& args)
}
else
{
std::shared_ptr<char> filenameUFT8 = WcharMbcsConverter::tchar2char(args->m_filename.c_str());
FILE* pyFile = _tfopen(args->m_filename.c_str(), _T("r"));

if (containsExtendedChars(filenameUFT8.get()))
{
// First obtain the size needed by passing NULL and 0.
const long initLength = GetShortPathName(args->m_filename.c_str(), NULL, 0);
if (initLength > 0)
{
// Dynamically allocate the correct size
// (terminating null char was included in length)
tstring buffer(initLength, 0);

// Now simply call again using same long path.

long length = GetShortPathName(args->m_filename.c_str(), const_cast<LPWSTR>(buffer.c_str()), initLength);
if (length > 0)
{
filenameUFT8 = WcharMbcsConverter::tchar2char(buffer.c_str());
}
}
}

// We assume PyFile_FromString won't modify the file name passed in param
// (that would be quite troubling) and that the missing 'const' is simply an oversight
// from the Python API developers.
// We also assume the second parameter, "r" won't be modified by the function call.
//lint -e{1776} Converting a string literal to char * is not const safe (arg. no. 2)
PyObject* pyFile = PyFile_FromString(filenameUFT8.get(), "r");

if (pyFile)
{
PyRun_SimpleFile(PyFile_AsFile(pyFile), filenameUFT8.get());
Py_DECREF(pyFile);
PyRun_SimpleFile(pyFile, WcharMbcsConverter::tchar2char(args->m_filename.c_str()).get());
}

fclose(pyFile);
}

if (NULL != args->m_completedEvent)
Expand Down
11 changes: 10 additions & 1 deletion PythonScript/src/ScintillaPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,18 @@ BOOST_PYTHON_MODULE(Npp)
export_match();
}

//see https://github.com/TNG/boost-python-examples/blob/master/10-Embedding/embedding.cpp
#if PY_MAJOR_VERSION >= 3
# define INIT_MODULE PyInit_Npp
extern "C" PyObject* INIT_MODULE();
#else
# define INIT_MODULE initNpp
extern "C" void INIT_MODULE();
#endif

void preinitScintillaModule()
{
PyImport_AppendInittab("Npp", &initNpp);
PyImport_AppendInittab("Npp", INIT_MODULE);
}

void importScintilla(boost::shared_ptr<ScintillaWrapper> editor, boost::shared_ptr<ScintillaWrapper> editor1, boost::shared_ptr<ScintillaWrapper> editor2)
Expand Down
4 changes: 2 additions & 2 deletions PythonScript/src/ScintillaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ void ScintillaWrapper::forEachLine(PyObject* function)

boost::python::object result = boost::python::call<boost::python::object>(function, GetLine(line), line, lineCount);

if (result.is_none() || !PyInt_Check(result.ptr()))
if (result.is_none() || !PyLong_Check(result.ptr()))
{
++line;
}
else
{
line += PyInt_AsLong(result.ptr());
line += PyLong_AsLong(result.ptr());
}

lineCount = GetLineCount();
Expand Down

0 comments on commit 5f9ca93

Please sign in to comment.