925e6b
diff -up Python-2.7.3/configure.ac.debug-build Python-2.7.3/configure.ac
925e6b
--- Python-2.7.3/configure.ac.debug-build	2012-04-18 19:46:22.066498521 -0400
925e6b
+++ Python-2.7.3/configure.ac	2012-04-18 19:46:22.078498372 -0400
925e6b
@@ -635,7 +635,7 @@ AC_SUBST(LIBRARY)
925e6b
 AC_MSG_CHECKING(LIBRARY)
925e6b
 if test -z "$LIBRARY"
925e6b
 then
925e6b
-	LIBRARY='libpython$(VERSION).a'
925e6b
+	LIBRARY='libpython$(VERSION)$(DEBUG_EXT).a'
925e6b
 fi
925e6b
 AC_MSG_RESULT($LIBRARY)
925e6b
 
925e6b
@@ -780,8 +780,8 @@ if test $enable_shared = "yes"; then
925e6b
 	  INSTSONAME="$LDLIBRARY".$SOVERSION
925e6b
           ;;
925e6b
     Linux*|GNU*|NetBSD*|FreeBSD*|DragonFly*|OpenBSD*)
925e6b
-	  LDLIBRARY='libpython$(VERSION).so'
925e6b
-	  BLDLIBRARY='-L. -lpython$(VERSION)'
925e6b
+	  LDLIBRARY='libpython$(VERSION)$(DEBUG_EXT).so'
925e6b
+	  BLDLIBRARY='-L. -lpython$(VERSION)$(DEBUG_EXT)'
925e6b
 	  RUNSHARED=LD_LIBRARY_PATH=`pwd`:${LD_LIBRARY_PATH}
925e6b
 	  case $ac_sys_system in
925e6b
 	      FreeBSD*)
925e6b
@@ -905,6 +905,14 @@ else AC_MSG_RESULT(no); Py_DEBUG='false'
925e6b
 fi],
925e6b
 [AC_MSG_RESULT(no)])
925e6b
 
925e6b
+if test "$Py_DEBUG" = 'true'
925e6b
+then
925e6b
+	DEBUG_EXT=_d
925e6b
+	DEBUG_SUFFIX=-debug
925e6b
+fi
925e6b
+AC_SUBST(DEBUG_EXT)
925e6b
+AC_SUBST(DEBUG_SUFFIX)
925e6b
+
925e6b
 # XXX Shouldn't the code above that fiddles with BASECFLAGS and OPT be
925e6b
 # merged with this chunk of code?
925e6b
 
925e6b
diff -up Python-2.7.3/Lib/distutils/command/build_ext.py.debug-build Python-2.7.3/Lib/distutils/command/build_ext.py
925e6b
--- Python-2.7.3/Lib/distutils/command/build_ext.py.debug-build	2012-04-09 19:07:29.000000000 -0400
925e6b
+++ Python-2.7.3/Lib/distutils/command/build_ext.py	2012-04-18 19:46:22.079498360 -0400
925e6b
@@ -676,7 +676,10 @@ class build_ext (Command):
925e6b
         so_ext = get_config_var('SO')
925e6b
         if os.name == 'nt' and self.debug:
925e6b
             return os.path.join(*ext_path) + '_d' + so_ext
925e6b
-        return os.path.join(*ext_path) + so_ext
925e6b
+
925e6b
+        # Similarly, extensions in debug mode are named 'module_d.so', to
925e6b
+        # avoid adding the _d to the SO config variable:
925e6b
+        return os.path.join(*ext_path) + (sys.pydebug and "_d" or "") + so_ext
925e6b
 
925e6b
     def get_export_symbols (self, ext):
925e6b
         """Return the list of symbols that a shared extension has to
925e6b
@@ -761,6 +764,8 @@ class build_ext (Command):
925e6b
                 template = "python%d.%d"
925e6b
                 pythonlib = (template %
925e6b
                              (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
925e6b
+                if sys.pydebug:
925e6b
+                    pythonlib += '_d'
925e6b
                 return ext.libraries + [pythonlib]
925e6b
             else:
925e6b
                 return ext.libraries
925e6b
diff -up Python-2.7.3/Lib/distutils/sysconfig.py.debug-build Python-2.7.3/Lib/distutils/sysconfig.py
925e6b
--- Python-2.7.3/Lib/distutils/sysconfig.py.debug-build	2012-04-18 19:46:21.988499499 -0400
925e6b
+++ Python-2.7.3/Lib/distutils/sysconfig.py	2012-04-18 19:46:22.080498348 -0400
925e6b
@@ -85,7 +85,8 @@ def get_python_inc(plat_specific=0, pref
925e6b
                 # Include is located in the srcdir
925e6b
                 inc_dir = os.path.join(srcdir, "Include")
925e6b
             return inc_dir
925e6b
-        return os.path.join(prefix, "include", "python" + get_python_version())
925e6b
+        return os.path.join(prefix, "include",
925e6b
+                            "python" + get_python_version() + (sys.pydebug and '-debug' or ''))
925e6b
     elif os.name == "nt":
925e6b
         return os.path.join(prefix, "include")
925e6b
     elif os.name == "os2":
925e6b
@@ -250,7 +251,7 @@ def get_makefile_filename():
925e6b
     if python_build:
925e6b
         return os.path.join(project_base, "Makefile")
925e6b
     lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
925e6b
-    return os.path.join(lib_dir, "config", "Makefile")
925e6b
+    return os.path.join(lib_dir, "config" + (sys.pydebug and "-debug" or ""), "Makefile")
925e6b
 
925e6b
 
925e6b
 def parse_config_h(fp, g=None):
925e6b
diff -up Python-2.7.3/Lib/distutils/tests/test_install.py.debug-build Python-2.7.3/Lib/distutils/tests/test_install.py
925e6b
--- Python-2.7.3/Lib/distutils/tests/test_install.py.debug-build	2012-04-18 19:46:21.997499385 -0400
925e6b
+++ Python-2.7.3/Lib/distutils/tests/test_install.py	2012-04-18 19:46:22.080498348 -0400
925e6b
@@ -20,8 +20,9 @@ from distutils.tests import support
925e6b
 
925e6b
 
925e6b
 def _make_ext_name(modname):
925e6b
-    if os.name == 'nt' and sys.executable.endswith('_d.exe'):
925e6b
+    if sys.pydebug:
925e6b
         modname += '_d'
925e6b
+    
925e6b
     return modname + sysconfig.get_config_var('SO')
925e6b
 
925e6b
 
925e6b
diff -up Python-2.7.3/Makefile.pre.in.debug-build Python-2.7.3/Makefile.pre.in
925e6b
--- Python-2.7.3/Makefile.pre.in.debug-build	2012-04-18 19:46:22.073498437 -0400
925e6b
+++ Python-2.7.3/Makefile.pre.in	2012-04-18 19:48:46.336694896 -0400
925e6b
@@ -102,8 +102,8 @@ SCRIPTDIR=	$(prefix)/lib64
925e6b
 # Detailed destination directories
925e6b
 BINLIBDEST=	$(LIBDIR)/python$(VERSION)
925e6b
 LIBDEST=	$(SCRIPTDIR)/python$(VERSION)
925e6b
-INCLUDEPY=	$(INCLUDEDIR)/python$(VERSION)
925e6b
-CONFINCLUDEPY=	$(CONFINCLUDEDIR)/python$(VERSION)
925e6b
+INCLUDEPY=	$(INCLUDEDIR)/python$(VERSION)$(DEBUG_SUFFIX)
925e6b
+CONFINCLUDEPY=	$(CONFINCLUDEDIR)/python$(VERSION)$(DEBUG_SUFFIX)
925e6b
 LIBP=		$(LIBDIR)/python$(VERSION)
925e6b
 
925e6b
 # Symbols used for using shared libraries
925e6b
@@ -117,6 +117,12 @@ DESTSHARED=	$(BINLIBDEST)/lib-dynload
925e6b
 EXE=		@EXEEXT@
925e6b
 BUILDEXE=	@BUILDEXEEXT@
925e6b
 
925e6b
+# DEBUG_EXT is used by ELF files (names and SONAMEs); it will be "_d" for a debug build
925e6b
+# DEBUG_SUFFIX is used by filesystem paths; it will be "-debug" for a debug build
925e6b
+# Both will be empty in an optimized build
925e6b
+DEBUG_EXT=	@DEBUG_EXT@
925e6b
+DEBUG_SUFFIX=	@DEBUG_SUFFIX@
925e6b
+
925e6b
 # Short name and location for Mac OS X Python framework
925e6b
 UNIVERSALSDK=@UNIVERSALSDK@
925e6b
 PYTHONFRAMEWORK=	@PYTHONFRAMEWORK@
925e6b
@@ -180,8 +186,8 @@ LIBOBJDIR=	Python/
925e6b
 LIBOBJS=	@LIBOBJS@
925e6b
 UNICODE_OBJS=   @UNICODE_OBJS@
925e6b
 
925e6b
-PYTHON=		python$(EXE)
925e6b
-BUILDPYTHON=	python$(BUILDEXE)
925e6b
+PYTHON=		python$(DEBUG_SUFFIX)$(EXE)
925e6b
+BUILDPYTHON=	python$(DEBUG_SUFFIX)$(BUILDEXE)
925e6b
 
925e6b
 PYTHON_FOR_BUILD=@PYTHON_FOR_BUILD@
925e6b
 _PYTHON_HOST_PLATFORM=@_PYTHON_HOST_PLATFORM@
925e6b
@@ -413,7 +419,7 @@ sharedmods: $(BUILDPYTHON)
925e6b
 	$(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' \
925e6b
 		$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
925e6b
 
925e6b
-libpython$(VERSION).so: $(LIBRARY_OBJS)
925e6b
+libpython$(VERSION)$(DEBUG_EXT).so: $(LIBRARY_OBJS)
925e6b
 	if test $(INSTSONAME) != $(LDLIBRARY); then \
925e6b
 		$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
925e6b
 		$(LN) -f $(INSTSONAME) $@; \
925e6b
@@ -796,18 +802,18 @@ bininstall:	altbininstall
925e6b
 	then rm -f $(DESTDIR)$(BINDIR)/$(PYTHON); \
925e6b
 	else true; \
925e6b
 	fi
925e6b
-	(cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(EXE) $(PYTHON))
925e6b
-	-rm -f $(DESTDIR)$(BINDIR)/python2$(EXE)
925e6b
-	(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(EXE) python2$(EXE))
925e6b
-	-rm -f $(DESTDIR)$(BINDIR)/python2-config
925e6b
-	(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)-config python2-config)
925e6b
-	-rm -f $(DESTDIR)$(BINDIR)/python-config
925e6b
-	(cd $(DESTDIR)$(BINDIR); $(LN) -s python2-config python-config)
925e6b
+	(cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(DEBUG_SUFFIX)$(EXE) $(PYTHON))
925e6b
+	-rm -f $(DESTDIR)$(BINDIR)/python2$(DEBUG_SUFFIX)$(EXE)
925e6b
+	(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(DEBUG_SUFFIX)$(EXE) python2$(DEBUG_SUFFIX)$(EXE))
925e6b
+	-rm -f $(DESTDIR)$(BINDIR)/python2$(DEBUG_SUFFIX)-config
925e6b
+	(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(DEBUG_SUFFIX)-config python2$(DEBUG_SUFFIX)-config)
925e6b
+	-rm -f $(DESTDIR)$(BINDIR)/python$(DEBUG_SUFFIX)-config
925e6b
+	(cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(DEBUG_SUFFIX)-config python$(DEBUG_SUFFIX)-config)
925e6b
 	-test -d $(DESTDIR)$(LIBPC) || $(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$(LIBPC)
925e6b
-	-rm -f $(DESTDIR)$(LIBPC)/python2.pc
925e6b
-	(cd $(DESTDIR)$(LIBPC); $(LN) -s python-$(VERSION).pc python2.pc)
925e6b
-	-rm -f $(DESTDIR)$(LIBPC)/python.pc
925e6b
-	(cd $(DESTDIR)$(LIBPC); $(LN) -s python2.pc python.pc)
925e6b
+	-rm -f $(DESTDIR)$(LIBPC)/python2$(DEBUG_SUFFIX).pc
925e6b
+	(cd $(DESTDIR)$(LIBPC); $(LN) -s python-$(VERSION)$(DEBUG_SUFFIX).pc python2$(DEBUG_SUFFIX).pc)
925e6b
+	-rm -f $(DESTDIR)$(LIBPC)/python$(DEBUG_SUFFIX).pc
925e6b
+	(cd $(DESTDIR)$(LIBPC); $(LN) -s python2$(DEBUG_SUFFIX).pc python$(DEBUG_SUFFIX).pc)
925e6b
 
925e6b
 # Install the interpreter with $(VERSION) affixed
925e6b
 # This goes into $(exec_prefix)
925e6b
@@ -820,7 +826,7 @@ altbininstall:	$(BUILDPYTHON)
925e6b
 		else	true; \
925e6b
 		fi; \
925e6b
 	done
925e6b
-	$(INSTALL_PROGRAM) $(BUILDPYTHON) $(DESTDIR)$(BINDIR)/python$(VERSION)$(EXE)
925e6b
+	$(INSTALL_PROGRAM) $(BUILDPYTHON) $(DESTDIR)$(BINDIR)/python$(VERSION)$(DEBUG_SUFFIX)$(EXE)
925e6b
 	if test -f $(LDLIBRARY); then \
925e6b
 		if test -n "$(DLLLIBRARY)" ; then \
925e6b
 			$(INSTALL_SHARED) $(DLLLIBRARY) $(DESTDIR)$(BINDIR); \
925e6b
@@ -970,10 +976,11 @@ $(srcdir)/Lib/$(PLATDIR):
925e6b
 	fi; \
925e6b
 	cd $(srcdir)/Lib/$(PLATDIR); $(RUNSHARED) ./regen
925e6b
 
925e6b
-python-config: $(srcdir)/Misc/python-config.in
925e6b
+python$(DEBUG_SUFFIX)-config: $(srcdir)/Misc/python-config.in
925e6b
 	# Substitution happens here, as the completely-expanded BINDIR
925e6b
 	# is not available in configure
925e6b
-	sed -e "s,@EXENAME@,$(BINDIR)/python$(VERSION)$(EXE)," < $(srcdir)/Misc/python-config.in >python-config
925e6b
+	sed -e "s,@EXENAME@,$(BINDIR)/python$(VERSION)$(DEBUG_SUFFIX)$(EXE)," < $(srcdir)/Misc/python-config.in >python$(DEBUG_SUFFIX)-config
925e6b
+
925e6b
 
925e6b
 # Install the include files
925e6b
 INCLDIRSTOMAKE=$(INCLUDEDIR) $(CONFINCLUDEDIR) $(INCLUDEPY) $(CONFINCLUDEPY)
925e6b
@@ -994,13 +1001,13 @@ inclinstall:
925e6b
 	$(INSTALL_DATA) pyconfig.h $(DESTDIR)$(CONFINCLUDEPY)/pyconfig.h
925e6b
 
925e6b
 # Install the library and miscellaneous stuff needed for extending/embedding
925e6b
-# This goes into $(exec_prefix)
925e6b
-LIBPL=		$(LIBP)/config
925e6b
+# This goes into $(exec_prefix)$(DEBUG_SUFFIX)
925e6b
+LIBPL=		$(LIBP)/config$(DEBUG_SUFFIX)
925e6b
 
925e6b
 # pkgconfig directory
925e6b
 LIBPC=		$(LIBDIR)/pkgconfig
925e6b
 
925e6b
-libainstall:	all python-config
925e6b
+libainstall:	all python$(DEBUG_SUFFIX)-config
925e6b
 	@for i in $(LIBDIR) $(LIBP) $(LIBPL) $(LIBPC); \
925e6b
 	do \
925e6b
 		if test ! -d $(DESTDIR)$$i; then \
925e6b
@@ -1016,11 +1023,10 @@ libainstall:	all python-config
925e6b
 	$(INSTALL_DATA) Modules/Setup $(DESTDIR)$(LIBPL)/Setup
925e6b
 	$(INSTALL_DATA) Modules/Setup.local $(DESTDIR)$(LIBPL)/Setup.local
925e6b
 	$(INSTALL_DATA) Modules/Setup.config $(DESTDIR)$(LIBPL)/Setup.config
925e6b
-	$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(VERSION).pc
925e6b
+	$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(VERSION)$(DEBUG_SUFFIX).pc
925e6b
 	$(INSTALL_SCRIPT) $(srcdir)/Modules/makesetup $(DESTDIR)$(LIBPL)/makesetup
925e6b
 	$(INSTALL_SCRIPT) $(srcdir)/install-sh $(DESTDIR)$(LIBPL)/install-sh
925e6b
-	$(INSTALL_SCRIPT) python-config $(DESTDIR)$(BINDIR)/python$(VERSION)-config
925e6b
-	rm python-config
925e6b
+	$(INSTALL_SCRIPT) python$(DEBUG_SUFFIX)-config $(DESTDIR)$(BINDIR)/python$(VERSION)$(DEBUG_SUFFIX)-config
925e6b
 	@if [ -s Modules/python.exp -a \
925e6b
 		"`echo $(MACHDEP) | sed 's/^\(...\).*/\1/'`" = "aix" ]; then \
925e6b
 		echo; echo "Installing support files for building shared extension modules on AIX:"; \
925e6b
diff -up Python-2.7.3/Misc/python-config.in.debug-build Python-2.7.3/Misc/python-config.in
925e6b
--- Python-2.7.3/Misc/python-config.in.debug-build	2012-04-09 19:07:33.000000000 -0400
925e6b
+++ Python-2.7.3/Misc/python-config.in	2012-04-18 19:46:22.082498324 -0400
925e6b
@@ -45,7 +45,7 @@ for opt in opt_flags:
925e6b
 
925e6b
     elif opt in ('--libs', '--ldflags'):
925e6b
         libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
925e6b
-        libs.append('-lpython'+pyver)
925e6b
+        libs.append('-lpython' + pyver + (sys.pydebug and "_d" or ""))
925e6b
         # add the prefix/lib/pythonX.Y/config dir, but only if there is no
925e6b
         # shared library in prefix/lib/.
925e6b
         if opt == '--ldflags':
925e6b
diff -up Python-2.7.3/Modules/makesetup.debug-build Python-2.7.3/Modules/makesetup
925e6b
--- Python-2.7.3/Modules/makesetup.debug-build	2012-04-09 19:07:34.000000000 -0400
925e6b
+++ Python-2.7.3/Modules/makesetup	2012-04-18 19:46:22.083498312 -0400
925e6b
@@ -233,7 +233,7 @@ sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
925e6b
 			*$mod.o*)	base=$mod;;
925e6b
 			*)		base=${mod}module;;
925e6b
 			esac
925e6b
-			file="$srcdir/$base\$(SO)"
925e6b
+			file="$srcdir/$base\$(DEBUG_EXT)\$(SO)"
925e6b
 			case $doconfig in
925e6b
 			no)	SHAREDMODS="$SHAREDMODS $file";;
925e6b
 			esac
925e6b
diff -up Python-2.7.3/Python/dynload_shlib.c.debug-build Python-2.7.3/Python/dynload_shlib.c
925e6b
--- Python-2.7.3/Python/dynload_shlib.c.debug-build	2012-04-09 19:07:35.000000000 -0400
925e6b
+++ Python-2.7.3/Python/dynload_shlib.c	2012-04-18 19:46:22.083498312 -0400
925e6b
@@ -46,11 +46,16 @@ const struct filedescr _PyImport_DynLoad
925e6b
     {"module.exe", "rb", C_EXTENSION},
925e6b
     {"MODULE.EXE", "rb", C_EXTENSION},
925e6b
 #else
925e6b
+#ifdef Py_DEBUG
925e6b
+    {"_d.so", "rb", C_EXTENSION},
925e6b
+    {"module_d.so", "rb", C_EXTENSION},
925e6b
+#else
925e6b
     {".so", "rb", C_EXTENSION},
925e6b
     {"module.so", "rb", C_EXTENSION},
925e6b
-#endif
925e6b
-#endif
925e6b
-#endif
925e6b
+#endif /* Py_DEBUG */
925e6b
+#endif /* __VMS */
925e6b
+#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
925e6b
+#endif /* __CYGWIN__ */
925e6b
     {0, 0}
925e6b
 };
925e6b
 
925e6b
diff -up Python-2.7.3/Python/sysmodule.c.debug-build Python-2.7.3/Python/sysmodule.c
925e6b
--- Python-2.7.3/Python/sysmodule.c.debug-build	2012-04-09 19:07:35.000000000 -0400
925e6b
+++ Python-2.7.3/Python/sysmodule.c	2012-04-18 19:46:22.083498312 -0400
925e6b
@@ -1506,6 +1506,12 @@ _PySys_Init(void)
925e6b
                         PyString_FromString("legacy"));
925e6b
 #endif
925e6b
 
925e6b
+#ifdef Py_DEBUG
925e6b
+	PyDict_SetItemString(sysdict, "pydebug", Py_True);
925e6b
+#else
925e6b
+	PyDict_SetItemString(sysdict, "pydebug", Py_False);
925e6b
+#endif
925e6b
+
925e6b
 #undef SET_SYS_FROM_STRING
925e6b
     if (PyErr_Occurred())
925e6b
         return NULL;