Skip to content

Commit

Permalink
Fix for DSN Names with non-ASCII chars (mkleehammer#951)
Browse files Browse the repository at this point in the history
* Fix for DSN Names with non-ASCII chars

Fixes: mkleehammer#948

Co-authored-by: bamboo <bamboo@galaxy.ad>
Co-authored-by: Gord Thompson <gordon.d.thompson@gmail.com>
  • Loading branch information
3 people authored Dec 16, 2021
1 parent 7aaca60 commit b1cfb75
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/pyodbcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,16 @@ static PyObject* mod_datasources(PyObject* self)
if (!result)
return 0;

SQLCHAR szDSN[500]; // Using a buffer larger than SQL_MAX_DSN_LENGTH + 1 for systems that ignore it
SWORD cbDSN;
// Using a buffer larger than SQL_MAX_DSN_LENGTH + 1 for systems that ignore it.
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
SQLWCHAR szDSN[500];
SQLWCHAR szDesc[500];
#else
SQLCHAR szDSN[500];
SQLCHAR szDesc[500];
#endif

SWORD cbDSN;
SWORD cbDesc;

SQLUSMALLINT nDirection = SQL_FETCH_FIRST;
Expand All @@ -626,12 +633,30 @@ static PyObject* mod_datasources(PyObject* self)

for (;;)
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
// wchar_t and UTF-16 on Windows
ret = SQLDataSourcesW(henv, nDirection, szDSN, _countof(szDSN), &cbDSN, szDesc, _countof(szDesc), &cbDesc);

if (!SQL_SUCCEEDED(ret))
break;

int byteorder = BYTEORDER_NATIVE;
PyObject* key = PyUnicode_DecodeUTF16((char*)szDSN, cbDSN * sizeof(wchar_t), "strict", &byteorder);
PyObject* val = PyUnicode_DecodeUTF16((char*)szDesc, cbDesc * sizeof(wchar_t), "strict", &byteorder);
#else
// UTF-8
ret = SQLDataSources(henv, nDirection, szDSN, _countof(szDSN), &cbDSN, szDesc, _countof(szDesc), &cbDesc);

if (!SQL_SUCCEEDED(ret))
break;

PyDict_SetItemString(result, (const char*)szDSN, PyString_FromString((const char*)szDesc));
PyObject* key = PyString_FromString((const char*)szDSN);
PyObject* val = PyString_FromString((const char*)szDesc);
#endif

if(key && val)
PyDict_SetItem(result, key, val);

nDirection = SQL_FETCH_NEXT;
}

Expand Down

0 comments on commit b1cfb75

Please sign in to comment.