Expose Subversion revision number (calculated via "svnversion .") to Python.
Add C API function Py_GetBuildNumber(), add it to the interactive prompt
banner (i.e. Py_GetBuildInfo()), and add it as the sys.build_number
attribute.  The build number is a string instead of an int because it may
contain a trailing 'M' if there are local modifications.
diff --git a/Doc/api/init.tex b/Doc/api/init.tex
index 51b925f..67f74a1 100644
--- a/Doc/api/init.tex
+++ b/Doc/api/init.tex
@@ -272,6 +272,12 @@
   \withsubitem{(in module sys)}{\ttindex{version}}
 \end{cfuncdesc}
 
+\begin{cfuncdesc}{const char*}{Py_GetBuildNumber}{}
+  Return a string representing the Subversion revision that this Python
+  executable was built from.  This number is a string because it may contain a
+  trailing 'M' if Python was built from a mixed revision source tree.
+\end{cfuncdesc}
+
 \begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
   Return the platform identifier for the current platform.  On \UNIX,
   this is formed from the ``official'' name of the operating system,
diff --git a/Doc/lib/libsys.tex b/Doc/lib/libsys.tex
index 4efe5f6..7ffa35d 100644
--- a/Doc/lib/libsys.tex
+++ b/Doc/lib/libsys.tex
@@ -27,6 +27,12 @@
   \versionadded{2.0}
 \end{datadesc}
 
+\begin{datadesc}{build_number}
+  A string representing the Subversion revision that this Python executable
+  was built from.  This number is a string because it may contain a trailing
+  'M' if Python was built from a mixed revision source tree.
+\end{datadesc}  
+
 \begin{datadesc}{builtin_module_names}
   A tuple of strings giving the names of all modules that are compiled
   into this Python interpreter.  (This information is not available in
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index e309078..6cf33d8 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -108,6 +108,7 @@
 PyAPI_FUNC(const char *) Py_GetCopyright(void);
 PyAPI_FUNC(const char *) Py_GetCompiler(void);
 PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
+PyAPI_FUNC(const char *) Py_GetBuildNumber(void);
 
 /* Internal -- various one-time initializations */
 PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
diff --git a/Makefile.pre.in b/Makefile.pre.in
index ab01deb..0ce2afa 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -349,7 +349,9 @@
 		$(SIGNAL_OBJS) \
 		$(MODOBJS) \
 		$(srcdir)/Modules/getbuildinfo.c
-	if test -f buildno; then \
+	if test -d .svn; then \
+		svnversion . >buildno; \
+	elif test -f buildno; then \
 		expr `cat buildno` + 1 >buildno1; \
 		mv -f buildno1 buildno; \
 	else echo 1 >buildno; fi
@@ -444,7 +446,7 @@
 # Special rules for object files
 
 Modules/getbuildinfo.o: $(srcdir)/Modules/getbuildinfo.c buildno
-	$(CC) -c $(PY_CFLAGS) -DBUILD=`cat buildno` -o $@ $(srcdir)/Modules/getbuildinfo.c
+	$(CC) -c $(PY_CFLAGS) -DBUILD=\"`cat buildno`\" -o $@ $(srcdir)/Modules/getbuildinfo.c
 
 Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
 	$(CC) -c $(PY_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
diff --git a/Misc/NEWS b/Misc/NEWS
index bd00491..d4aaa74 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@
 Core and builtins
 -----------------
 
+- Patch #1382163: Expose Subversion revision number to Python.  New C API
+  function Py_GetBuildNumber().  New attribute sys.build_number.  Build number
+  is now displayed in interactive prompt banner.
+
 - Implementation of PEP 341 - Unification of try/except and try/finally.
   "except" clauses can now be written together with a "finally" clause in
   one try statement instead of two nested ones.  Patch #1355913.
diff --git a/Modules/getbuildinfo.c b/Modules/getbuildinfo.c
index b541bb6..8b1ca22 100644
--- a/Modules/getbuildinfo.c
+++ b/Modules/getbuildinfo.c
@@ -21,7 +21,7 @@
 #endif
 
 #ifndef BUILD
-#define BUILD 0
+#define BUILD "0"
 #endif
 
 const char *
@@ -29,6 +29,12 @@
 {
 	static char buildinfo[50];
 	PyOS_snprintf(buildinfo, sizeof(buildinfo),
-		      "#%d, %.20s, %.9s", BUILD, DATE, TIME);
+		      "%s, %.20s, %.9s", BUILD, DATE, TIME);
 	return buildinfo;
 }
+
+const char *
+Py_GetBuildNumber(void)
+{
+	return BUILD;
+}
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index d9f1337..f97a56d 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1003,6 +1003,9 @@
 	PyDict_SetItemString(sysdict, "hexversion",
 			     v = PyInt_FromLong(PY_VERSION_HEX));
 	Py_XDECREF(v);
+	PyDict_SetItemString(sysdict, "build_number",
+			     v = PyString_FromString(Py_GetBuildNumber()));
+	Py_XDECREF(v);
 	/*
 	 * These release level checks are mutually exclusive and cover
 	 * the field, so don't get too fancy with the pre-processor!