Issue 97: Select with empty 'text' field causes MemoryError when unicode_strings=True on Ubuntu/FreeTDS/iODBC
Status:  Complete
Owner: ----
Closed:  Dec 2011
Reported by av1024@gmail.com, Apr 13, 2010
What steps will reproduce the problem?
1. conn=connect('...', unicode_strings=True)
2. c = conn.execute('SELECT TOP 1 text_field FROM aTable')
   print c.fetchone() # where text_field is NULL

What is the expected output? What do you see instead?
Expected 'None' or empty string. Causes MemoryError exception where
unicode_strings=True

What version of the product are you using? On what operating system?
pyODBC 2.1.7, Ubuntu 9.04+, FreeTDS/tdsodbc 0.82-14 shipped with Ubuntu
python 2.5.x, 2.6.2

Please provide any additional information below.
Looks like some conversion error. Executing 'select DATALENGTH(text_field)'
returns 'None'

Apr 16, 2010
Project Member #1 mkleehammer
The Unicode handling has been revamped in the Python 3.x line, and I think I need to bring it over to the 2.x 
line.

For reference, can you tell me the value of sys.maxunicode on your build:

  import sys
  print sys.maxunicode

I suspect it is UCS4, which for some reason, has become a very popular configuration in the Linux distros.

Also, if I can't reproduce this, can I give you a custom pyd (DLL) to try?
Status: Investigating
Apr 16, 2010
Project Member #2 mkleehammer
Another thing I noticed, the value you want is "unicode_results", not "unicode_strings".

(I know this seems error prone, but lots of people like to use keywords to pass the DSN values and it is a DB API 
standard, so I allow it.  If I don't recognize the keyword, it simply gets added to the DSN.)

Can you confirm which you were using?
Apr 18, 2010
#3 av1024@gmail.com
The sys.maxunicode == 1114111 == 0x10FFFF.
On my (other) win64 machine maxunicode==65535.

Of course, "unicode_results".

Yes, I will try your custom pyd.
Nov 17, 2010
#4 s...@mitre.org
I've also encountered this error, except the type I've encountered it on is nvarchar and it doesn't matter what unicode_results is.

My configuration:

pyodbc 2.1.8
FreeTDS CVS checkout from 11/16/2010
MacOS X 10.6.4
Python 2.6
iODBC driver manager
SQL Server 2008 RTM

I get the same behavior as the reporter with 'select DATALENGTH...' on the relevant column.

sys.maxunicode = 65535

Dec 26, 2011
Project Member #5 mkleehammer
I'm closing this because I believe it is fixed in the 3.0.x builds.  Also, Microsoft now has a Linux driver which definitely works.

Status: Complete
Oct 9, 2013
#6 gsib...@gmail.com
Still encountering this issue on OS/X.  iODBC and FreeTDS.