SF.net SVN: mahogany:[7584] trunk/M/src/Python/InitPython.cpp

[email protected] Sat, 05 Dec 2009 00:37:13 +0000
Newsgroups gmane.mail.mahogany.cvs
Message-ID <[email protected]>
Revision: 7584
          http://mahogany.svn.sourceforge.net/mahogany/?rev=7584&view=rev
Author:   vadz
Date:     2009-12-05 00:37:13 +0000 (Sat, 05 Dec 2009)

Log Message:
-----------
Fix setting of PYTHONPATH when using dynamically loaded Python DLL.

We must set PYTHONPATH before loading the DLL as changing its value at CRT
level is not enough when Python DLL uses a different CRT from the one that we
use.

Modified Paths:
--------------
    trunk/M/src/Python/InitPython.cpp

Modified: trunk/M/src/Python/InitPython.cpp
===================================================================
--- trunk/M/src/Python/InitPython.cpp	2009-12-05 00:35:30 UTC (rev 7583)
+++ trunk/M/src/Python/InitPython.cpp	2009-12-05 00:37:13 UTC (rev 7584)
@@ -72,18 +72,18 @@
 extern bool
 InitPython(void)
 {
-   // first check if Python is available
-#ifdef USE_PYTHON_DYNAMIC
-   if ( !InitPythonDll() )
+   if ( !READ_APPCONFIG(MP_USEPYTHON) )
    {
-      wxLogError(_("Python dynamic library couldn't be found."));
-
-      return false;
+      // it is not an error to have it disabled
+      return true;
    }
-#endif // USE_PYTHON_DYNAMIC
 
-   // set PYTHONPATH correctly to find our modules and scripts
-   String pythonPathNew = _T("PYTHONPATH=");
+   // set PYTHONPATH correctly to find our modules and scripts: this must be
+   // done before loading the Python DLL below because its copy of CRT (which
+   // may be different from ours when using VC9) will initialize its _environ
+   // array from Win32 process environment when the DLL is loaded, so we must
+   // update the environment before this happens
+   String pythonPathNew;
 
    String path = READ_APPCONFIG(MP_PYTHONPATH);
    const bool didntHavePath = path.empty();
@@ -124,20 +124,27 @@
    pythonPathNew += path;
 
    // also keep the old path but after our directories
-   const char *pythonPathOld = getenv("PYTHONPATH");
-   if ( pythonPathOld )
+   wxString pythonPathOld;
+   if ( wxGetEnv("PYTHONPATH", &pythonPathOld) )
    {
       pythonPathNew << PATH_SEPARATOR << pythonPathOld;
    }
 
-   // on some systems putenv() takes "char *" so give it non-const pointer
-   putenv(pythonPathNew.char_str());
+   if ( !wxSetEnv("PYTHONPATH", pythonPathNew) )
+   {
+      wxLogWarning(_("Setting PYTHONPATH to \"%s\" failed, loading some "
+                     "Python modules might not work."), pythonPathNew);
+   }
 
-   if ( !READ_APPCONFIG(MP_USEPYTHON) )
+   // check if Python is available at all if we load it during run-time
+#ifdef USE_PYTHON_DYNAMIC
+   if ( !InitPythonDll() )
    {
-      // it is not an error to have it disabled
-      return true;
+      wxLogError(_("Python dynamic library couldn't be loaded."));
+
+      return false;
    }
+#endif // USE_PYTHON_DYNAMIC
 
    // initialise the interpreter -- this we do always, just to avoid problems
    Py_Initialize();
@@ -184,7 +191,7 @@
 
    if ( rc && didntHavePath )
    {
-      // rememember the path
+      // remember the path
       mApplication->GetProfile()->writeEntry(MP_PYTHONPATH, path);
    }
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev