Skip to content

Fix PL/Python crash and re-enable plpython_returns - #2410

Open
tuhaihe wants to merge 3 commits into
apache:mainfrom
tuhaihe:plpython-returns-py3
Open

tuhaihe wants to merge 3 commits into
apache:mainfrom
tuhaihe:plpython-returns-py3

Conversation

@tuhaihe

@tuhaihe tuhaihe commented Nov 24, 2026

Copy link
Copy Markdown
Member

Fixes #ISSUE_Number

What does this PR do?

This PR fixes a backend crash in PL/Python and turns the Cloudberry-only plpython_returns regression test back on. The test has been disabled since the Python 3 port in 2020.

  1. Fix PL/Python crash when __str__ returns non-str

PLyObject_AsString() passed the result of PyObject_Str() (or PyObject_Repr() for a float) to PLyUnicode_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.

CREATE FUNCTION f() RETURNS text AS $$
class Foo:
    def __str__(self):
        return None
return Foo()
$$ LANGUAGE plpython3u;
SELECT f();  -- crashed the backend before this fix

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.

  1. Re-enable plpython_returns for Python 3

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 did exec('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 run exec() with an explicit namespace dict and use plpython3u.

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:

  • bytea accepts only bytes-like objects.
  • dict repr keeps insertion order.
  • A str with a NUL byte is cut at the NUL.
  • Ragged multidimensional arrays get the newer error text.

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

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


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
-- 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be improved, otherwise LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants