2

Good morning, can somebody please help me.. I am desperate.

I created a basic example to migrate a C++ class to python. I have successfully converted from C++ -> JAVA but now I need it in Python. I do it in Visual Studio 2010 on Windows 8, (and tried on Windows 7 32bit too). Everything works, .py was generated and .pyd was generated too, but when i run the python program it crashes. It writes program stop working (classic windows window). I found it crash just right on library load. It is this line:

_mod = imp.load_module('_KatanaWrapper', fp, pathname, description)

But this whole file is generated from SWIG so i don't think there is something wrong. It must be something with SWIG? Does anyone know?

I do it in a similar way as here description

Edit 2x: This is my c++ code. There is all right...its so simple and it works in c++.

Test.h

#pragma once

class Test
{
   public:
     Test(void);
     ~Test(void);
     int mySum(int first, int second);
};

Test.c

#include "Test.h"
Test::Test(void)
{
}

int Test::mySum(int first,int second) {
return first + second;
}
Test::~Test(void)
{
}

Edit: And here is my interface file (nothing special):

/* File : translate.i */
%module KatanaWrapper

%{
/* Includes the header in the wrapper code */
#include "Test.h"
%}

/* Parse the header file to generate wrappers */
%include "Test.h"

And command line of this:

$(SolutionDir)\..\swig\swig.exe -c++ -python %(FullPath)

Edit: and interesting is, when I change this command to

$(SolutionDir)\..\swig\swig.exe -c++ -java %(FullPath)

It i must write to generated classes package (package testapplication;) but it still don't works (java crashes) but when i change command like this:

$(SolutionDir)\..\swig\swig.exe -c++ -java -package testapplication %(FullPath)

It works! Is something similar in PYTHON? Can help this?

This is compiler options generated by VS. It's little bit chaos and there is Java includes too (for Java migration)

  /I"C:\Program Files (x86)\Java\jdk1.7.0_17\include\win32" /I"C:\Program Files (x86)\Java\jdk1.7.0_17\include" /I"C:\Python27\include" /I"C:\Python33\include" /Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" /D "KATANAWRAPPER_EXPORTS" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\_KatanaWrapper.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue 
Community
  • 1
  • 1
majkl-cz
  • 31
  • 4
  • I don't think you've included enough information for anyone to be able to answer this. If it's crashing in the way you describe, the problem will lie in the C[++] code. Your Visual Studio debugger ought to be able to tell you which line of the C[++] code is causing the problem. – Aya Apr 14 '13 at 13:58
  • I added code. But as i write..there is all right :( And even I dont run any method in python file. I just try run genereated basic script. – majkl-cz Apr 14 '13 at 14:05
  • Well, the `imp.load_module()` will cause some of the SWIG-generated C code to run, and it's most likely crashing somewhere in there. You should be able to run `python.exe` under the debugger, so that when you import your module, the debugger will stop at the exact line of C code where it's crashing. I'd start by trying that. – Aya Apr 14 '13 at 14:14
  • I have no experiences with debuging :( Is that something like it? I made this [screenshot](http://imageshack.us/photo/my-images/213/screenpydebug.jpg/) – majkl-cz Apr 14 '13 at 14:32
  • Yep. That's the sort of thing I was talking about. Well, it looks like it's failing in `PyModule_Create()`, although it's difficult to tell why exactly. Can you include the contents of your SWIG interface file in the question? – Aya Apr 14 '13 at 14:41
  • Yes, its done. But this is very simple too... – majkl-cz Apr 14 '13 at 14:47
  • I can't see anything obviously wrong. If you also add in the contents of `Test.h` to the post, I'd have enough to see if I can reproduce the fault. – Aya Apr 14 '13 at 14:52
  • Works for me on Linux. I'm wondering if it's to do with your compiler options? – Aya Apr 14 '13 at 15:10
  • I haven't used MS Visual Studio for a **really** long time, so I'm not sure there's much more I can do. All I can suggest is to take a look at [this question](http://stackoverflow.com/questions/11693047/how-to-create-a-dll-with-swig-from-visual-studio-2010). – Aya Apr 14 '13 at 15:23
  • I added something to my post but I dont know if it is what you writing about. And I also added some with command line. Btw thans for responding. The fact is it works to JAVA and this is weird why not to python :( – majkl-cz Apr 14 '13 at 15:24
  • 1
    Well, the `/I"C:\Python27\include" /I"C:\Python33\include"` looks a little suspicious - you probably shouldn't be including both at the same time. Try limiting to the one which matches the Python version you're testing on. – Aya Apr 14 '13 at 15:31
  • Yes but i added this after this problems so I try another python version. So It didn't helps when I remove second include :( – majkl-cz Apr 14 '13 at 15:42
  • So problem solved as i wrote in my topic. Seriously thanks for your willingness, I'm a dork :) – majkl-cz Apr 14 '13 at 16:34
  • @majkl-cz: Why don't you answer your own question? As to what was happening with the lib directory...C++ compilers don't use wildcards. When you put the *.*, VS probably tried to find a directory named *.*. If you did it from a command line, VS would interpret it as if every file was a directory. – kirbyfan64sos Apr 14 '13 at 17:45
  • I didn't answer it bacuse of time limit to answer. It writed a few hours. Now It is possible. I will repair it later I must go now (for train :) ) Thanks for explanation – majkl-cz Apr 14 '13 at 18:09

1 Answers1

0

------------ SOLVED ------------

Hmmmm i found point of my problem. I had python lib folder included in Linker -> Input -> Aditional Dependences with this syntaxe (necessary .):

C:\Python33\libs\*.*

When I put this into VC++ Directories -> Library Directories with this syntaxe:

C:\Python33\libs

It works suddenly. I don't understand diference but it works and it is important. Sorry for the fuss.

majkl-cz
  • 31
  • 4