"startswith first arg" error under Python 3 using

Issue #416 duplicate
Anonymous created an issue

Python 3.3.5, Whoosh 2.7.0, Haystack 2.3.1, Django 1.8.2

It seems to happen whenever suggest is called

  File "/home/travis/build/aristotle-mdr/aristotle-metadata-registry/aristotle_mdr/forms/search.py", line 311, in search
    if sqs.count() < 5:
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/query.py", line 479, in count
    return len(self)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/query.py", line 91, in __len__
    self._result_count = self.query.get_count()
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/__init__.py", line 626, in get_count
    self.run()
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/__init__.py", line 563, in run
    results = self.backend.search(final_query, **search_kwargs)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/__init__.py", line 35, in wrapper
    return func(obj, query_string, *args, **kwargs)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/whoosh_backend.py", line 459, in search
    results = self._process_results(raw_page, highlight=highlight, query_string=query_string, spelling_query=spelling_query, result_class=result_class)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/whoosh_backend.py", line 652, in _process_results
    spelling_suggestion = self.create_spelling_suggestion(query_string)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/haystack/backends/whoosh_backend.py", line 682, in create_spelling_suggestion
    suggestions = corrector.suggest(word, limit=1)
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/whoosh/spelling.py", line 66, in suggest
    for item in _suggestions(text, maxdist, prefix):
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/whoosh/spelling.py", line 111, in _suggestions
    for sug in reader.terms_within(sugfield, text, maxdist, prefix=prefix):
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/whoosh/reading.py", line 536, in terms_within
    for btext in self.expand_prefix(fieldname, text[:prefix]):
  File "/home/travis/virtualenv/python3.3.5/lib/python3.3/site-packages/whoosh/reading.py", line 241, in expand_prefix
    if fn != fieldname or not text.startswith(prefix):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Comments (5)

  1. Jacob Svensson

    The same error can be triggered from other code-paths, like this:

          ...
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/searching.py", line 1
            minscore=minscore)
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/highlight.py", line 9
            bterms = results.query_terms(expand=True, fieldname=fieldname)
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/searching.py", line 1
            fieldname=fieldname, expand=expand)
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/query/qcore.py", line
            for fieldname, text in terms:
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/query/terms.py", line
            for btext in self._btexts(ixreader):
          File "/redacted/virtual/lib/python3.4/site-packages/whoosh/reading.py", line 241
            if fn != fieldname or not text.startswith(prefix):
        TypeError: startswith first arg must be bytes or a tuple of bytes, not str
    

    Here using Whoosh 2.7.0, and Python 3.4.0.

  2. RogerHaase

    Using python 2.7.8 and whoosh 2.7.0 on MoinMoin 2 project, have a different message on line 241:

    Traceback (most recent call last):
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1836, in __call__
        return self.wsgi_app(environ, start_response)
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1820, in wsgi_app
        response = self.make_response(self.handle_exception(e))
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1403, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1817, in wsgi_app
        response = self.full_dispatch_request()
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
        rv = self.dispatch_request()
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\flask\app.py", line 1461, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "d:\bitbucket\m2-x7\MoinMoin\apps\frontend\views.py", line 398, in search
        word_suggestions = corrector.suggest(lastword, limit=3)
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\whoosh\spelling.py", line 66, in suggest
        for item in _suggestions(text, maxdist, prefix):
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\whoosh\spelling.py", line 111, in _suggestions
        for sug in reader.terms_within(sugfield, text, maxdist, prefix=prefix):
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\whoosh\reading.py", line 536, in terms_within
        for btext in self.expand_prefix(fieldname, text[:prefix]):
      File "D:\Bitbucket\m2-x7-venv-python\lib\site-packages\whoosh\reading.py", line 241, in expand_prefix
        if fn != fieldname or not text.startswith(prefix):
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 14: ordinal not in range(128)
    

    text = 'ajudanaformata\xc3\xa7\xc3\xa3o' prefix = u''

  3. Log in to comment