Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of invalid values in the Search, Search, Replace and Replace methods #370

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import unittest
from Npp import notepad, editor

class SearchReplaceInvalidArgumentTestCase(unittest.TestCase):
def setUp(self):
notepad.new()

def tearDown(self):
notepad.close()

def test_invalid_search_argument(self):
for invalid_arg in ["", None]:
for method in [editor.research, editor.search, editor.replace, editor.rereplace]:
self.assertRaises(TypeError, method, invalid_arg, None)

def test_invalid_replace_argument(self):
for method in [editor.replace, editor.rereplace]:
self.assertRaises(TypeError, method, "abc", None)


suite = unittest.TestLoader().loadTestsFromTestCase(SearchReplaceInvalidArgumentTestCase)
14 changes: 14 additions & 0 deletions PythonScript/src/ScintillaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,15 @@ void ScintillaWrapper::replaceImpl(boost::python::object searchStr, boost::pytho
intptr_t currentDocumentCodePage = this->GetCodePage();

std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage);

if (searchStr.is_none() || searchChars.empty()) {
throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string");
}

if (replaceStr.is_none()) {
throw NppPythonScript::ArgumentException("repalce parameter must not be none");
Ekopalypse marked this conversation as resolved.
Show resolved Hide resolved
Ekopalypse marked this conversation as resolved.
Show resolved Hide resolved
}

std::string replaceChars;
bool isPythonReplaceFunction = true;

Expand Down Expand Up @@ -953,6 +962,11 @@ void ScintillaWrapper::searchImpl(boost::python::object searchStr,

std::string searchChars = extractEncodedString(searchStr, currentDocumentCodePage);

if (searchStr.is_none() || searchChars.empty())
{
throw NppPythonScript::ArgumentException("search parameter must not be none or an empty string");
}

if (!PyCallable_Check(matchFunction.ptr()))
{
throw NppPythonScript::ArgumentException("match parameter must be callable, i.e. either a function or a lambda expression");
Expand Down
5 changes: 5 additions & 0 deletions docs/source/scintilla.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5197,6 +5197,9 @@ Helper Methods
See :meth:`editor.rereplace`, as this method is identical, with the exception that the search string is treated literally,
and not as a regular expression.

The search argument triggers an exception if either an empty string or None is specified.
The replace argument triggers an exception if None is specified.

If you use a function as the replace argument, the function will still receive a ``re.MatchObject`` like object as the parameter,
``group(0)`` will therefore always contain the string searched for (possibly in a different case if ``re.IGNORECASE`` was passed in the flags)

Expand Down Expand Up @@ -5233,6 +5236,8 @@ Helper Methods
The replace function provided to editor.rereplace should not invoke additional calls to editor.rereplace or other replacement functions
that modify the same text being processed. Doing so may result in unpredictable behavior.

The search argument triggers an exception if either an empty string or None is specified.
The replace argument triggers an exception if None is specified.

``flags`` are from the re module (e.g. ``re.IGNORECASE``), so ``import re`` if you use the flags.

Expand Down