------------------------------------------------------------
revno: 6680
committer: Barry Warsaw <[email protected]>
branch nick: 3.0
timestamp: Wed 2009-02-04 23:35:49 -0500
message:
  Work around the limitation in Python 2.6's OptionParser class's refusal to
  accept unicode options strings in its add_option() method.
modified:
  src/mailman/options.py

=== modified file 'src/mailman/options.py'
--- src/mailman/options.py      2009-01-17 02:04:21 +0000
+++ src/mailman/options.py      2009-02-05 04:35:49 +0000
@@ -65,6 +65,29 @@
     TYPE_CHECKER['yesno'] = check_yesno
 
 
+class SafeOptionParser(OptionParser):
+    """A unicode-compatible `OptionParser`.
+
+    Python's standard option parser does not accept unicode options.  Rather
+    than try to fix that, this class wraps the add_option() method and saves
+    having to wrap the options in str() calls.
+    """
+    def add_option(self, *args, **kwargs):
+        # Check to see if the first or first two options are unicodes and turn
+        # them into 8-bit strings before calling the superclass's method.
+        if len(args) == 0:
+            return OptionParser.add_option(self, *args, **kwargs)
+        old_args = list(args)
+        new_args = []
+        arg0 = old_args.pop(0)
+        new_args.append(str(arg0))
+        if len(old_args) > 0:
+            arg1 = old_args.pop(0)
+            new_args.append(str(arg1))
+        new_args.extend(old_args)
+        return OptionParser.add_option(self, *new_args, **kwargs)
+
+
 
 class Options:
     """Common argument parser."""
@@ -73,9 +96,10 @@
     usage = None
 
     def __init__(self):
-        self.parser = OptionParser(version=MAILMAN_VERSION,
-                                   option_class=MailmanOption,
-                                   usage=self.usage)
+        self.parser = SafeOptionParser(
+            version=MAILMAN_VERSION,
+            option_class=MailmanOption,
+            usage=self.usage)
         self.add_common_options()
         self.add_options()
         options, arguments = self.parser.parse_args()
@@ -97,7 +121,7 @@
         """Add options common to all scripts."""
         # Python requires str types here.
         self.parser.add_option(
-            str('-C'), str('--config'),
+            '-C', '--config',
             help=_('Alternative configuration file to use'))
 
     def initialize(self, propagate_logs=None):



--
Primary development focus
https://code.launchpad.net/~mailman-coders/mailman/3.0

Your team Mailman Checkins is subscribed to branch lp:mailman.
To unsubscribe from this branch go to 
https://code.launchpad.net/~mailman-coders/mailman/3.0/+edit-subscription.
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to