This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ashish Sadanandan
Recipients Ashish Sadanandan
Date 2015-03-07.20:02:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1425758526.01.0.875818979723.issue23603@psf.upfronthosting.co.za>
In-reply-to
Content
I'm trying to embed Python 3.4.3 (x64) in a program compiled using MinGW-W64 g++ 4.9.2 (output from "g++ -v" attached) and Boost.Python 1.57.0. A simple example kept crashing at runtime and I managed to track it down to this testcase (which is not using Boost.Python but demonstrates why a check in Boost is failing). `python34.zip` in the `Py_SetPath()` call is a zip archive containing the entire contents of the `Lib` directory in my Python3.4 installation.


		#include <Python.h>
		#include <iostream>

		int main()
		{
			Py_SetPath(L"python34.zip");
			Py_Initialize();

			PyObject *s = PyUnicode_FromString("Hello World");
			std::cout << PyUnicode_Check(s) << std::endl;
			std::cout << PyUnicode_CheckExact(s) << std::endl;
			std::cout << PyUnicode_AsUTF8(s) << std::endl;

			PyRun_SimpleString("from time import time, ctime\n"
							   "print('Today is', ctime(time())\n)");

			Py_Finalize();
		}


I compile this using

                g++ -ID:/Tools/Python/3.4/x64/include -O0 -g3 -pedantic -Wall -Wextra -std=c++14 test.cpp -LD:/Tools/Python/3.4/x64/libs -lpython34 -o test.exe

Running test.exe results in

                0
                1
                Hello World
                Today is Sat Mar  7 12:06:53 2015

The problem is the first line of output. Creating a `PyObject` using `PyUnicode_FromString()` and then calling `PyUnicode_Check()` on the earlier result is returning `0`. The cause of this is that the `tp_flags` field somewhere within `PyObject` is `0` and `PyUnicode_Check()` performs a bitand with that and returns `0`. If I understand the docs correctly, when `PyUnicode_CheckExact()` returns true, `PyUnicode_Check()` should also return true because the former is a more stringent check than the latter.

Additional details that may or may not be relevant. I followed these steps to create `libpython34.a` for linking with g++. From an MSYS prompt 

                $ gendef.exe /C/Windows/System32/python34.dll
                $ dlltool --dllname /C/Windows/System32/python34.dll --def python34.def --output-lib libpython34.a

I also tried downloading libpython34.a from Christoph Gohlke's website (http://www.lfd.uci.edu/~gohlke/pythonlibs/#libpython) but that produces the same result.

Is this a bug, or do I not understand what `PyUnicode_Check()` is supposed to do?
History
Date User Action Args
2015-03-07 20:02:06Ashish Sadanandansetrecipients: + Ashish Sadanandan
2015-03-07 20:02:06Ashish Sadanandansetmessageid: <1425758526.01.0.875818979723.issue23603@psf.upfronthosting.co.za>
2015-03-07 20:02:05Ashish Sadanandanlinkissue23603 messages
2015-03-07 20:02:05Ashish Sadanandancreate