Skip to content

Commit 2956745

Browse files
committed
Fix mapping of PostgreSQL encodings to Python encodings.
Windows encodings, "win1252" and so forth, are named differently in Python, like "cp1252". Also, if the PyUnicode_AsEncodedString() function call fails for some reason, use a plain ereport(), not a PLy_elog(), to report that error. That avoids recursion and crash, if PLy_elog() tries to call PLyUnicode_Bytes() again. This fixes bug reported by Asif Naeem. Backpatch down to 9.0, before that plpython didn't even try these conversions. Jan Urbański, with minor comment improvements by me.
1 parent 5b214b4 commit 2956745

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/pl/plpython/plpython.c

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,16 +3690,71 @@ PLyUnicode_Bytes(PyObject *unicode)
36903690
const char *serverenc;
36913691

36923692
/*
3693-
* Python understands almost all PostgreSQL encoding names, but it doesn't
3694-
* know SQL_ASCII.
3693+
* Map PostgreSQL encoding to a Python encoding name.
36953694
*/
3696-
if (GetDatabaseEncoding() == PG_SQL_ASCII)
3697-
serverenc = "ascii";
3698-
else
3699-
serverenc = GetDatabaseEncodingName();
3695+
switch (GetDatabaseEncoding())
3696+
{
3697+
case PG_SQL_ASCII:
3698+
/*
3699+
* Mapping SQL_ASCII to Python's 'ascii' is a bit bogus. Python's
3700+
* 'ascii' means true 7-bit only ASCII, while PostgreSQL's
3701+
* SQL_ASCII means that anything is allowed, and the system doesn't
3702+
* try to interpret the bytes in any way. But not sure what else
3703+
* to do, and we haven't heard any complaints...
3704+
*/
3705+
serverenc = "ascii";
3706+
break;
3707+
case PG_WIN1250:
3708+
serverenc = "cp1250";
3709+
break;
3710+
case PG_WIN1251:
3711+
serverenc = "cp1251";
3712+
break;
3713+
case PG_WIN1252:
3714+
serverenc = "cp1252";
3715+
break;
3716+
case PG_WIN1253:
3717+
serverenc = "cp1253";
3718+
break;
3719+
case PG_WIN1254:
3720+
serverenc = "cp1254";
3721+
break;
3722+
case PG_WIN1255:
3723+
serverenc = "cp1255";
3724+
break;
3725+
case PG_WIN1256:
3726+
serverenc = "cp1256";
3727+
break;
3728+
case PG_WIN1257:
3729+
serverenc = "cp1257";
3730+
break;
3731+
case PG_WIN1258:
3732+
serverenc = "cp1258";
3733+
break;
3734+
case PG_WIN866:
3735+
serverenc = "cp866";
3736+
break;
3737+
case PG_WIN874:
3738+
serverenc = "cp874";
3739+
break;
3740+
default:
3741+
/* Other encodings have the same name in Python. */
3742+
serverenc = GetDatabaseEncodingName();
3743+
break;
3744+
}
3745+
37003746
rv = PyUnicode_AsEncodedString(unicode, serverenc, "strict");
37013747
if (rv == NULL)
3702-
PLy_elog(ERROR, "could not convert Python Unicode object to PostgreSQL server encoding");
3748+
{
3749+
/*
3750+
* Use a plain ereport instead of PLy_elog to avoid recursion, if
3751+
* the traceback formatting functions try to do unicode to bytes
3752+
* conversion again.
3753+
*/
3754+
ereport(ERROR,
3755+
(errcode(ERRCODE_INTERNAL_ERROR),
3756+
errmsg("could not convert Python Unicode object to PostgreSQL server encoding")));
3757+
}
37033758
return rv;
37043759
}
37053760

0 commit comments

Comments
 (0)