title: Python always crashes using pydobc connection
body: <p>I am having a hard time with <code>pyodbc</code> module (<code>python3</code>). The following code always crashes python (I run it from DOS terminal). The crash happens when <code>main()</code> function returns. My data source is a 4D v13 database remote server. I am using 4D-ODBC driver which seems to be properly installed but really not efficient driver. I have disabled SSL connection and my firewall.</p>
  
  <pre><code>class ODBCSource:
  
      def __init__(self, dsn):
          self.dsn = str(dsn)
          try:
              self.con = pyodbc.connect("dsn={}".format(self.dsn))
              self.cur = self.con.cursor()
              logger.info("ODBC Source DSN='{}' connected.".format(self.dsn))
          except Exception as err:
              self.con = None
              self.cur = None
              logger.error("Cannot connect ODBC Source DSN='{}': {}.".format(self.dsn, err))
  
      def __bool__(self):
          return not(self.con is None) or not(self.cur is None)
  
      def __str__(self):
          return "&lt;cripython.dal.ODBCSource id={:#x} dsn='{}'&gt;".format(id(self), self.dsn)
  
      __repr__ = __str__
  
  # Main Function:
  def main():
      db = ODBCSource('CRIPI-4D')
  
  # Program Entry Point:
  if(__name__ == "__main__"):
      main()
  </code></pre>
  
  <p>Windows crash window trace give me the following informations:</p>
  
  <pre><code>Problem signature:
    Problem Event Name:   APPCRASH
    Application Name: python.exe
    Application Version:  0.0.0.0
    Application Timestamp:    5066b7a2
    Fault Module Name:    4DODBC.dll
    Fault Module Version: 14.0.1.0
    Fault Module Timestamp:   53b4d1d6
    Exception Code:   c0000005
    Exception Offset: 00031798
    OS Version:   6.1.7601.2.1.0.256.48
    Locale ID:    2060
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
  </code></pre>
  
  <p>I have enabled ODBC Trace in order to check what could be wrong with the code. I have found 3 errors in the log.</p>
  
  <pre><code>[...]
  odbcsource      1724-2a8    ENTER SQLGetTypeInfo 
          HSTMT               0x0059B1F0
          SWORD                       12 &lt;SQL_VARCHAR&gt;
  
  odbcsource      1724-2a8    EXIT  SQLGetTypeInfo  with return code -1 (SQL_ERROR)
          HSTMT               0x0059B1F0
          SWORD                       12 &lt;SQL_VARCHAR&gt;
  
          DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0) 
  
  odbcsource      1724-2a8    ENTER SQLGetTypeInfo 
          HSTMT               0x0059B1F0
          SWORD                       -9 &lt;SQL_WVARCHAR&gt;
  
  odbcsource      1724-2a8    EXIT  SQLGetTypeInfo  with return code -1 (SQL_ERROR)
          HSTMT               0x0059B1F0
          SWORD                       -9 &lt;SQL_WVARCHAR&gt;
  
          DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0) 
  
  odbcsource      1724-2a8    ENTER SQLGetTypeInfo 
          HSTMT               0x0059B1F0
          SWORD                       -2 &lt;SQL_BINARY&gt;
  
  odbcsource      1724-2a8    EXIT  SQLGetTypeInfo  with return code -1 (SQL_ERROR)
          HSTMT               0x0059B1F0
          SWORD                       -2 &lt;SQL_BINARY&gt;
  
          DIAG [24000] [Microsoft][ODBC Driver Manager] Invalid cursor state (0) 
  
  odbcsource      1724-2a8    ENTER SQLFreeStmt 
          HSTMT               0x0059B1F0
          UWORD                        0 &lt;SQL_CLOSE&gt;
   [...]
  </code></pre>
  
  <p>By the way, when I run this script from Python IDLE I get no error at all. Does anyone know what is going wrong with this code and how should I solve it?</p>