PATCH: Fix version check

Mark Mitchell mark at codesourcery.com
Thu May 27 19:03:16 UTC 2004


This patch fixes the Python version-checking code in QMTest, which
itself had features requiring a new version of Python. (!)

--
Mark Mitchell
CodeSourcery, LLC
mark at codesourcery.com

2004-05-27  Mark Mitchell  <mark at codesourcery.com>

	* qm/test/qmtest (check_python_version): Rework to avoid using
	newer Python features.

Index: qm/test/qmtest
===================================================================
RCS file: /home/qm/Repository/qm/qm/test/qmtest,v
retrieving revision 1.4.2.1
diff -c -5 -p -r1.4.2.1 qmtest
*** qm/test/qmtest	26 May 2004 01:21:23 -0000	1.4.2.1
--- qm/test/qmtest	27 May 2004 19:00:55 -0000
***************
*** 14,31 ****
  # For license terms see the file COPYING.
  #
  ########################################################################
  
  ########################################################################
  # Imports
  ########################################################################
  
  import errno
  import gc
  import os
  import os.path
- import sys
  import string
  import traceback
  
  # The Python interpreter will place the directory containing this
  # script in the default path to search for modules.  That is
--- 14,73 ----
  # For license terms see the file COPYING.
  #
  ########################################################################
  
  ########################################################################
+ # Check Python Version
+ ########################################################################
+ 
+ # Before doing anything else, check that the version of Python in use
+ # is sufficiently recent.  All code in this section should be portable
+ # even to old versions of Python.
+ 
+ import sys
+ 
+ def check_python_version():
+     """Check to see if the Python interpreter in use is acceptable.
+ 
+     If the Python interpreter is not sufficiently recent, issue an
+     error message and exit."""
+ 
+     required_python_version = (2, 2)
+ 
+     # Get the version of Python in use.
+     try:
+         actual_version = sys.version_info
+     except:
+         # Older versions of Python do not have "sys.version_info".
+         actual_version = (0, 0, 0, 0)
+ 
+     old = 0
+     for i in range(len(required_python_version)):
+         if required_python_version[i] > actual_version[i]:
+             old = 1
+ 
+     if old:
+         if len(required_python_version) == 2:
+             version = "%d.%d" % required_python_version
+         else:
+             version = "%d.%d.%d" % required_python_version
+         sys.stderr.write(
+             ("QMTest requires Python %s or later.\n"
+              "Set the QM_PYTHON environment variable to an appropriate "
+              "Python interpreter.\n") % version)
+         sys.exit(1)
+ 
+ check_python_version()
+ 
+ ########################################################################
  # Imports
  ########################################################################
  
  import errno
  import gc
  import os
  import os.path
  import string
  import traceback
  
  # The Python interpreter will place the directory containing this
  # script in the default path to search for modules.  That is
*************** if sys.platform != "win32":
*** 94,138 ****
  ########################################################################
  # Functions
  ########################################################################
  
  def print_error_message(message):
      prefix = "qmtest: error: "
      message = qm.structured_text.to_text(str(message),
                                           indent=len(prefix))
      message = prefix + message[len(prefix):]
      sys.stderr.write(message)
  
  
- _required_python_version = (2, 2)
- def check_python_version():
-     """Check to see if the Python interpreter in use is acceptable.
- 
-     If the Python interpreter is not sufficiently recent, issue an
-     error message and exit."""
- 
-     version_str = ".".join([str(num) for num in _required_python_version])
-     message = "Python " + version_str + " or higher is required.\n"
-     message += "Set QM_PYTHON to an appropriate Python interpreter.\n"
-     try:
-         if sys.version_info < _required_python_version:
-             print_error_message(message)
-             sys.exit(1)
-     except AttributeError:
-         print_error_message(message)
-         sys.exit(1)
- 
- 
  def main():
      """Run QMTest.
  
      returns -- The exit code that should be provided to the operating
      system."""
      
-     # Make sure our Python is recent enough.
-     check_python_version()
- 
      # Save the initial signal mask, as early as possible.
      if sys.platform != "win32":
          qm.sigmask.save_mask()
  
      # Parse the command line.
--- 136,164 ----
  ########################################################################
  # Functions
  ########################################################################
  
  def print_error_message(message):
+     """Output an error message.
+ 
+     'message' -- Structured text for the error message to emit.  The
+     messing is emitted to the standard error stream with an
+     identifying prefix."""
+     
      prefix = "qmtest: error: "
      message = qm.structured_text.to_text(str(message),
                                           indent=len(prefix))
      message = prefix + message[len(prefix):]
      sys.stderr.write(message)
  
  
  def main():
      """Run QMTest.
  
      returns -- The exit code that should be provided to the operating
      system."""
      
      # Save the initial signal mask, as early as possible.
      if sys.platform != "win32":
          qm.sigmask.save_mask()
  
      # Parse the command line.



More information about the qmtest mailing list