Repository navigation
fix: quote negative zero floats and decimals with a leading space - #1480
Conversation
dvarrazzo
left a comment
There was a problem hiding this comment.
Thank you, good catch.
Proposing a few fixes. When you are ready please squash the commits.
|
Thanks for the review! I've shortened the news entry, folded the negative zero cases into the existing On I'll squash the commits before merge. |
860e9be to
85ba923
Compare
|
Thank you for the changes. Rebased, will merge after tests pass. |
Adding the space avoids that a placeholder preceded with a minus sign such as '-%s' is converted to '--' and interpreted as a comment. This was already done for negative numbers, but -0 failed the test.
85ba923 to
17c5743
Compare
Requested by Taltal · project thread
When a value is merged into the query on the client side (
sql.Literal,sql.SQL.format(),ClientCursor), the float and Decimal text dumpers put a space in front of negative numbers, so thatselect 1-%sdoesn't becomeselect 1--1. The check isobj >= 0, which is true for-0.0andDecimal("-0"), but their text form still starts with-:Everything after
--is then a comment, here including theWHEREclause. Same output with the Python and the C implementation. Negative zero is easy to get without noticing (-0.0 * 5,round(-0.04, 1),Decimal("-0.00")read from a file).The fix checks the sign on the dumped text instead (
value.startswith(b"-")), in_SpecialValuesDumper.quote()and in the CFloatDumper.quote()/DecimalDumper.quote(). Special values such as'-Infinity'::float8are returned before that check, so they don't change. Integer dumpers are unchanged, since an int can't be negative zero.Tests: added
test_quote_minus_zerototests/types/test_numeric.py. It doesn't need a database. WithPSYCOPG_IMPL=pythonit fails on master and passes with this change. I couldn't build the C extension locally (no libpq headers), so the C side relies on CI: I checked that_psycopg.pyxstill cythonizes and passes cython-lint, and the installed 3.3.8 binary wheel fails the new test as expected. isort, black, flake8, mypy and codespell pass on the touched files. I also added a news entry.I used Claude Code to help find and fix this. I have reviewed the change and can answer questions about it.