classification
Title: embedded python import cmath
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Thanh Ly, amaury.forgeotdarc, mark.dickinson, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2011-06-01 00:31 by Thanh Ly, last changed 2019-10-22 22:56 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
my_py.cpp Thanh Ly, 2011-06-04 21:06
MDW 48 Ch Deck Card-import_cmath_issue.zip Thanh Ly, 2011-06-04 21:06
Messages (9)
msg137408 - (view) Author: Thanh Ly (Thanh Ly) Date: 2011-06-01 00:31
Calling Python script from C/C++ seems to work fine until using 'import cmath' causing the C/C++ application to hang. Other modules can be imported such as 'math'.
msg137612 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-06-03 23:14
Hanging is different from crashing.
2.6 and 3.1 (soon) are only open for security issues.
Perhaps your system is missing something needed for the import.

If you want this to stay open, retest with 2.7 or 3.2 and give much more information: your system/OS, C/C++ compiler, your Python binary (copy the startup line in the interactive interpreter), and a minimal code that shows the problem.
msg137662 - (view) Author: Thanh Ly (Thanh Ly) Date: 2011-06-04 21:06
Hi Terry, I do need to do a lot more investigating into the issue.

I'm using borland builder 6 on a win XP PC to create a interface DLL in 
C/C++ for a GUI to call into a large number of pre-written Python test 
scripts.

I'm using Python26.dll (I will try using the lib file) to create my embedded 
Python environment and redirecting stdin, stdout and stderr bo to the GUI.

The strange issue is when calling the script containing 'import cmath' from 
Python console, it works  fine. However when calling a script containing 
'import cmath' my embedded method (GUI), the application hangs. all other 
'import's work fine from my embedded environment.

I've attached a copy of my project with 'my_py.cpp' including the following 
snippets:

PyRun_SimpleString( //"import cmath\n"    // TRIED HERE!!! 'import cmath'
                                "import log\n"
                                "import sys\n"
                                "class StdoutCatcher:\n"
                                "\tdef write(self, str):\n"
                                "\t\tlog.CaptureStdout(str)\n"
                                "class StderrCatcher:\n"
                                "\tdef write(self, str):\n"
                                "\t\tlog.CaptureStderr(str)\n"
                                "sys.stdout = StdoutCatcher()\n"
                                "sys.stderr = StderrCatcher()\n");

and I tried within a script using:

pValue = DLL_PyObject_CallObject(pFunc, pArgs);    // CALLED HERE!!! python 
scripted containing 'import cmath'

Thanks,
Thanh Ly.

-----Original Message----- 
From: Terry J. Reedy
Sent: Saturday, June 04, 2011 12:14 AM
To: thanh.l@ntlworld.com
Subject: [issue12232] embedded python import cmath

Terry J. Reedy <tjreedy@udel.edu> added the comment:

Hanging is different from crashing.
2.6 and 3.1 (soon) are only open for security issues.
Perhaps your system is missing something needed for the import.

If you want this to stay open, retest with 2.7 or 3.2 and give much more 
information: your system/OS, C/C++ compiler, your Python binary (copy the 
startup line in the interactive interpreter), and a minimal code that shows 
the problem.

----------
nosy: +terry.reedy
stage:  -> test needed
type: crash -> behavior
versions:  -Python 2.6

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue12232>
_______________________________________
msg137672 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-06-05 00:10
{It you reply by mail, please snip off the message you are replying to.}

From what I have read, Windows is not a very pleasant environment for extending and embedding.

1. It works best if both Python and the extender or embedder are compiled with the same version (of Microsoft VC). Does Borland Builder 6 claim compatibility with any particular versions of MS C++?

2. Problems like this probably need a debug build of Python, which you would have to make yourself, and unlike *nux, Windows does not come with a system compiler. (There is now, however, a free VC Express, but I believe the one needed to go with 2.6 is no longer available ;-(.

I am inclined to close this both because it does not involve current Python and because it appears to be an issue with BB or the interface that is out of our control (as you said, Python by itself works fine). I am only leaving this open to give you a chance to provide evidence otherwise with 2.7 or 3.2.

I recommend that you might try the trivial 'import cmath\n' with other Python versions. Which versions would depend on what BB6 claims to be compatible with.

I also recommend that you ask for help on python-list or other forums
(such a Stackoverflow). Use a subject like "Embedding Python 2.6 with Borland Builder 6" This might catch the attention of someone who knows something about BB.
msg137759 - (view) Author: Thanh Ly (Thanh Ly) Date: 2011-06-06 17:44
Hi Terry

With BB 6 I import python26.dll and embed and extend Python in the same 
project (sent) fairly successfully. I converted the DLL to a BB format lib 
file and manage to extend Python successfully but could not link when trying 
to embed Python with the Lib file.

I would like to try with 2.7 and 3.1 however, I'm hard against it, time wise 
and not sure when I can verify the issue.

I did manage to try with 2.4 (as lib file was readily available for BB) and 
it worked.

Thank you for your advice and recommendations.

Thanh.
msg137769 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-06-06 19:19
Is it possible that the problem is related to floating point exceptions?
A quick Google search showed similar issue in another embedded library:
http://www.softintegration.com/support/faq/embed.html#borland

It seems important to add this instruction:
   _control87(MCW_EM, MCW_EM);
Can you try and modify your program accordingly?
msg137770 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-06-06 19:51
> Is it possible that the problem is related to floating point exceptions?

Hmm.  It's possible, but it's still a bit surprising that the crash occurs at import time rather than when calling one of the cmath functions.  But it's very possible that there are some missing PyFPE_START_PROTECT macros in the cmath source.

BTW, if the problem *is* in cmath, it's likely to also occur with Python 2.7:  that module changed very little between 2.6 and 2.7.
msg137772 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-06-06 19:56
Looking at the cmath source, I'd be a bit suspicious that either the Py_NAN or the Py_HUGE_VAL macro (or both) is triggering an FPE.  Both those values are used during module initialization.
msg355168 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-22 22:56
No activity since 2011, I close the issue.
History
Date User Action Args
2019-10-22 22:56:32vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg355168

resolution: out of date
stage: test needed -> resolved
2011-06-07 01:23:09terry.reedysetversions: + Python 2.7
2011-06-06 19:56:32mark.dickinsonsetmessages: + msg137772
2011-06-06 19:51:01mark.dickinsonsetmessages: + msg137770
2011-06-06 19:19:47amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg137769
2011-06-06 17:44:03Thanh Lysetmessages: + msg137759
2011-06-05 00:10:13terry.reedysetmessages: + msg137672
2011-06-04 21:06:07Thanh Lysetfiles: + my_py.cpp, MDW 48 Ch Deck Card-import_cmath_issue.zip

messages: + msg137662
2011-06-04 15:47:24mark.dickinsonsetnosy: + mark.dickinson
2011-06-03 23:14:21terry.reedysetversions: - Python 2.6
nosy: + terry.reedy

messages: + msg137612

type: crash -> behavior
stage: test needed
2011-06-01 00:31:39Thanh Lycreate