Skip to content

Commit 9186f6c

Browse files
committed
start to fix python unittests
- corrected importlib issue (https://docs.python.org/3/library/importlib.html) - removed deprecated e.message (https://stackoverflow.com/questions/1272138/baseexception-message-deprecated-in-python-2-6) - use __str__ to get the unicode string, no encode needed with python3 - workaround how to check if GIL is holden - need to check init sequence, just first tests
1 parent 33d8671 commit 9186f6c

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

PythonScript/python_tests/RunTests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import unittest
44
import os
5+
import importlib
56

67
# unittest module expects argv to be set
78
sys.argv = ['']
@@ -13,10 +14,10 @@
1314
for test_name in os.listdir(os.path.join(path, 'tests')):
1415
(test_name, ext) = os.path.splitext(test_name)
1516
if ext == '.py':
16-
test_module = reload(__import__('npp_unit_tests.tests.' + test_name))
17+
test_module = importlib.reload(__import__('npp_unit_tests.tests.' + test_name))
1718
test_suite = getattr(test_module.tests, test_name)
1819
if test_suite:
19-
reload(test_suite)
20+
importlib.reload(test_suite)
2021
if hasattr(test_suite, 'suite'):
2122
test_suites.append(test_suite.suite)
2223

PythonScript/python_tests/tests/NotepadWrapperTestCase.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ def _invalid_parameter_passed(self, method, *args):
8787
try:
8888
method(*args)
8989
except Exception as e:
90-
if 'did not match C++ signature' in e.message:
90+
if 'did not match C++ signature' in str(e):
9191
raise ArgumentError
92-
elif 'invalid aka unknown bufferID provided' in e.message:
92+
elif 'invalid aka unknown bufferID provided' in str(e):
9393
raise ArgumentError
9494
else:
95-
raise Exception('args:{} - message:{}'.format(args, e.message))
95+
raise Exception('args:{} - message:{}'.format(args, str(e)))
9696

9797

9898
def __test_invalid_parameter_passed(self, notepad_method):

PythonScript/src/GILManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace NppPythonScript
3838
protected:
3939
static bool doIHaveTheGIL()
4040
{
41-
41+
//TODO check if PyThreadState_GET should be used or something else
4242
PyThreadState* thisThreadState = PyGILState_GetThisThreadState();
43-
return (thisThreadState && thisThreadState == PyThreadState_GET());
43+
return (thisThreadState && thisThreadState == _PyThreadState_UncheckedGet());
4444
}
4545

4646
};

PythonScript/src/PythonConsole.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ void PythonConsole::writeText(boost::python::object text)
193193
{
194194
if (PyUnicode_Check(text.ptr()))
195195
{
196-
boost::python::object utf8String(boost::python::handle<PyObject>(PyUnicode_AsUTF8String(text.ptr())));
197-
198-
std::string textToWrite((const char *)boost::python::extract<const char *>(utf8String), _len(utf8String));
199-
GILRelease release;
196+
197+
198+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
199+
GILRelease release;
200200
if (m_runStatementExecuted)
201201
{
202202
mp_consoleDlg->writeColoredText(textToWrite.size(), textToWrite.c_str());
@@ -229,9 +229,9 @@ void PythonConsole::writeError(boost::python::object text)
229229
{
230230
if (PyUnicode_Check(text.ptr()))
231231
{
232-
boost::python::object utf8String(boost::python::handle<PyObject>(PyUnicode_AsUTF8String(text.ptr())));
233-
234-
std::string textToWrite((const char *)boost::python::extract<const char *>(utf8String));
232+
233+
234+
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
235235
GILRelease release;
236236
mp_consoleDlg->writeError(textToWrite.size(), textToWrite.c_str());
237237
}
@@ -302,8 +302,8 @@ void PythonConsole::consume(std::shared_ptr<std::string> statement)
302302
{
303303
boost::python::object oldStdout = m_sys.attr("stdout");
304304
m_sys.attr("stdout") = boost::python::ptr(this);
305-
PyObject* unicodeCommand = PyUnicode_FromEncodedObject(boost::python::str(statement->c_str()).ptr(), "utf-8", NULL);
306-
boost::python::object result = m_pushFunc(boost::python::handle<PyObject>(unicodeCommand));
305+
306+
boost::python::object result = m_pushFunc(boost::python::str(statement->c_str()));
307307
//Py_DECREF(unicodeCommand);
308308
m_sys.attr("stdout") = oldStdout;
309309

PythonScript/src/PythonHandler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ void PythonHandler::initPython()
9292
// Don't import site - if Python 2.7 doesn't find it as part of Py_Initialize,
9393
// it does an exit(1) - AGH!
9494
Py_NoSiteFlag = 1;
95+
Py_IgnoreEnvironmentFlag = 1;
96+
Py_NoUserSiteDirectory = 1;
9597

9698
Py_Initialize();
9799
// Initialise threading and create & acquire Global Interpreter Lock

PythonScript/src/ScintillaWrapper.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ std::string ScintillaWrapper::getStringFromObject(boost::python::object o)
5555
std::string raw;
5656
if (PyUnicode_Check(o.ptr()))
5757
{
58-
boost::python::object utf8Text = o.attr("encode")("utf-8");
59-
raw = std::string(boost::python::extract<const char *>(utf8Text), _len(utf8Text));
58+
boost::python::object utf8Text = o.attr("__str__")();
59+
raw = std::string(boost::python::extract<const char *>(utf8Text));
6060
}
6161
else
6262
{
@@ -666,7 +666,9 @@ std::string ScintillaWrapper::extractEncodedString(boost::python::object str, in
666666
codePageName = getCurrentAnsiCodePageName();
667667
}
668668

669-
boost::python::object searchUtf8(str.attr("encode")(codePageName));
669+
//TODO how to get str.attr("encode")(codePageName) working again here
670+
//currently this is always unicode (utf16 or utf8??), the internal representation of python3
671+
boost::python::object searchUtf8(str.attr("__str__")());
670672
searchLength = boost::python::extract<int>(searchUtf8.attr("__len__")());
671673
resultStr.append(boost::python::extract<const char*>(searchUtf8), searchLength);
672674
}

0 commit comments

Comments
 (0)