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

Ci test #2

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires-python = ">=3.8"
#
# https://cibuildwheel.readthedocs.io/en/stable/options/#requires-python

description = "DB API module for ODBC"
description = "DB API module for ODBC foo test whatever"
readme = "README.md"
license = {text = "MIT-0 License"}

Expand Down
6 changes: 3 additions & 3 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static char* StrDup(const char* text) {
}


static bool Connect(PyObject* pConnectString, HDBC hdbc, long timeout, Object& encoding)
static bool Connect(PyObject* pConnectString, HDBC hdbc, long timeout, PyObject* encoding)
{
assert(PyUnicode_Check(pConnectString));

Expand Down Expand Up @@ -170,7 +170,7 @@ static bool ApplyPreconnAttrs(HDBC hdbc, SQLINTEGER ikey, PyObject *value, char
}

PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, long timeout, bool fReadOnly,
PyObject* attrs_before, Object& encoding)
PyObject* attrs_before, PyObject* encoding)
{
//
// Allocate HDBC and connect
Expand All @@ -196,7 +196,7 @@ PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, long timeou
PyObject* value = 0;

Object encodingholder;
char *strencoding = encoding.Get() ?
char *strencoding = (encoding != 0) ?
(PyUnicode_Check(encoding) ? PyBytes_AsString(encodingholder = PyCodec_Encode(encoding, "utf-8", "strict")) :
PyBytes_Check(encoding) ? PyBytes_AsString(encoding) : 0) : 0;

Expand Down
2 changes: 1 addition & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct Connection
* exception is set and zero is returned.
*/
PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, long timeout, bool fReadOnly,
PyObject* attrs_before, Object& encoding);
PyObject* attrs_before, PyObject* encoding);

/*
* Used by the Cursor to implement commit and rollback.
Expand Down
2 changes: 0 additions & 2 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,6 @@ static PyObject* execute(Cursor* cur, PyObject* pSql, PyObject* params, bool ski
Py_XDECREF(prevParam->pObject);
newParam.BufferLength = newParam.StrLen_or_Ind;
newParam.StrLen_or_Ind = SQL_DATA_AT_EXEC;
Py_INCREF(cell);
newParam.pObject = cell;
*prevParam = newParam;
if(prevParam->ParameterValuePtr == &newParam.Data)
{
Expand Down
1 change: 1 addition & 0 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ bool BindParameter(Cursor* cur, Py_ssize_t index, ParamInfo& info)
{
// Bind the TVP's columns --- all need to use DAE
PyObject *param = PySequence_GetItem(row, i);
Py_XDECREF(param);
GetParameterInfo(cur, i, param, info.nested[i], true);
info.nested[i].BufferLength = info.nested[i].StrLen_or_Ind;
info.nested[i].StrLen_or_Ind = SQL_DATA_AT_EXEC;
Expand Down
2 changes: 1 addition & 1 deletion src/pyodbcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs)
int fAutoCommit = 0;
int fReadOnly = 0;
long timeout = 0;
Object encoding;
PyObject* encoding = 0;

Object attrs_before; // Optional connect attrs set before connecting

Expand Down
27 changes: 27 additions & 0 deletions tests/postgresql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,30 @@ def convert(value):
cursor.connection.add_output_converter(pyodbc.SQL_WVARCHAR, None)
value = cursor.execute("select v from t1").fetchone()[0]
assert value == '123.45'


def test_refcount_encoding():
"""
Ensure we handle the reference count to `encoding` properly. In the past we freed a
borrowed reference. This would cause a segfault.
"""
# https://github.com/mkleehammer/pyodbc/issues/1343
import sys
encoding = 'utf-16le'
count_before = sys.getrefcount(encoding)

def _test():
# I've moved this into a function so the exception's stack trace will be freed under
# the covers when we leave the function. Otherwise we'd have a 2nd reference to
# `encoding` in the stack trace of the exception.
try:
cnxn = pyodbc.connect(CNXNSTR, encoding=encoding)
except:
pass

for i in range(10):
_test()

count_after = sys.getrefcount(encoding)

assert count_after == count_before