Skip to content

Commit

Permalink
Use unsigned 16 bit integer in protocol
Browse files Browse the repository at this point in the history
In the Front End / Back End protocol we previously used a signed int
where we should have used an unsigned int. This has the effect of
doubling the number of parameters that can be sent.
  • Loading branch information
tlocke committed Feb 22, 2024
1 parent 7d0cdde commit 4ef917e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pg8000/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def pack_funcs(fmt):


i_pack, i_unpack = pack_funcs("i")
h_pack, h_unpack = pack_funcs("h")
H_pack, H_unpack = pack_funcs("H")
ii_pack, ii_unpack = pack_funcs("ii")
ihihih_pack, ihihih_unpack = pack_funcs("ihihih")
ci_pack, ci_unpack = pack_funcs("ci")
Expand Down Expand Up @@ -623,7 +623,7 @@ def handle_BACKEND_KEY_DATA(self, data, context):
self._backend_key_data = data

def handle_ROW_DESCRIPTION(self, data, context):
count = h_unpack(data)[0]
count = H_unpack(data)[0]
idx = 2
columns = []
input_funcs = []
Expand Down Expand Up @@ -656,7 +656,7 @@ def handle_ROW_DESCRIPTION(self, data, context):
def send_PARSE(self, statement_name_bin, statement, oids=()):
val = bytearray(statement_name_bin)
val.extend(statement.encode(self._client_encoding) + NULL_BYTE)
val.extend(h_pack(len(oids)))
val.extend(H_pack(len(oids)))
for oid in oids:
val.extend(i_pack(0 if oid == -1 else oid))

Expand Down Expand Up @@ -762,7 +762,7 @@ def send_BIND(self, statement_name_bin, params):
"""https://www.postgresql.org/docs/current/protocol-message-formats.html"""

retval = bytearray(
NULL_BYTE + statement_name_bin + h_pack(0) + h_pack(len(params))
NULL_BYTE + statement_name_bin + H_pack(0) + H_pack(len(params))
)

for value in params:
Expand All @@ -772,7 +772,7 @@ def send_BIND(self, statement_name_bin, params):
val = value.encode(self._client_encoding)
retval.extend(i_pack(len(val)))
retval.extend(val)
retval.extend(h_pack(0))
retval.extend(H_pack(0))

self._send_message(BIND, retval)
_write(self._sock, FLUSH_MSG)
Expand Down
9 changes: 9 additions & 0 deletions test/native/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,12 @@ def test_not_parsed_if_no_params(mocker, con):
mock_to_statement = mocker.patch("pg8000.native.to_statement")
con.run("ROLLBACK")
mock_to_statement.assert_not_called()


def test_max_parameters(con):
SIZE = 60000
kwargs = {f"param_{i}": 1 for i in range(SIZE)}
con.run(
f"SELECT 1 WHERE 1 IN ({','.join([f':param_{i}' for i in range(SIZE)])})",
**kwargs,
)

0 comments on commit 4ef917e

Please sign in to comment.