Embedded Python and MySQLdb (was Re: PyApache and MySQLdb (was Re: anyone get one of the python apache modules to work with 2.1?))

Brandon Long blong at fiction.net
Tue Aug 28 23:59:36 EDT 2001


On 08/28/01 Brandon Long uttered the following other thing:
> 
> Ok, it seems somehow related to the MySQLdb module (0.9.0)

Its definitely the MySQLdb module, unless there is something that
PyApache is doing that its not supposed to.

I've got this simple embedded python wrapper which is doing
approximately what PyApache is doing, and its showing the bug.
(attached is pytest.c and test.py)

Anyone have any idea was MySQLdb is doing wrong?
 
It essentially seems to be causing Python to no longer be able to
recognize builtin exceptions on the second iteration, which results in
an exception like the following in site.py.

> On the second request, the import of the site.py module fails (during
> import __builtin__) with:
> 'import site' failed; traceback:
> Traceback (most recent call last):
>   File "/neo/opt/encap/python-2.1-nothread/lib/python2.1/site.py", line 60, in ?    
>      import sys, os
>   File "/neo/opt/encap/python-2.1-nothread/lib/python2.1/os.py", line 54, in ?
>     __all__.extend(_get_exports_list(posix))
>   File "/neo/opt/encap/python-2.1-nothread/lib/python2.1/os.py", line 35, in _get_exports_list
>     return list(module.__all__)
> AttributeError: 'posix' module has no attribute '__all__'

Brandon
-- 
 "Here's a nickel, kid.  Get yourself a better computer." 
     -"Dilbert" by Scott Adams 6-24-95
                                           http://www.fiction.net/blong/
-------------- next part --------------

import sys, os
import time 
import MySQLdb

print "hello world"
-------------- next part --------------
/* To compile:
 *  gcc -o pytest.o -c pytest.c -I/path/to/include/python2.1
 *  gcc -rdynamic -o pytest pytest.o -L/path/to/lib/python2.1/config -lpython2.1 -lieee -lm -lutil -lpthread -ldl
 */

#include <Python.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

PyThreadState *TState = NULL;

int py_init ()
{
  if (!Py_IsInitialized())
  {
      Py_Initialize();
#ifdef WITH_THREAD
      PyEval_InitThreads();
#endif

      TState = PyEval_SaveThread();
      Py_VerboseFlag = 0;
      Py_DebugFlag = 0;
  }
}

int execute_python_script ()
{
  PyThreadState *tstate;
  FILE *fp;

#ifdef WITH_THREAD
  PyEval_AcquireLock();
#endif
  Py_VerboseFlag = 1;

  tstate = Py_NewInterpreter();

  fp = fopen("test.py", "r");
  if (fp == NULL)
  {
    fprintf(stderr, "Unable to open test.py: [%d] %s\n", errno, strerror(errno));
#ifdef WITH_THREAD
  PyEval_ReleaseLock();
#endif
    return -1;
  }

  PyRun_SimpleFile(fp, "test.py");

  fclose(fp);

  Py_EndInterpreter (tstate);

#ifdef WITH_THREAD
  PyEval_ReleaseLock();
#endif
}

int py_clean()
{
#ifdef WITH_THREAD  
  if (TState)
    PyEval_AcquireThread (TState);
  TState = NULL;
  Py_Finalize();
#else
  Py_Finalize();
#endif
}

int main(int argc, char **argv)
{
  int x;
  py_init();

  for (x = 0; x < 2; x++)
  {
    fprintf(stderr, "Executing %d\n", x);
    execute_python_script();
  }

  py_clean();
}


More information about the Python-list mailing list