Skip to content

Commit d736334

Browse files
committed
- used 'rb' to open file as stated at docu https://docs.python.org/3/c-api/veryhigh.html of PyRun_SimpleFileEx:
Note: On Windows, fp should be opened as binary mode (e.g. fopen(filename, "rb"). Otherwise, Python may not handle script file with LF line ending correctly. - readded error handling for PyRun_SimpleFileEx - added unittestfiles with mac, unix lineending and for codepage windows-1252 - still open: handling of non unicode strings represented as py byte in python3, failing e.g. in editor.getText()
1 parent 99243a6 commit d736334

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: windows-1252 -*-
2+
import unittest
3+
from Npp import notepad, editor
4+
5+
class CP1252FileCase(unittest.TestCase):
6+
def setUp(self):
7+
notepad.new()
8+
9+
def tearDown(self):
10+
editor.setSavePoint()
11+
notepad.close()
12+
13+
def test_simple_text_output(self):
14+
editor.write(u'test123äöü');
15+
text = editor.getText()
16+
self.assertEqual(text, u'test123äöü');
17+
18+
suite = unittest.TestLoader().loadTestsFromTestCase(CP1252FileCase)

PythonScript/python_tests/tests/LineEndingMacTestCase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-import unittestfrom Npp import notepad, editorclass LineEndingMacTestCase(unittest.TestCase): def setUp(self): notepad.new() def tearDown(self): editor.setSavePoint() notepad.close() def test_simple_text_output(self): editor.write(u'test123äöü'); text = editor.getText() self.assertEqual(text, u'test123äöü');suite = unittest.TestLoader().loadTestsFromTestCase(LineEndingMacTestCase)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
import unittest
3+
from Npp import notepad, editor
4+
5+
class LineEndingUnixTestCase(unittest.TestCase):
6+
def setUp(self):
7+
notepad.new()
8+
9+
def tearDown(self):
10+
editor.setSavePoint()
11+
notepad.close()
12+
13+
def test_simple_text_output(self):
14+
editor.write(u'test123äöü');
15+
text = editor.getText()
16+
self.assertEqual(text, u'test123äöü');
17+
18+
suite = unittest.TestLoader().loadTestsFromTestCase(LineEndingUnixTestCase)

PythonScript/src/PythonHandler.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,21 @@ void PythonHandler::runScriptWorker(const std::shared_ptr<RunScriptArgs>& args)
305305
}
306306
else
307307
{
308-
FILE* pyFile = _Py_wfopen(args->m_filename.c_str(), _T("r"));
309-
310-
311-
312-
308+
FILE* pyFile = _Py_wfopen(args->m_filename.c_str(), _T("rb"));
313309
if (pyFile)
314310
{
315-
PyRun_SimpleFileEx(pyFile, WcharMbcsConverter::tchar2char(args->m_filename.c_str()).get(), 1);
311+
if (PyRun_SimpleFileEx(pyFile, WcharMbcsConverter::tchar2char(args->m_filename.c_str()).get(), 1) == -1)
312+
{
313+
if (ConfigFile::getInstance()->getSetting(_T("ADDEXTRALINETOOUTPUT")) == _T("1"))
314+
{
315+
mp_console->writeText(boost::python::str("\n"));
316+
}
317+
318+
if (ConfigFile::getInstance()->getSetting(_T("OPENCONSOLEONERROR")) == _T("1"))
319+
{
320+
mp_console->pythonShowDialog();
321+
}
322+
}
316323
}
317324

318325
}

0 commit comments

Comments
 (0)