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