reptile's profilefree mind, free soulPhotosBlogLists Tools Help

reptile

There are no music lists on this space.
No list items have been added yet.

free mind, free soul

March 06

Solution for segmentation fault when embedding Python in C/C++

 

Embedded python script reverse.py

def rstring(s):
    i = len(s)-1
    t = ''
    while(i > -1):
        t += s[i]
        i -= 1
    return t

 

Host C++ source

#include <Python.h>
#include <stdio.h>

int main() {
  PyObject *strret, *mymod, *strfunc, *strargs;
  char* cstrret;

  Py_Initialize();

  mymod = PyImport_ImportModule("reverse");
  if(mymod == NULL)
    printf("no such module!\n");
  strfunc = PyObject_GetAttrString(mymod, "rstring");

  strargs = Py_BuildValue("(s)", "Hello, world");

  strret = PyEval_CallObject(strfunc, strargs);
  PyArg_Parse(strret, "s", &cstrret);
  printf("Reversed string: %s", cstrret);

  return 0;
}

When C++ executes, seg fault happens.

The root cause is that after C++ host initializes the python interpreter, the interpreter can’t find and import the module reverse. Thus mymod is actually a NULL pointer. Why? The path of reverse module it wants to import is not included in $PYTHONPATH.

Two solutions here. One is to add the path of the module in $PYTHONPATH. The other is to add following lines in C++, right before importing module, so that python interpreter can find the module.

   PyRun_SimpleString("import sys");
   PyRun_SimpleString("sys.path.append(\"module_path\")");

February 17

Can't exec 'cc1plus' error of MinGW on Vista

The possible reason could be that MinGW can't modify PATH env due to tight security on Vista. cc1plus is located under $MinGW_installdir\libexec\gcc\mingw32\$(gcc_version). Adding this location to $PATH solves the problem.

December 25

Restore Windows' bootloader after removing Linux

After installing a Linux distro on a Windows XP system, the Windows' bootloader will be replaced by GRUB or LILO in MBR, which directs user in either Windows or Linux you installed.

The following are the steps to restore XP's bootloader.

1. Restart the computer with Windows XP CD.

2. Choose to repair Windows to enter recovery console.

3. Select the number which represents the Windows system.

3. Type fixmbr. The windows bootloader will be restored in MBR.


June 10

Google Gears' bug on firefox


After installing Google Gears in my Google Reader on FF for one or two days, the font size in Reader changed. It was either too big or too small. This made me very uncomfortable when reading it. I couldn't change it back to which it used to be.

Now even better, the Reader in FF became blank from yesterday, except for the logo on the top left and my Google ID and other menus on the top right. No matter how many times I refreshed, it's still blank. The menu links on top right don't work either. I couldn't even sign out from Reader. I tried Reader on IE later, it was good. I've been using FF since it's 1.0 was leased, and I really don't want to switch back to IE.


However, the other FFs installed on my home computer and a Linux box still work well. These FFs are the same version, 2.0.0.4. The only difference is that I didn't install Gears on those 2 FFs.

Now to clean up the mess. Fire up FF. Click Tools --> Add-ons and find Google Gears. What? They don't allow uninstall? This is supposed to be M$'s style, not Google's. What about "Don't be evil" motto? Well, though no uninstall, it can be disabled. Anyway, this is still better than M$.

Now Reader in FF is back to normal. Happy ending.

 
There are no photo albums.