@@ -934,6 +934,87 @@ _check_and_flush (FILE *stream)
934934 return fflush (stream ) || prev_fail ? EOF : 0 ;
935935}
936936
937+ /* Subversion branch and revision management */
938+ static const char _patchlevel_revision [] = PY_PATCHLEVEL_REVISION ;
939+ static const char headurl [] = "$HeadURL$" ;
940+ static int svn_initialized ;
941+ static char patchlevel_revision [50 ]; /* Just the number */
942+ static char branch [50 ];
943+ static char shortbranch [50 ];
944+ static const char * svn_revision ;
945+
946+ static void svnversion_init (void )
947+ {
948+ const char * python , * br_start , * br_end , * br_end2 , * svnversion ;
949+ int len , istag ;
950+
951+ if (svn_initialized )
952+ return ;
953+
954+ python = strstr (headurl , "/python/" );
955+ if (!python )
956+ Py_FatalError ("subversion keywords missing" );
957+
958+ br_start = python + 8 ;
959+ br_end = strchr (br_start , '/' );
960+ /* Works even for trunk,
961+ as we are in trunk/Python/sysmodule.c */
962+ br_end2 = strchr (br_end + 1 , '/' );
963+
964+ istag = strncmp (br_start , "tags" , 4 ) == 0 ;
965+ if (strncmp (br_start , "trunk" , 5 ) == 0 ) {
966+ strcpy (branch , "trunk" );
967+ strcpy (shortbranch , "trunk" );
968+
969+ }
970+ else if (istag || strncmp (br_start , "branches" , 8 ) == 0 ) {
971+ len = br_end2 - br_start ;
972+ strncpy (branch , br_start , len );
973+ branch [len ] = '\0' ;
974+
975+ len = br_end2 - (br_end + 1 );
976+ strncpy (shortbranch , br_end + 1 , len );
977+ shortbranch [len ] = '\0' ;
978+ }
979+ else {
980+ Py_FatalError ("bad HeadURL" );
981+ return ;
982+ }
983+
984+
985+ svnversion = _Py_svnversion ();
986+ if (strcmp (svnversion , "exported" ) != 0 )
987+ svn_revision = svnversion ;
988+ else if (istag ) {
989+ len = strlen (_patchlevel_revision );
990+ strncpy (patchlevel_revision , _patchlevel_revision + 11 ,
991+ len - 13 );
992+ patchlevel_revision [len - 13 ] = '\0' ;
993+ svn_revision = patchlevel_revision ;
994+ }
995+ else
996+ svn_revision = "" ;
997+
998+ svn_initialized = 1 ;
999+ }
1000+
1001+ /* Return svnversion output if available.
1002+ Else return Revision of patchlevel.h if on branch.
1003+ Else return empty string */
1004+ const char *
1005+ Py_SubversionRevision ()
1006+ {
1007+ svnversion_init ();
1008+ return svn_revision ;
1009+ }
1010+
1011+ const char *
1012+ Py_SubversionShortBranch ()
1013+ {
1014+ svnversion_init ();
1015+ return shortbranch ;
1016+ }
1017+
9371018PyObject *
9381019_PySys_Init (void )
9391020{
@@ -1003,8 +1084,9 @@ _PySys_Init(void)
10031084 PyDict_SetItemString (sysdict , "hexversion" ,
10041085 v = PyInt_FromLong (PY_VERSION_HEX ));
10051086 Py_XDECREF (v );
1006- PyDict_SetItemString (sysdict , "build_number" ,
1007- v = PyString_FromString (Py_GetBuildNumber ()));
1087+ svnversion_init ();
1088+ v = Py_BuildValue ("(ssz)" , "CPython" , branch , svn_revision );
1089+ PyDict_SetItemString (sysdict , "subversion" , v );
10081090 Py_XDECREF (v );
10091091 /*
10101092 * These release level checks are mutually exclusive and cover
0 commit comments