﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37469	Lexemes with only escaped characters cause a crash	François Freitag		"Searching for `Lexeme('&')` passes the provided value through `psql_escape`, which returns `None`. When UTF8Dumper calls `bytes(None, ""utf-8"")`:
{{{
ERROR: test_quote_lexeme (postgres_tests.test_search.TestLexemes.test_quote_lexeme) (lexeme='&')
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/pdi/dev/django/tests/postgres_tests/test_search.py"", line 886, in test_quote_lexeme
    self.assertEqual(quote_lexeme(lexeme), quoted)
                     ~~~~~~~~~~~~^^^^^^^^
  File ""/home/pdi/dev/django/django/contrib/postgres/search.py"", line 26, in quote_lexeme
    return UTF8Dumper(str).quote(psql_escape(value)).decode()
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File ""/home/pdi/dev/django/.venv/lib/python3.14/site-packages/psycopg/adapt.py"", line 59, in quote
    if (value := self.dump(obj)) is None:
                 ~~~~~~~~~^^^^^
  File ""/home/pdi/dev/django/django/contrib/postgres/search.py"", line 23, in dump
    return bytes(obj, ""utf-8"")
TypeError: encoding without a string argument
}}}

See the attached patch to reproduce in the django test suite.

Lexeme is documented as:
> Lexeme objects allow search operators to be safely used with strings from an untrusted source. The content of each lexeme is escaped so that any operators that may exist in the string itself will not be interpreted."	Bug	new	contrib.postgres	6.0	Normal		full-text search, psycopg3	François Freitag Calvin Vu	Accepted	0	0	0	0	0	0
