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.

classification
Title: Embedding Python3.4 - PyUnicode_Check fails (MinGW-W64)
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.4
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Ashish Sadanandan, pitrou, vstinner
Priority: normal Keywords:

Created on 2015-03-07 20:02 by Ashish Sadanandan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
g++dashv.txt Ashish Sadanandan, 2015-03-07 20:02 Output of "g++ -v"
Messages (4)
msg237472 - (view) Author: Ashish Sadanandan (Ashish Sadanandan) Date: 2015-03-07 20:02
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?
msg237488 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-03-08 01:05
Your interpretation of PyUnicode_Check() is correct. I don't know why your program fails but I would suggest some kind of compiler bug or incompatibility. You can find the definition of the unicode type in Objects/unicodeobject.c. There you'll find that Py_TPFLAGS_UNICODE_SUBCLASS is set in PyUnicode_Type's tp_flags.

(for the record, under Windows we only support builds made with MSVC)
msg237592 - (view) Author: Ashish Sadanandan (Ashish Sadanandan) Date: 2015-03-09 05:31
Thanks for the reply. MSVC does produce the expected results, I forgot to mention that earlier.

So I guess this means I can't realistically expect anyone to look into this problem? I can switch to MSVC but the spotty C++11/14 support is irritating to have to live with.
msg342534 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-15 02:39
No activity for 4 years. I close the issue.
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67791
2019-05-15 02:39:04vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg342534

resolution: out of date
stage: resolved
2015-03-09 05:31:46Ashish Sadanandansetmessages: + msg237592
2015-03-08 01:05:40pitrousetnosy: + pitrou
messages: + msg237488
2015-03-07 21:04:42Ashish Sadanandansettitle: MinGW-64 and embedding Python3.4 -> Embedding Python3.4 - PyUnicode_Check fails (MinGW-W64)
2015-03-07 20:03:55Ashish Sadanandansettitle: MinGW-64 -> MinGW-64 and embedding Python3.4
2015-03-07 20:02:05Ashish Sadanandancreate