Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b66de4c

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 fc548b2 commit b66de4c

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/pl/plpython/plpy_util.c

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

6767
/*
68-
* Python understands almost all PostgreSQL encoding names, but it doesn't
69-
* know SQL_ASCII.
68+
* Map PostgreSQL encoding to a Python encoding name.
7069
*/
71-
if (GetDatabaseEncoding() == PG_SQL_ASCII)
72-
serverenc = "ascii";
73-
else
74-
serverenc = GetDatabaseEncodingName();
70+
switch (GetDatabaseEncoding())
71+
{
72+
case PG_SQL_ASCII:
73+
/*
74+
* Mapping SQL_ASCII to Python's 'ascii' is a bit bogus. Python's
75+
* 'ascii' means true 7-bit only ASCII, while PostgreSQL's
76+
* SQL_ASCII means that anything is allowed, and the system doesn't
77+
* try to interpret the bytes in any way. But not sure what else
78+
* to do, and we haven't heard any complaints...
79+
*/
80+
serverenc = "ascii";
81+
break;
82+
case PG_WIN1250:
83+
serverenc = "cp1250";
84+
break;
85+
case PG_WIN1251:
86+
serverenc = "cp1251";
87+
break;
88+
case PG_WIN1252:
89+
serverenc = "cp1252";
90+
break;
91+
case PG_WIN1253:
92+
serverenc = "cp1253";
93+
break;
94+
case PG_WIN1254:
95+
serverenc = "cp1254";
96+
break;
97+
case PG_WIN1255:
98+
serverenc = "cp1255";
99+
break;
100+
case PG_WIN1256:
101+
serverenc = "cp1256";
102+
break;
103+
case PG_WIN1257:
104+
serverenc = "cp1257";
105+
break;
106+
case PG_WIN1258:
107+
serverenc = "cp1258";
108+
break;
109+
case PG_WIN866:
110+
serverenc = "cp866";
111+
break;
112+
case PG_WIN874:
113+
serverenc = "cp874";
114+
break;
115+
default:
116+
/* Other encodings have the same name in Python. */
117+
serverenc = GetDatabaseEncodingName();
118+
break;
119+
}
120+
75121
rv = PyUnicode_AsEncodedString(unicode, serverenc, "strict");
76122
if (rv == NULL)
77-
PLy_elog(ERROR, "could not convert Python Unicode object to PostgreSQL server encoding");
123+
{
124+
/*
125+
* Use a plain ereport instead of PLy_elog to avoid recursion, if
126+
* the traceback formatting functions try to do unicode to bytes
127+
* conversion again.
128+
*/
129+
ereport(ERROR,
130+
(errcode(ERRCODE_INTERNAL_ERROR),
131+
errmsg("could not convert Python Unicode object to PostgreSQL server encoding")));
132+
}
78133
return rv;
79134
}
80135

0 commit comments

Comments
 (0)