Better handle multimatch index out of scope. Resolve #2207.

This commit is contained in:
Griatch 2020-11-13 20:39:44 +01:00
parent aac9eb17d9
commit 5f8911ec72
4 changed files with 15 additions and 9 deletions

View file

@ -209,10 +209,15 @@ def cmdparser(raw_string, cmdset, caller, match_index=None):
quality = [mat[4] for mat in matches] quality = [mat[4] for mat in matches]
matches = matches[-quality.count(quality[-1]) :] matches = matches[-quality.count(quality[-1]) :]
if len(matches) > 1 and match_index is not None and 0 < match_index <= len(matches): if len(matches) > 1 and match_index is not None:
# We couldn't separate match by quality, but we have an # We couldn't separate match by quality, but we have an
# index argument to tell us which match to use. # index argument to tell us which match to use.
matches = [matches[match_index - 1]] if 0 < match_index <= len(matches):
matches = [matches[match_index - 1]]
else:
# we tried to give an index outside of the range - this means
# a no-match
matches = []
# no matter what we have at this point, we have to return it. # no matter what we have at this point, we have to return it.
return matches return matches

View file

@ -466,7 +466,6 @@ class ObjectDBManager(TypedObjectManager):
# strips the number # strips the number
match_number, searchdata = match.group("number"), match.group("name") match_number, searchdata = match.group("number"), match.group("name")
match_number = int(match_number) - 1 match_number = int(match_number) - 1
match_number = match_number if match_number >= 0 else None
if match_number is not None or not exact: if match_number is not None or not exact:
# run search again, with the exactness set by call # run search again, with the exactness set by call
matches = _searcher(searchdata, candidates, typeclass, exact=exact) matches = _searcher(searchdata, candidates, typeclass, exact=exact)
@ -474,11 +473,13 @@ class ObjectDBManager(TypedObjectManager):
# deal with result # deal with result
if len(matches) > 1 and match_number is not None: if len(matches) > 1 and match_number is not None:
# multiple matches, but a number was given to separate them # multiple matches, but a number was given to separate them
try: if 0 <= match_number < len(matches):
# limit to one match
matches = [matches[match_number]] matches = [matches[match_number]]
except IndexError: else:
# match number not matching anything # a number was given outside of range. This means a no-match.
pass matches = []
# return a list (possibly empty) # return a list (possibly empty)
return matches return matches

View file

@ -394,7 +394,8 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
a global search. a global search.
- `me,self`: self-reference to this object - `me,self`: self-reference to this object
- `<num>-<string>` - can be used to differentiate - `<num>-<string>` - can be used to differentiate
between multiple same-named matches between multiple same-named matches. The exact form of this input
is given by `settings.SEARCH_MULTIMATCH_REGEX`.
global_search (bool): Search all objects globally. This overrules 'location' data. global_search (bool): Search all objects globally. This overrules 'location' data.
use_nicks (bool): Use nickname-replace (nicktype "object") on `searchdata`. use_nicks (bool): Use nickname-replace (nicktype "object") on `searchdata`.
typeclass (str or Typeclass, or list of either): Limit search only typeclass (str or Typeclass, or list of either): Limit search only

View file

@ -87,7 +87,6 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, _BASE_SESSION_CLASS):
super().dataReceived(data) super().dataReceived(data)
except ValueError as err: except ValueError as err:
from evennia.utils import logger from evennia.utils import logger
logger.log_err(f"Malformed telnet input: {err}") logger.log_err(f"Malformed telnet input: {err}")
def connectionMade(self): def connectionMade(self):