Ticket #4657: boost-1.44-py3.1.patch

File boost-1.44-py3.1.patch, 2.9 KB (added by anonymous, 12 years ago)
  • libs/mpi/src/python/datatypes.cpp

    diff -urN -x '*~' boost_1_44_0/libs/mpi/src/python/datatypes.cpp boost_1_44_0-hacked/libs/mpi/src/python/datatypes.cpp
    old new  
    1717
    1818void export_datatypes()
    1919{
     20#if PY_MAJOR_VERSION < 3
    2021  register_serialized(long(0), &PyInt_Type);
     22#endif
    2123  register_serialized(false, &PyBool_Type);
    2224  register_serialized(double(0.0), &PyFloat_Type);
    2325}
  • libs/mpi/src/python/py_environment.cpp

    diff -urN -x '*~' boost_1_44_0/libs/mpi/src/python/py_environment.cpp boost_1_44_0-hacked/libs/mpi/src/python/py_environment.cpp
    old new  
    1111 *  This file reflects the Boost.MPI "environment" class into Python
    1212 *  methods at module level.
    1313 */
     14
     15#include <locale>
     16#include <string>
    1417#include <boost/python.hpp>
    1518#include <boost/mpi.hpp>
    1619
     
    5053
    5154  // If anything changed, convert C-style argc/argv into Python argv
    5255  if (mpi_argv != my_argv)
     56  {
     57#if PY_MAJOR_VERSION >= 3
     58    // Code stolen from py3k/Modules/python.c.
     59
     60    wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*mpi_argc);
     61    /* We need a second copies, as Python might modify the first one. */
     62    wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*mpi_argc);
     63
     64    if (!argv_copy || !argv_copy2) {
     65      fprintf(stderr, "out of memory\n");
     66      return false;
     67    }
     68
     69    std::locale mylocale;
     70    mbstate_t mystate;
     71
     72    const std::codecvt<char, wchar_t, mbstate_t>& myfacet =
     73      std::use_facet<std::codecvt<char, wchar_t, mbstate_t> >(mylocale);
     74
     75    for (int i = 0; i < mpi_argc; i++)
     76    {
     77      size_t length = strlen(mpi_argv[i]);
     78
     79      wchar_t *dest = (wchar_t *) PyMem_Malloc(sizeof(wchar_t) * (length + 1));
     80
     81      const char *from_next;
     82      wchar_t *to_next;
     83
     84      std::codecvt<wchar_t,char,mbstate_t>::result myresult =
     85        myfacet.out(mystate,
     86            mpi_argv[i], mpi_argv[i] + length + 1, from_next,
     87            dest, dest+length+1, to_next);
     88
     89      if (myresult != std::codecvt<wchar_t,char,mbstate_t>::ok )
     90      {
     91        fprintf(stderr, "failure translating argv\n");
     92        return 1;
     93      }
     94
     95      argv_copy2[i] = argv_copy[i] = dest;
     96      if (!argv_copy[i])
     97          return false;
     98    }
     99
     100    PySys_SetArgv(mpi_argc, argv_copy);
     101
     102    for (int i = 0; i < mpi_argc; i++) {
     103        PyMem_Free(argv_copy2[i]);
     104    }
     105    PyMem_Free(argv_copy);
     106    PyMem_Free(argv_copy2);
     107#else
    53108    PySys_SetArgv(mpi_argc, mpi_argv);
     109#endif
     110  }
    54111
    55   for (int arg = 0; arg < my_argc; ++arg)
    56     free(my_argv[arg]);
    57   delete [] my_argv;
     112  for (int arg = 0; arg < mpi_argc; ++arg)
     113    free(mpi_argv[arg]);
     114  delete [] mpi_argv;
    58115
    59116  return true;
    60117}