Repository navigation
Conversation
PLyObject_AsString() calls PyObject_Str(), or PyObject_Repr() for a
float, and gives the result to PLyUnicode_Bytes() without a check.
When the object's __str__ or __repr__ raises an exception or returns
a value that is not a string, the call returns NULL.
PLyUnicode_Bytes() then reads the NULL pointer and the backend gets
SIGSEGV.
For example, this crashes the server:
CREATE FUNCTION f() RETURNS text AS $$
class Foo:
def __str__(self):
return None
return Foo()
$$ LANGUAGE plpython3u;
SELECT f();
Check the result and raise "could not create string representation
of Python object", the same error as Python 2 gave. This is the crash
that the FIXME in the plpython Makefile talks about. PostgreSQL master
has the same code and also crashes.
Backpatch-through: REL_2_STABLE
Assisted-by: Claude Code
The Cloudberry-only plpython_returns test checks how PL/Python maps
return values to SQL types, on the coordinator and on a segment. It
has been out of REGRESS since the Python 3 port, because each test
function did exec('y = ' + s) and then returned y. In Python 3,
exec() cannot create a local variable of the calling function, so
every call failed with NameError.
Run exec() with an explicit namespace dict and return y from it. Some
arguments are statements, not expressions, so eval() is not enough.
Also use plpython3u, because plpythonu no longer exists.
Four FROM-clause calls had an extra '}' in the argument, so they only
tested a SyntaxError, and its text changes in Python 3.12. Fix them to
match the SELECT-clause calls above them.
Add a test for a float subclass with a bad __repr__, which used to
crash the backend, and add the test back to REGRESS.
The new expected output differs from the Python 2 one where Python 3
behaves differently:
- bytea accepts only bytes-like objects, so str, bool and non-empty
dict values now raise an error.
- A str with a NUL byte is cut at the NUL, not rejected.
- dict repr keeps insertion order.
- Errors for ragged multidimensional arrays use the newer text.
Assisted-by: Claude Code
This alternative expected file is left from the time when PL/Python supported both Python 2 and Python 3. Its functions use LANGUAGE plpythonu, but sql/plpython_types.sql now uses plpython3u, so pg_regress can never match it. Apart from the language name, it is the same as plpython_types.out. PostgreSQL 16 does not have this file. Assisted-by: Claude Code
reshke
reviewed
Nov 24, 2026
| -- b) The returned string contains null characters (valid in Python) | ||
| -- [ERROR] From Python String with null characters | ||
| -- Cloudberry: with Python 3, PLyUnicode_Bytes() stops at the first NUL, | ||
| -- so the string is silently truncated instead of rejected. |
Contributor
There was a problem hiding this comment.
I think this can be improved, otherwise LGTM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #ISSUE_Number
What does this PR do?
This PR fixes a backend crash in PL/Python and turns the Cloudberry-only
plpython_returnsregression test back on. The test has been disabled since the Python 3 port in 2020.__str__returns non-strPLyObject_AsString()passed the result ofPyObject_Str()(orPyObject_Repr()for a float) toPLyUnicode_Bytes()without a NULL check. When__str__or__repr__raises an exception or returns a value that is not a string, the backend got SIGSEGV. Now it raises "could not create string representation of Python object", the same error that Python 2 gave.This is the crash that the old FIXME in the plpython Makefile mentions. The code comes from upstream PostgreSQL, and PostgreSQL master crashes in the same way.
The test checks how PL/Python maps return values to SQL types, on the coordinator and on a segment (FROM
gp_single_row). Each test function didexec('y = ' + s)and then return y, which fails in Python 3 because exec() cannot create a local variable of the calling function. The functions now runexec()with an explicit namespace dict and useplpython3u.Four FROM-clause calls had an extra
}, so they only tested a SyntaxError, and its text changed in Python 3.12. They now match the SELECT-clause calls above them. A test for a float subclass with a bad__repr__covers the second crash path.The expected output differs from the Python 2 one only where Python 3 behaves differently:
3. Remove the unused plpython_types_3.out
This alternative expected file is left from the time when PL/Python supported both Python 2 and Python 3. It uses
LANGUAGE plpythonu, so it can never match, and PostgreSQL 16 does not have it.Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions