[Pythonmac-SIG] OSX10.6/readline-6.1/python-2.6.5

Zvezdan Petkovic zvezdan at zope.com
Tue May 25 20:11:10 CEST 2010


On May 25, 2010, at 10:25 AM, Chris Kees wrote:

> Hi,
> 
> I'm compiling python and readline from source and getting a segmentation fault in readline after two lines of input. Anybody seen this before or know the fix? Summary of the problem follows.
> 
> % python
> Python 2.6.5 (r265:3362, May 25 2010, 09:06:56) 
> [GCC 4.2.1 (Apple Inc. build 5659)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = 1
> >>> b = 2
> Segmentation fault


I've seen this before if a Mac OS X native editline (readline emulation) is used with Mac OS X version <= 10.4.  This was fixed and patched for 10.6 and 10.5 in Python 2.6.5 release.

You should be able to use a native editline (readline).  There's no need for an external readline library if you use Python 2.6.5 with Mac OS X 10.6 or 10.5.


> The built python works fine if I compile without readline support. My readline and python configuration steps look like this:
> 
> readline:
> configure --prefix=${MY_PREFIX} CC=/usr/bin/gcc
> python:
> ./configure --prefix=${MY_PREFIX} --enable-framework=${MY_PREFIX}  CFLAGS="-I${MY_PREFIX}/include" LDFLAGS="-L${MY_PREFIX}/lib" --with-readline-dir=${MY_PREFIX}


It is better to use CPPFLAGS="-I${MY_PREFIX}/include" to pass include path, not CFLAGS.  Using CPPFLAGS ensures that the order of include path parts is the same as Python build intended it to be: -I. -IInclude ... are first and then user's CPPFLAGS include path parts. 

This is a configuration that worked for me with GNU readline from MacPorts (in /opt/local/...)

	./configure \
	    --prefix=${HOME}/opt \
	    LDFLAGS=-L/opt/local/lib \
	    CPPFLAGS=-I/opt/local/include \
	    MACOSX_DEPLOYMENT_TARGET=10.6

One can check that Python was linked against it using:

	OPT_PYTHON=~/opt/bin/python2.6
	MODULE_PATH="import readline; print readline.__file__"
	otool -L $(${OPT_PYTHON} -c "${MODULE_PATH}")

It should return:

	/opt/local/lib/libreadline.X.Y.dylib


> Also, I've tried MACOSX_DEPLOYMENT_TARGET=10.3 and 10.6.


The default configuration for Intel-based Mac sets the deployment target to 10.4 (Tiger).  For a PPC Mac, the default configuration sets the deployment target to 10.3 (Panther).

As mentioned before, if you set the deployment target to 10.6, you don't need an external copy of readline (unless you want specific GNU readline features).

This configuration builds a 64-bit Python 2.6 linked against the native editline.

	./configure \
	    --prefix=${HOME}/opt \
	    MACOSX_DEPLOYMENT_TARGET=10.6

The check with otool (see above) returns:

	/usr/lib/libedit.2.dylib
	(compatibility version 2.0.0, current version 2.11.0) 

(Add more options for a universal build or a 32-bit only build.)

Now, let's see the gdb output ...


> Running python in gdb shows the 
> % gdb python
> GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar  5 04:43:10 UTC 2010)
> Copyright 2004 Free Software Foundation, Inc.
> GDB is free software, covered by the GNU General Public License, and you are
> welcome to change it and/or distribute copies of it under certain conditions.
> Type "show copying" to see the conditions.
> There is absolutely no warranty for GDB.  Type "show warranty" for details.
> This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ..... done
> 
> (gdb) run
> Starting program: /Users/cekees/src/proteus/darwin/bin/python 
> Reading symbols for shared libraries ++++. done
> 
> Program received signal SIGTRAP, Trace/breakpoint trap.
> 0x00007fff5fc01028 in __dyld__dyld_start ()
> (gdb) c
> Continuing.
> Reading symbols for shared libraries .......... done
> Python 2.6.5 (r265:3362, May 25 2010, 09:06:56) 
> [GCC 4.2.1 (Apple Inc. build 5659)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> Reading symbols for shared libraries ... done
> >>> a = 1
> >>> b = 2
> 
> Program received signal EXC_BAD_ACCESS, Could not access memory.
> Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
> 0x00000001003ee0a7 in call_readline (sys_stdin=<value temporarily unavailable, due to optimizations>, sys_stdout=<value temporarily unavailable, due to optimizations>, prompt=<value temporarily unavailable, due to optimizations>) at /Users/cekees/src/proteus/externalPackages/Python-2.6.5/Modules/readline.c:1037
> 1037				line = history_get(state->length)->line;


The line above should be executed for GNU readline.
However, for the native editline it causes a crash because of off-by-one array access.

You should check what library is the readline module linked against using otool as shown above.  It seems that it's linked against libedit, but compiled in a way that prevents it from using the code specific to libedit.

This line should be executed for libedit:

	line = history_get(state->length - 1)->line;

Are you sure that Python can actually find your copy of readline?
What is the exact path to your copy of readline library?
Is your readline built as 64-bit, 32-bit, or universal?
Is your Python built as 64-bit, 32-bit, or universal?

There are many possible reasons why your build of Python did not link against your copy of GNU readline and more information is needed to pinpoint the exact one.

Python 2.6.5 on Mac OS X 10.6 can be successfully linked against:

(a) GNU readline, and
(b) native Mac OS X editline.

It works fine with both.


> (gdb) back
> #0  0x00000001003ee0a7 in call_readline (sys_stdin=<value temporarily unavailable, due to optimizations>, sys_stdout=<value temporarily unavailable, due to optimizations>, prompt=<value temporarily unavailable, due to optimizations>) at /Users/cekees/src/proteus/externalPackages/Python-2.6.5/Modules/readline.c:1037
> #1  0x00000001000086e2 in PyOS_Readline (sys_stdin=0x7fff702be0c0, sys_stdout=0x7fff702be158, prompt=0x10059cf24 ">>> ") at Parser/myreadline.c:208
> #2  0x000000010000a088 in tok_nextc (tok=0x100458310) at Parser/tokenizer.c:784
> #3  0x000000010000a81a in tok_get [inlined] () at /Users/cekees/src/proteus/externalPackages/Python-2.6.5/Parser/tokenizer.c:1131
> #4  0x000000010000a81a in PyTokenizer_Get (tok=0x100458310, p_start=0x7fff5fbfe658, p_end=0x7fff5fbfe650) at Parser/tokenizer.c:1571
> #5  0x00000001000054da in parsetok (tok=0x100458310, g=<value temporarily unavailable, due to optimizations>, start=<value temporarily unavailable, due to optimizations>, err_ret=0x7fff5fbfe6b0, flags=0x7fff5fbfe6dc) at Parser/parsetok.c:159
> #6  0x00000001000dd592 in orte_iof_base_setup_prefork () at Python/pythonrun.c:1463
> #7  0x00000001000de7a3 in orte_ns_base_get_proc_name_string () at Python/pythonrun.c:824
> #8  0x00000001000dea8e in PyRun_InteractiveLoopFlags (fp=0x7fff702be0c0, filename=0x10011a988 "<stdin>", flags=0x7fff5fbfe8e0) at Python/pythonrun.c:764
> #9  0x00000001000df301 in PyRun_AnyFileExFlags (fp=0x7fff702be0c0, filename=0x10011a988 "<stdin>", closeit=0, flags=0x7fff5fbfe8e0) at Python/pythonrun.c:733
> #10 0x00000001000ee697 in orte_rmaps_base_get_job_map () at Modules/main.c:572
> #11 0x0000000100000f14 in ?? ()



More information about the Pythonmac-SIG mailing list