Skip to content

Commit

Permalink
Replace assert macros with assert
Browse files Browse the repository at this point in the history
It is easier to build debug versions of Python now.
  • Loading branch information
mkleehammer committed Aug 26, 2023
1 parent 294f353 commit 1f99534
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 58 deletions.
6 changes: 1 addition & 5 deletions src/cnxninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ bool CnxnInfo_init()
// Called during startup to give us a chance to import the hash code. If we can't find it, we'll print a warning
// to the console and not cache anything.

// First try hashlib which was added in 2.5. 2.6 complains using warnings which we don't want affecting the
// caller.

map_hash_to_info = PyDict_New();

update = PyUnicode_FromString("update");
if (!map_hash_to_info || !update)
return false;

hashlib = PyImport_ImportModule("hashlib");

if (!hashlib)
return false;

Expand Down Expand Up @@ -105,7 +101,7 @@ static PyObject* CnxnInfo_New(Connection* cnxn)
// WARNING: The GIL lock is released for the *entire* function here. Do not
// touch any objects, call Python APIs, etc. We are simply making ODBC
// calls and setting atomic values (ints & chars). Also, make sure the lock
// gets released -- do not add an early exit.
// gets reaquired -- do not add an early exit.

SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
Expand Down
4 changes: 2 additions & 2 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi, long timeou
Object& encoding)
{
// This should have been checked by the global connect function.
I(PyUnicode_Check(pConnectString) || PyUnicode_Check(pConnectString));
assert(PyUnicode_Check(pConnectString) || PyUnicode_Check(pConnectString));

// The driver manager determines if the app is a Unicode app based on whether we call SQLDriverConnectA or
// SQLDriverConnectW. Some drivers, notably Microsoft Access/Jet, change their behavior based on this, so we try
Expand Down Expand Up @@ -1460,7 +1460,7 @@ static PyObject* Connection_exit(PyObject* self, PyObject* args)
Connection* cnxn = (Connection*)self;

// If an error has occurred, `args` will be a tuple of 3 values. Otherwise it will be a tuple of 3 `None`s.
I(PyTuple_Check(args));
assert(PyTuple_Check(args));

if (cnxn->nAutoCommit == SQL_AUTOCOMMIT_OFF)
{
Expand Down
2 changes: 1 addition & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct Connection

SQLLEN GetMaxLength(SQLSMALLINT ctype) const
{
I(ctype == SQL_C_BINARY || ctype == SQL_C_WCHAR || ctype == SQL_C_CHAR);
assert(ctype == SQL_C_BINARY || ctype == SQL_C_WCHAR || ctype == SQL_C_CHAR);
if (maxwrite != 0)
return maxwrite;
if (ctype == SQL_C_BINARY)
Expand Down
14 changes: 7 additions & 7 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ static bool create_name_map(Cursor* cur, SQLSMALLINT field_count, bool lower)
ODBCCHAR *szName = NULL;
SQLRETURN ret;

I(cur->hstmt != SQL_NULL_HANDLE && cur->colinfos != 0);
assert(cur->hstmt != SQL_NULL_HANDLE && cur->colinfos != 0);

// These are the values we expect after free_results. If this function fails, we do not modify any members, so
// they should be set to something Cursor_close can deal with.
I(cur->description == Py_None);
I(cur->map_name_to_index == 0);
assert(cur->description == Py_None);
assert(cur->map_name_to_index == 0);

if (cur->cnxn->hdbc == SQL_NULL_HANDLE)
{
Expand Down Expand Up @@ -327,8 +327,8 @@ static bool free_results(Cursor* self, int flags)
// 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. */

I((flags & STATEMENT_MASK) != 0);
I((flags & PREPARED_MASK) != 0);
assert((flags & STATEMENT_MASK) != 0);
assert((flags & PREPARED_MASK) != 0);

if ((flags & PREPARED_MASK) == FREE_PREPARED)
{
Expand Down Expand Up @@ -554,7 +554,7 @@ static bool PrepareResults(Cursor* cur, int cCols)
// Allocates the ColumnInfo structures describing the returned data.

int i;
I(cur->colinfos == 0);
assert(cur->colinfos == 0);

cur->colinfos = (ColumnInfo*)PyMem_Malloc(sizeof(ColumnInfo) * cCols);
if (cur->colinfos == 0)
Expand Down Expand Up @@ -2368,7 +2368,7 @@ static PyObject* Cursor_exit(PyObject* self, PyObject* args)
return 0;

// If an error has occurred, `args` will be a tuple of 3 values. Otherwise it will be a tuple of 3 `None`s.
I(PyTuple_Check(args));
assert(PyTuple_Check(args));

if (cursor->cnxn->nAutoCommit == SQL_AUTOCOMMIT_OFF && PyTuple_GetItem(args, 0) == Py_None)
{
Expand Down
8 changes: 4 additions & 4 deletions src/getdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static PyObject* GetText(Cursor* cur, Py_ssize_t iCol)

if (isNull)
{
I(pbData == 0 && cbData == 0);
assert(pbData == 0 && cbData == 0);
Py_RETURN_NONE;
}

Expand All @@ -287,7 +287,7 @@ static PyObject* GetBinary(Cursor* cur, Py_ssize_t iCol)

if (isNull)
{
I(pbData == 0 && cbData == 0);
assert(pbData == 0 && cbData == 0);
Py_RETURN_NONE;
}

Expand All @@ -311,7 +311,7 @@ static PyObject* GetDataUser(Cursor* cur, Py_ssize_t iCol, int conv)

if (isNull)
{
I(pbData == 0 && cbData == 0);
assert(pbData == 0 && cbData == 0);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -355,7 +355,7 @@ static PyObject* GetDataDecimal(Cursor* cur, Py_ssize_t iCol)

if (isNull)
{
I(pbData == 0 && cbData == 0);
assert(pbData == 0 && cbData == 0);
Py_RETURN_NONE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ static char* CreateDecimalString(long sign, PyObject* digits, long exp)
}
}

I(pch == 0 || (int)(strlen(pch) + 1) == len);
assert(pch == 0 || (int)(strlen(pch) + 1) == len);

return pch;
}
Expand Down Expand Up @@ -925,7 +925,7 @@ static bool GetDecimalInfo(Cursor* cur, Py_ssize_t index, PyObject* param, Param
info.DecimalDigits = (SQLSMALLINT)info.ColumnSize;
}

I(info.ColumnSize >= (SQLULEN)info.DecimalDigits);
assert(info.ColumnSize >= (SQLULEN)info.DecimalDigits);

info.ParameterValuePtr = CreateDecimalString(sign, digits, exp);
if (!info.ParameterValuePtr)
Expand Down
17 changes: 0 additions & 17 deletions src/pyodbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,6 @@ inline void _strlwr(char* name)
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

// Building an actual debug version of Python is so much of a pain that it never happens. I'm providing release-build
// versions of assertions.

#if defined(PYODBC_ASSERT) && defined(_MSC_VER)
#include <crtdbg.h>
inline void FailAssert(const char* szFile, size_t line, const char* szExpr)
{
printf("assertion failed: %s(%d)\n%s\n", szFile, (int)line, szExpr);
__debugbreak(); // _CrtDbgBreak();
}
#define I(expr) if (!(expr)) FailAssert(__FILE__, __LINE__, #expr);
#define N(expr) if (expr) FailAssert(__FILE__, __LINE__, #expr);
#else
#define I(expr)
#define N(expr)
#endif

#ifdef PYODBC_TRACE
void DebugTrace(const char* szFmt, ...);
#else
Expand Down
23 changes: 4 additions & 19 deletions src/pyodbcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ PyObject* GetClassForThread(const char* szModule, const char* szClass)
// modules.)

PyObject* dict = PyThreadState_GetDict();
I(dict);
assert(dict);
if (dict == 0)
{
// I don't know why there wouldn't be thread state so I'm going to raise an exception
Expand Down Expand Up @@ -782,16 +782,6 @@ static char getdecimalsep_doc[] =
"Gets the decimal separator character used when parsing NUMERIC from the database.";


#ifdef PYODBC_LEAK_CHECK
static PyObject* mod_leakcheck(PyObject* self, PyObject* args)
{
UNUSED(self, args);
pyodbc_leak_check();
Py_RETURN_NONE;
}
#endif


static PyMethodDef pyodbc_methods[] =
{
{ "connect", (PyCFunction)mod_connect, METH_VARARGS|METH_KEYWORDS, connect_doc },
Expand All @@ -802,11 +792,6 @@ static PyMethodDef pyodbc_methods[] =
{ "TimestampFromTicks", (PyCFunction)mod_timestampfromticks, METH_VARARGS, timestampfromticks_doc },
{ "drivers", (PyCFunction)mod_drivers, METH_NOARGS, drivers_doc },
{ "dataSources", (PyCFunction)mod_datasources, METH_NOARGS, datasources_doc },

#ifdef PYODBC_LEAK_CHECK
{ "leakcheck", (PyCFunction)mod_leakcheck, METH_NOARGS, 0 },
#endif

{ 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -1235,7 +1220,7 @@ PyMODINIT_FUNC PyInit_pyodbc()
PyModule_AddObject(module, "Binary", binary_type);
Py_INCREF(binary_type);

I(null_binary != 0); // must be initialized first
assert(null_binary != 0); // must be initialized first
PyModule_AddObject(module, "BinaryNull", null_binary);

PyModule_AddIntConstant(module, "UNICODE_SIZE", sizeof(Py_UNICODE));
Expand Down Expand Up @@ -1277,7 +1262,7 @@ static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts)
// parts
// A dictionary of text keywords and text values that will be appended.

I(PyUnicode_Check(existing));
assert(PyUnicode_Check(existing));

Py_ssize_t pos = 0;
PyObject* key = 0;
Expand Down Expand Up @@ -1342,7 +1327,7 @@ static PyObject* MakeConnectionString(PyObject* existing, PyObject* parts)
PyUnicode_WriteChar(result, offset++, (Py_UCS4)';');
}

I(offset == length);
assert(offset == length);

return result;
}
2 changes: 1 addition & 1 deletion src/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Tuple

PyObject*& operator[](int i)
{
I(p != 0);
assert(p != 0);
return PyTuple_GET_ITEM(p, i);
}

Expand Down

0 comments on commit 1f99534

Please sign in to comment.