Skip to content

Support bytearray and memoryview as parameters #153

Open

Description

@Cito

bytearray and memoryview objects cannot be used as parameters. In pg, they are converted with str() like objects of unknown types, so that values like "bytearray(b'abc')" or '<memory at 0x7f...>' are stored without an error, even in bytea columns and with inserttable(). pgdb raises an InterfaceError ("Do not know how to adapt type"), except for hstore values. In 6.3, pg will raise a TypeError and pgdb an InterfaceError in all these cases, so that no wrong data is stored.

psycopg 3 sends both types as bytea, like bytes. I plan to do the same in 6.4, with one rule everywhere: query parameters in pg and pgdb, also inline and as elements of arrays and records, inserttable() and escape_bytea(). memoryview objects that are not contiguous will be copied instead of rejected (psycopg 3 raises BufferError for them).

Open questions:

  • In the C layer, bytes parameters are passed unchanged as text in the client encoding. Should bytearray and memoryview always be sent as bytea there, or behave like bytes?
  • Should the data be copied, or the buffer held during the call?
  • Cursor.copy_from() should also be checked with such objects.

Activity

  1. added this to the 6.4 milestone on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions