[pygobject] minor py3k fixups for python modules



commit a499b2f0d622b671bd154544f66b73f1278e66ed
Author: John (J5) Palmieri <johnp redhat com>
Date:   Thu Sep 9 07:56:44 2010 -0400

    minor py3k fixups for python modules
    
    * add _basestring and _bytes and _callable wrappers
    * use items instead of iteritems and range instead of xrange
    
    fix py3k modules
    
    https://bugzilla.gnome.org/show_bug.cgi?id=615872

 gi/overrides/Gtk.py |   24 ++++++++++++++++--------
 glib/option.py      |   16 ++++++++++++----
 2 files changed, 28 insertions(+), 12 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index fc7b28c..1f6901c 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -19,12 +19,20 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 # USA
 
+import sys
 import gobject
 from gi.repository import Gdk
 from gi.repository import GObject
 from ..types import override
 from ..importer import modules
 
+if sys.version_info >= (3, 0):
+    _basestring = str
+    _callable = lambda c: hasattr(c, '__call__')
+else:
+    _basestring = basestring
+    _callable = callable
+
 Gtk = modules['Gtk'].introspection_module
 __all__ = []
 
@@ -184,7 +192,7 @@ __all__.append('ActionGroup')
 
 class UIManager(Gtk.UIManager):
     def add_ui_from_string(self, buffer):
-        if not isinstance(buffer, basestring):
+        if not isinstance(buffer, _basestring):
             raise TypeError('buffer must be a string')
 
         length = len(buffer)
@@ -207,7 +215,7 @@ class Builder(Gtk.Builder):
             if handler is None:
                 raise AttributeError('Handler %s not found' % handler_name)
 
-            if not callable(handler):
+            if not _callable(handler):
                 raise TypeError('Handler %s is not a method or function' % handler_name)
 
             after = flags or GObject.ConnectFlags.AFTER
@@ -226,7 +234,7 @@ class Builder(Gtk.Builder):
                                   obj_or_map);
 
     def add_from_string(self, buffer):
-        if not isinstance(buffer, basestring):
+        if not isinstance(buffer, _basestring):
             raise TypeError('buffer must be a string')
 
         length = len(buffer)
@@ -234,7 +242,7 @@ class Builder(Gtk.Builder):
         return Gtk.Builder.add_from_string(self, buffer, length)
 
     def add_objects_from_string(self, buffer, object_ids):
-        if not isinstance(buffer, basestring):
+        if not isinstance(buffer, _basestring):
             raise TypeError('buffer must be a string')
 
         length = len(buffer)
@@ -330,14 +338,14 @@ class TextBuffer(Gtk.TextBuffer):
         return tag
 
     def insert(self, iter, text):
-        if not isinstance(text , basestring):
+        if not isinstance(text , _basestring):
             raise TypeError('text must be a string, not %s' % type(text))
 
         length = len(text)
         Gtk.TextBuffer.insert(self, iter, text, length)
 
     def insert_at_cursor(self, text):
-        if not isinstance(text , basestring):
+        if not isinstance(text , _basestring):
             raise TypeError('text must be a string, not %s' % type(text))
 
         length = len(text)
@@ -388,7 +396,7 @@ class TreeStore(Gtk.TreeStore, TreeModel):
         if len(row) != n_columns:
             raise ValueError('row sequence has the incorrect number of elements')
 
-        for i in xrange(n_columns):
+        for i in range(n_columns):
             if row[i] is not None:
                 self.set_value(treeiter, i, row[i])
 
@@ -405,7 +413,7 @@ class TreeViewColumn(Gtk.TreeViewColumn):
         if cell_renderer:
             self.pack_start(cell_renderer, True)
 
-        for (name, value) in attributes.iteritems():
+        for (name, value) in attributes.items():
             self.add_attribute(cell_renderer, name, value)
 
 TreeViewColumn = override(TreeViewColumn)
diff --git a/glib/option.py b/glib/option.py
index 33c2dd5..ff38b57 100644
--- a/glib/option.py
+++ b/glib/option.py
@@ -34,6 +34,13 @@ import optparse
 from optparse import OptParseError, OptionError, OptionValueError, \
                      BadOptionError, OptionConflictError
 
+if sys.version_info >= (3, 0):
+    _basestring = str
+    _bytes = lambda s: s.encode()
+else:
+    _basestring = basestring
+    _bytes = str
+
 import glib
 _glib = sys.modules['glib._glib']
 
@@ -125,10 +132,10 @@ class Option(optparse.Option):
             flags |= _glib.OPTION_FLAG_FILENAME
 
         for (long_name, short_name) in zip(self._long_opts, self._short_opts):
-            yield (long_name[2:], short_name[1], flags, self.help, self.metavar)
+            yield (long_name[2:], _bytes(short_name[1]), flags, self.help, self.metavar)
 
         for long_name in self._long_opts[len(self._short_opts):]:
-            yield (long_name[2:], '\0', flags, self.help, self.metavar)
+            yield (long_name[2:], _bytes('\0'), flags, self.help, self.metavar)
 
 class OptionGroup(optparse.OptionGroup):
     """A group of command line options.
@@ -199,6 +206,7 @@ class OptionGroup(optparse.OptionGroup):
         entries = []
         for option in self.option_list:
             entries.extend(option._to_goptionentries())
+ 
         group.add_entries(entries)
 
         return group
@@ -214,7 +222,7 @@ class OptionGroup(optparse.OptionGroup):
     def set_values_to_defaults(self):
         for option in self.option_list:
             default = self.defaults.get(option.dest)
-            if isinstance(default, basestring):
+            if isinstance(default, _basestring):
                 opt_str = option.get_opt_string()
                 self.defaults[option.dest] = option.check_value(
                     opt_str, default)
@@ -292,7 +300,7 @@ class OptionParser(optparse.OptionParser):
         return context
 
     def add_option_group(self, *args, **kwargs):
-        if isinstance(args[0], basestring):
+        if isinstance(args[0], _basestring):
             optparse.OptionParser.add_option_group(self,
                 OptionGroup(self, *args, **kwargs))
             return



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]