1

I'm a Python newbie having problems with embedding Python 3.2 in a piece of C++ code.

Here's an example. The code looks like this:

std::cout << "Hello world" << std::endl;
std::wcout << "Hello world" << std::endl;

PyIntitialize();

std::cout << "Hello world" << std::endl;
std::wcout << "Hello world" << std::endl;

And the console output looks like this:

Hello World
Hello World
Hello World
H e l l o  W o r l d

It appears that the width of a wchar_t has been reset by Python to twice its original size. On the other hand the width of a char remains the same after the initialization.

Details: Win7 - 64 bit. Visual Studio 2008 for the C++ code sample above. I'm also using Visual Studio 2008 to build Python from source.

Why is Python changing the stdoutput stream (if this is indeed the case as it appears) and what can I do to fix the problem? We use wcout throughout the code, so it has to work (and indeed it did when we were using Python 2.6).

Many thanks.

  • 1
    Well, it does indeed seem odd, but clearly `wchar_t` is not having its width modified! – David Heffernan Feb 23 '12 at 17:24
  • 2
    Your question is cut off at "Is there a setting in the" -- something is missing. :) – sarnold Feb 23 '12 at 22:29
  • Could be something to do with the format flags. Compare the output of `cout << wcout.flags() << "\n";` before and after you call `PyInitialize()`. – spencercw Feb 24 '12 at 00:53
  • Thanks, I'll try examining the format flags as spencerw suggests. Additionally, as I mentioned, I'm building Python from source. After I posted the initial question to the group, I experimented with changing the "Character Set" setting in Visual Studio from "Not Set" to "Use Unicode Character Set" and rebuilt. My thinking was that this might address the issue of the width of the wchar_t. – Alan Nichols Feb 24 '12 at 12:42
  • I'm now getting a problem with Python being unable to load the codecs. So my question is, is building under the unicode character set the correct thing to be doing? If so, is there a way to address the codecs issue? Again, many thanks. – Alan Nichols Feb 24 '12 at 12:44
  • 4
    You have far too many unknowns to make much headway. First of all try to rule out building Python from source as a candidate for the problem. I can't believe you didn't mention that first time round! Try with a standard binary distro to see if the same behaviour occurs. If it does you can narrow down the search. – David Heffernan Feb 24 '12 at 15:12
  • So after a lot of trial-and-error I came up with a solution that works; I'll post it here in the hope that it may be of use to others. The trick was in the first answer here: [link](http://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app), like this:`code` char* locale = setlocale(LC_ALL, "English"); // Get the CRT's current locale. std::locale lollocale(locale); setlocale(LC_ALL, locale); // Restore the CRT. std::wcout.imbue(lollocale); // Now set the std::wcout to have the locale that we got from the CRT. `code` – Alan Nichols Mar 20 '12 at 13:26

0 Answers0