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

docs: add more TRACE calls for easier debugging #1317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ static bool ApplyPreconnAttrs(HDBC hdbc, SQLINTEGER ikey, PyObject *value, char
return false;
}

TRACE("ApplyPreconnAttrs.SQLSetConnectAttrW key=%d value=%p vallen=%d\n", ikey, ivalue);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested this locally, but it appears there are 3 placeholders and 2 values.


Py_BEGIN_ALLOW_THREADS
ret = SQLSetConnectAttrW(hdbc, ikey, ivalue, vallen);
Py_END_ALLOW_THREADS
Expand Down Expand Up @@ -186,7 +188,7 @@ PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, long timeou
return RaiseErrorFromHandle(0, "SQLAllocHandle", SQL_NULL_HANDLE, SQL_NULL_HANDLE);

//
// Attributes that must be set before connecting.
// Attributes that must be set before connecting. These are passed via `attr_before` in python.
//

if (attrs_before)
Expand Down Expand Up @@ -363,6 +365,8 @@ static PyObject* Connection_set_attr(PyObject* self, PyObject* args)

Connection* cnxn = (Connection*)self;

TRACE("SQLSetConnectAttr id=%d value=%d\n", id, value);

SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLSetConnectAttr(cnxn->hdbc, id, (SQLPOINTER)(intptr_t)value, SQL_IS_INTEGER);
Expand Down
20 changes: 17 additions & 3 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static bool free_results(Cursor* self, int flags)
// this even when a query has not been executed.

// If we ran out of memory, it is possible that we have a cursor but colinfos is zero. However, we should be
// deleting this object, so the cursor will be freed when the HSTMT is destroyed. */
// deleting this object, so the cursor will be freed when the HSTMT is destroyed.

assert((flags & STATEMENT_MASK) != 0);
assert((flags & PREPARED_MASK) != 0);
Expand All @@ -344,6 +344,8 @@ static bool free_results(Cursor* self, int flags)

if (StatementIsValid(self))
{
TRACE("free_results: %p\n", self);

if ((flags & STATEMENT_MASK) == FREE_STATEMENT)
{
Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -387,6 +389,8 @@ static bool free_results(Cursor* self, int flags)

self->rowcount = -1;

TRACE("free_results: done\n");

return true;
}

Expand All @@ -407,6 +411,8 @@ static void closeimpl(Cursor* cur)
HSTMT hstmt = cur->hstmt;
cur->hstmt = SQL_NULL_HANDLE;

TRACE("SQLFreeHandle\n");

SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
Expand All @@ -415,6 +421,8 @@ static void closeimpl(Cursor* cur)
// If there is already an exception, don't overwrite it.
if (!SQL_SUCCEEDED(ret) && !PyErr_Occurred())
RaiseErrorFromHandle(cur->cnxn, "SQLFreeHandle", cur->cnxn->hdbc, SQL_NULL_HANDLE);
} else {
TRACE("Invalid statement handle, skipping SQLFreeHandle\n");
}

Py_XDECREF(cur->pPreparedSQL);
Expand Down Expand Up @@ -750,12 +758,16 @@ static PyObject* execute(Cursor* cur, PyObject* pSql, PyObject* params, bool ski
const char* pch = PyBytes_AS_STRING(query.Get());
SQLINTEGER cch = (SQLINTEGER)(PyBytes_GET_SIZE(query.Get()) / (isWide ? sizeof(uint16_t) : 1));

TRACE("cursor.execute: isWide=%d query_len=%d\n", isWide, cch);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we build on so many platforms with different pointer & integer widths, I think we should cast anything that is not a plain integer when using %d. Most likely everything is 64 bits nowdays, but it would't hurt.


Py_BEGIN_ALLOW_THREADS
if (isWide)
ret = SQLExecDirectW(cur->hstmt, (SQLWCHAR*)pch, cch);
else
ret = SQLExecDirect(cur->hstmt, (SQLCHAR*)pch, cch);
Py_END_ALLOW_THREADS

TRACE("cursor.execute, finished\n");
}

if (cur->cnxn->hdbc == SQL_NULL_HANDLE)
Expand Down Expand Up @@ -1136,7 +1148,7 @@ static PyObject* Cursor_setinputsizes(PyObject* self, PyObject* sizes)
PyErr_SetString(ProgrammingError, "Invalid cursor object.");
return 0;
}

Cursor *cur = (Cursor*)self;
if (Py_None == sizes)
{
Expand Down Expand Up @@ -2526,6 +2538,8 @@ Cursor_New(Connection* cnxn)

if (cnxn->timeout)
{
TRACE("cursor.new: setting timeout to %d\n", cnxn->timeout);

Py_BEGIN_ALLOW_THREADS
ret = SQLSetStmtAttr(cur->hstmt, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER)(uintptr_t)cnxn->timeout, 0);
Py_END_ALLOW_THREADS
Expand All @@ -2538,7 +2552,7 @@ Cursor_New(Connection* cnxn)
}
}

TRACE("cursor.new cnxn=%p hdbc=%d cursor=%p hstmt=%d\n", (Connection*)cur->cnxn, ((Connection*)cur->cnxn)->hdbc, cur, cur->hstmt);
TRACE("cursor.new: cnxn=%p hdbc=%d cursor=%p hstmt=%d\n", (Connection*)cur->cnxn, ((Connection*)cur->cnxn)->hdbc, cur, cur->hstmt);
}

return cur;
Expand Down
4 changes: 4 additions & 0 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ void FreeParameterData(Cursor* cur)
{
// Unbinds the parameters and frees the parameter buffer.

TRACE("FreeParameterData");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs newline?


if (cur->paramInfos)
{
// MS ODBC will crash if we use an HSTMT after the HDBC has been freed.
Expand All @@ -1330,6 +1332,8 @@ void FreeParameterInfo(Cursor* cur)
// Internal function to free just the cached parameter information. This is not used by the general cursor code
// since this information is also freed in the less granular free_results function that clears everything.

TRACE("FreeParameterInfo");

Py_XDECREF(cur->pPreparedSQL);
PyMem_Free(cur->paramtypes);
cur->pPreparedSQL = 0;
Expand Down