import codecs
from typing import Any, Dict, Optional, TYPE_CHECKING
+from .pq._enums import ConnStatus
from .errors import NotSupportedError
from ._compat import cache
from .pq.abc import PGconn
from .connection import BaseConnection
+OK = ConnStatus.OK
+
+
_py_codecs = {
"BIG5": "big5",
"EUC_CN": "gb2312",
Default to utf8 if the connection has no encoding info.
"""
- if not conn:
+ if not conn or conn.closed:
return "utf-8"
+
pgenc = conn.pgconn.parameter_status(b"client_encoding") or b"UTF8"
return pg2pyenc(pgenc)
Default to utf8 if the connection has no encoding info.
"""
+ if pgconn.status != OK:
+ return "utf-8"
+
pgenc = pgconn.parameter_status(b"client_encoding") or b"UTF8"
return pg2pyenc(pgenc)
__slots__ = """
_conn format _adapters arraysize _closed _results pgresult _pos
_iresult _rowcount _query _tx _last_query _row_factory _make_row
- _pgconn _encoding _execmany_returning
+ _pgconn _execmany_returning
__weakref__
""".split()
self._iresult = 0
self._rowcount = -1
self._query: Optional[PostgresQuery]
- self._encoding = "utf-8"
# None if executemany() not executing, True/False according to returning state
self._execmany_returning: Optional[bool] = None
if reset_query:
raise e.InterfaceError("the cursor is closed")
self._reset()
- self._encoding = pgconn_encoding(self._pgconn)
if not self._last_query or (self._last_query is not query):
self._last_query = None
self._tx = adapt.Transformer(self)
if status == TUPLES_OK:
return
elif status == FATAL_ERROR:
- raise e.error_from_result(res, encoding=pgconn_encoding(self._pgconn))
+ raise e.error_from_result(res, encoding=self._encoding)
elif status == PIPELINE_ABORTED:
raise e.PipelineAborted("pipeline aborted")
else:
self._reset(reset_query=False)
self._closed = True
+ @property
+ def _encoding(self) -> str:
+ return pgconn_encoding(self._pgconn)
+
class Cursor(BaseCursor["Connection[Any]", Row]):
__module__ = "psycopg"
assert ex.value.diag.severity in conn.info.error_message
conn.close()
- with pytest.raises(psycopg.OperationalError):
- conn.info.error_message
+ assert "NULL" in conn.info.error_message
@pytest.mark.crdb_skip("backend pid")
def test_backend_pid(self, conn):