Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use PyUnicode_Encode API #792

Merged
merged 3 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cnxninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool CnxnInfo_init()
static PyObject* GetHash(PyObject* p)
{
#if PY_MAJOR_VERSION >= 3
Object bytes(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(p), PyUnicode_GET_SIZE(p), 0));
Object bytes(PyUnicode_AsUTF8String(p));
if (!bytes)
return 0;
p = bytes.Get();
Expand Down
24 changes: 21 additions & 3 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1921,9 +1921,10 @@ bool ExecuteMulti(Cursor* cur, PyObject* pSql, PyObject* paramArrayObj)
if (PyUnicode_Check(objCell))
{
const TextEnc& enc = cur->cnxn->sqlwchar_enc;
int cb = PyUnicode_GET_DATA_SIZE(objCell) / 2;

PyObject* bytes = NULL;

#if PY_MAJOR_VERSION < 3
int cb = PyUnicode_GET_DATA_SIZE(objCell) / 2;
const Py_UNICODE* source = PyUnicode_AS_UNICODE(objCell);

switch (enc.optenc)
Expand All @@ -1941,11 +1942,28 @@ bool ExecuteMulti(Cursor* cur, PyObject* pSql, PyObject* paramArrayObj)
bytes = PyUnicode_EncodeUTF16(source, cb, "strict", BYTEORDER_BE);
break;
}

#else
switch (enc.optenc)
{
case OPTENC_UTF8:
bytes = PyUnicode_AsUTF8String(objCell);
break;
case OPTENC_UTF16:
bytes = PyUnicode_AsUTF16String(objCell);
break;
case OPTENC_UTF16LE:
bytes = PyUnicode_AsEncodedString(objCell, "utf_16_le", NULL);
keitherskine marked this conversation as resolved.
Show resolved Hide resolved
break;
case OPTENC_UTF16BE:
bytes = PyUnicode_AsEncodedString(objCell, "utf_16_be", NULL);
break;
}
#endif
if (bytes && PyBytes_Check(bytes))
{
objCell = bytes;
}
//TODO: Raise or clear error when bytes == NULL.
}

szLastFunction = "SQLPutData";
Expand Down