Drop sys.build_number. Add sys.subversion.
diff --git a/Doc/lib/libsys.tex b/Doc/lib/libsys.tex
index f8f015d..6a82d1b 100644
--- a/Doc/lib/libsys.tex
+++ b/Doc/lib/libsys.tex
@@ -27,10 +27,19 @@
   \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.
+\begin{datadesc}{subversion}
+  A triple (repo, branch, version) representing the Subversion
+  information of the Python interpreter.
+  \var{repo} is the name of the repository, \code{'CPython'}.
+  \var{branch} is the a string of one of the forms \code{'trunk'},
+  \code{'branches/name'} or \code{'tags/name'}.
+  \var{version} is the output of \code{svnversion}, if the
+  interpreter was built from a Subversion checkout; it contains
+  the revision number (range) and possibly a trailing 'M' if
+  there were local modifications. If the tree was exported
+  (or svnversion was not available), it is the revision of
+  \code{Include/patchlevel.h} if the branch is a tag. Otherwise,
+  it is \code{None}.
   \versionadded{2.5}
 \end{datadesc}  
 
diff --git a/Include/patchlevel.h b/Include/patchlevel.h
index 58ba053..2904fb6 100644
--- a/Include/patchlevel.h
+++ b/Include/patchlevel.h
@@ -28,6 +28,9 @@
 /* Version as a string */
 #define PY_VERSION		"2.5a0"
 
+/* Subversion Revision number of this file (not of the repository) */
+#define PY_PATCHLEVEL_REVISION  "$Revision$"
+
 /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2.
    Use this for numeric comparisons, e.g. #if PY_VERSION_HEX >= ... */
 #define PY_VERSION_HEX ((PY_MAJOR_VERSION << 24) | \
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index 6cf33d8..5c78cce 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -108,7 +108,9 @@
 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);
+PyAPI_FUNC(const char *) _Py_svnversion(void);
+PyAPI_FUNC(const char *) Py_SubversionRevision(void);
+PyAPI_FUNC(const char *) Py_SubversionShortBranch(void);
 
 /* Internal -- various one-time initializations */
 PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
diff --git a/Modules/getbuildinfo.c b/Modules/getbuildinfo.c
index 1ff061f..3312a87 100644
--- a/Modules/getbuildinfo.c
+++ b/Modules/getbuildinfo.c
@@ -20,32 +20,25 @@
 #endif
 #endif
 
-static const char revision[] = "$Revision$";
-static const char headurl[] = "$HeadURL$";
-
 const char *
 Py_GetBuildInfo(void)
 {
 	static char buildinfo[50];
-#ifdef SVNVERSION
-	static char svnversion[50] = SVNVERSION;
-#else
-	static char svnversion[50] = "exported";
-#endif
-	if (strcmp(svnversion, "exported") == 0 &&
-	    strstr(headurl, "/tags/") != NULL) {
-		int start = 11;
-		int stop = strlen(revision)-2;
-		strncpy(svnversion, revision+start, stop-start);
-		svnversion[stop-start] = '\0';
-	}
+	char *revision = Py_SubversionRevision();
+	char *sep = revision ? ":" : "";
+	char *branch = Py_SubversionShortBranch();
 	PyOS_snprintf(buildinfo, sizeof(buildinfo),
-		      "%s, %.20s, %.9s", svnversion, DATE, TIME);
+		      "%s%s%s, %.20s, %.9s", branch, sep, revision, 
+		      DATE, TIME);
 	return buildinfo;
 }
 
 const char *
-Py_GetBuildNumber(void)
+_Py_svnversion(void)
 {
-	return "0";
+#ifdef SVNVERSION
+	return SVNVERSION;
+#else
+	return "exported";
+#endif
 }
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index f97a56d..db0d1a6 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -934,6 +934,87 @@
   return fflush (stream) || prev_fail ? EOF : 0;
 }
 
+/* Subversion branch and revision management */
+static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
+static const char headurl[] = "$HeadURL$";
+static int svn_initialized;
+static char patchlevel_revision[50]; /* Just the number */
+static char branch[50];
+static char shortbranch[50];
+static const char *svn_revision;
+
+static void svnversion_init(void)
+{
+	const char *python, *br_start, *br_end, *br_end2, *svnversion;
+	int len, istag;
+
+	if (svn_initialized)
+		return;
+
+	python = strstr(headurl, "/python/");
+	if (!python)
+		Py_FatalError("subversion keywords missing");
+
+	br_start = python + 8;
+	br_end = strchr(br_start, '/');
+	/* Works even for trunk, 
+	   as we are in trunk/Python/sysmodule.c */
+	br_end2 = strchr(br_end+1, '/');
+
+	istag = strncmp(br_start, "tags", 4) == 0;
+	if (strncmp(br_start, "trunk", 5) == 0) {
+		strcpy(branch, "trunk");
+		strcpy(shortbranch, "trunk");
+
+	} 
+	else if (istag || strncmp(br_start, "branches", 8) == 0) {
+		len = br_end2 - br_start;
+		strncpy(branch, br_start, len);
+		branch[len] = '\0';
+
+		len = br_end2 - (br_end + 1);
+		strncpy(shortbranch, br_end + 1, len);
+		shortbranch[len] = '\0';
+	} 
+	else {
+		Py_FatalError("bad HeadURL");
+		return;
+	}
+
+
+	svnversion = _Py_svnversion();
+	if (strcmp(svnversion, "exported") != 0)
+		svn_revision = svnversion;
+	else if (istag) {
+		len = strlen(_patchlevel_revision);
+		strncpy(patchlevel_revision, _patchlevel_revision + 11,
+			len - 13);
+		patchlevel_revision[len - 13] = '\0';
+		svn_revision = patchlevel_revision;
+	}
+	else
+		svn_revision = "";
+	
+	svn_initialized = 1;
+}
+
+/* Return svnversion output if available.
+   Else return Revision of patchlevel.h if on branch.
+   Else return empty string */
+const char*
+Py_SubversionRevision()
+{
+	svnversion_init();
+	return svn_revision;
+}
+
+const char*
+Py_SubversionShortBranch()
+{
+	svnversion_init();
+	return shortbranch;
+}
+
 PyObject *
 _PySys_Init(void)
 {
@@ -1003,8 +1084,9 @@
 	PyDict_SetItemString(sysdict, "hexversion",
 			     v = PyInt_FromLong(PY_VERSION_HEX));
 	Py_XDECREF(v);
-	PyDict_SetItemString(sysdict, "build_number",
-			     v = PyString_FromString(Py_GetBuildNumber()));
+	svnversion_init();
+	v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);
+	PyDict_SetItemString(sysdict, "subversion", v);
 	Py_XDECREF(v);
 	/*
 	 * These release level checks are mutually exclusive and cover