4

I am using Ubuntu 12.04, Python 2.7.3.
I am having a segmentation fault in a C extension I have co-written. It seems to come from a pointer that was not free'd properly.

I then use valgrind to find memory leaks. According to that answer, I have to compile Python in debug mode to get a valgrind friendly version of Python and get rid of its irrelevant reports.

How to compile Python in debug mode?

Even though the answer I linked answers part of that question, it does not provide me enough details.
Indeed, I want to understand what is happening, not just type things at some places because "who knows? It could work".

Hence, I would like to know:

  • What to download to compile Python?
  • Where to type that ./configure?
  • What is going to happen to my current installation? Is it going to affect my system?
    I have read at many places that many processes on Ubuntu 12.04 are managed by Python and I do not want to mess up anything.

I am also trying to find answers to the questions mentioned by Yair Daon's comment:

  • Do you have to recompile Python once you are done using its debug compilation?

    • If yes, how to compile Python back to its standard mode?
Community
  • 1
  • 1
DRz
  • 1,078
  • 1
  • 11
  • 29
  • The question "how to compile Python in debug mode" is given in the accepted answer to the question you linked to. If that answer is not sufficient for your needs you'll have to explain them in more detail. – ChrisGPT was on strike Apr 18 '16 at 15:19
  • Only a command is written. I read the answer and I do not know what to download nor where to type that command, nor what is going to happen to my previous installation of Python (I do not want to mess with my current installation of Python...). This is why I am asking here for a full explanation. – DRz Apr 18 '16 at 15:26
  • 2
    Thank you for updating your question. IMO the best thing for you to do will be to look at the official [developer's guide](https://docs.python.org/devguide/). You will probably find most of what you need under the Quick Start heading and on the [setup page](https://docs.python.org/devguide/setup.html#setup). If you build a new Python in your home directory it shouldn't conflict with your system Python. Just make sure not to run `sudo make install` or similar (really, aside from installing system libraries etc. with `apt-get`, you shouldn't need `sudo` at all). – ChrisGPT was on strike Apr 18 '16 at 15:49
  • 2
    Do **not** alter the system Python under Ubuntu, you will screw up your system because too many Ubuntu mechanisms are Python based. Don't do a `sudo make install`, run your custom interpreter from your personal build directory. – msw Apr 18 '16 at 16:54

1 Answers1

3

Here are some inputs for anyone trying to compile Python in debug mode on Ubuntu:

  1. Download the version you need from the python website.
  2. Untar it using tar -xf and go to the new directory.

    Example:
    tar -xf Python2.7.3.tgz cd Python-2.7.3

  3. Configure your python installer for debug mode, using ./configure --with-pydebug. It will create a Makefile that you will just have to run.

  4. Compile the sources to create your new python interpreter by running the Makefile, using: make install.

As you create a new interpreter, your system's Python will stay clean.

If you compiled it using --prefix=/home/username/workspace/project/python/, you can now run your script with your new Python interpreter using:

/home/username/workspace/project/python/bin/python script.py

Or, you can also add the shebang line #!/home/username/workspace/project/python/bin/python at the beginning of your script, give it the execute privilege (sudo chmod 764 script.py) and run it more easily using your new Python interpreter by typing ./script.py.

Note: you may want to check Python's documentation for more configuring / compiling flags (such as --prefix / -j, thanks Chris for the link).

DRz
  • 1,078
  • 1
  • 11
  • 29