mirror of
https://github.com/postgres/postgres.git
synced 2025-07-17 06:41:09 +03:00
Prefer pg_any_to_server/pg_server_to_any over pg_do_encoding_conversion.
A large majority of the callers of pg_do_encoding_conversion were specifying the database encoding as either source or target of the conversion, meaning that we can use the less general functions pg_any_to_server/pg_server_to_any instead. The main advantage of using the latter functions is that they can make use of a cached conversion-function lookup in the common case that the other encoding is the current client_encoding. It's notationally cleaner too in most cases, not least because of the historical artifact that the latter functions use "char *" rather than "unsigned char *" in their APIs. Note that pg_any_to_server will apply an encoding verification step in some cases where pg_do_encoding_conversion would have just done nothing. This seems to me to be a good idea at most of these call sites, though it partially negates the performance benefit. Per discussion of bug #9210.
This commit is contained in:
@ -90,11 +90,9 @@ PLyUnicode_Bytes(PyObject *unicode)
|
||||
{
|
||||
PG_TRY();
|
||||
{
|
||||
encoded = (char *) pg_do_encoding_conversion(
|
||||
(unsigned char *) utf8string,
|
||||
strlen(utf8string),
|
||||
PG_UTF8,
|
||||
GetDatabaseEncoding());
|
||||
encoded = pg_any_to_server(utf8string,
|
||||
strlen(utf8string),
|
||||
PG_UTF8);
|
||||
}
|
||||
PG_CATCH();
|
||||
{
|
||||
@ -109,7 +107,7 @@ PLyUnicode_Bytes(PyObject *unicode)
|
||||
/* finally, build a bytes object in the server encoding */
|
||||
rv = PyBytes_FromStringAndSize(encoded, strlen(encoded));
|
||||
|
||||
/* if pg_do_encoding_conversion allocated memory, free it now */
|
||||
/* if pg_any_to_server allocated memory, free it now */
|
||||
if (utf8string != encoded)
|
||||
pfree(encoded);
|
||||
|
||||
@ -149,10 +147,7 @@ PLyUnicode_FromString(const char *s)
|
||||
char *utf8string;
|
||||
PyObject *o;
|
||||
|
||||
utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
|
||||
strlen(s),
|
||||
GetDatabaseEncoding(),
|
||||
PG_UTF8);
|
||||
utf8string = pg_server_to_any(s, strlen(s), PG_UTF8);
|
||||
|
||||
o = PyUnicode_FromString(utf8string);
|
||||
|
||||
|
Reference in New Issue
Block a user