]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Fixed a bug on Python 3 when writing to stdout
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 29 Jul 2013 09:26:19 +0000 (11:26 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 29 Jul 2013 09:26:19 +0000 (11:26 +0200)
CHANGES
babel/messages/frontend.py

diff --git a/CHANGES b/CHANGES
index 66202f2dd55f546249d0ce26412974ba9eb7fc85..6fe47511ec3088ca841eb162cb9e5e1519d313f1 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -15,6 +15,8 @@ Version 1.3
   This primarily makes ``zh_CN`` work again which was broken
   due to how it was defined in the likely subtags combined with
   our broken resolving.  This fixes #37.
+- Fixed a bug that caused pybabel to break when writing to stdout
+  on Python 3.
 
 Version 1.2
 -----------
index 3cec787dcfd9ea1d7bd5a65eab396119abafdd34..144bc98a108080149bdd5fce35f051649cbec42b 100755 (executable)
@@ -35,7 +35,7 @@ from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
 from babel.messages.mofile import write_mo
 from babel.messages.pofile import read_po, write_po
 from babel.util import odict, LOCALTZ
-from babel._compat import string_types, BytesIO
+from babel._compat import string_types, BytesIO, PY2
 
 
 class compile_catalog(Command):
@@ -826,7 +826,7 @@ class CommandLineInterface(object):
                           help='path to the output POT file')
         parser.add_option('-w', '--width', dest='width', type='int',
                           help="set output line width (default 76)")
-        parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true',
+        parser.add_option('--no-wrap', dest='no_wrap', action='store_true',
                           help='do not break long message lines, longer than '
                                'the output line width, into several lines')
         parser.add_option('--sort-output', dest='sort_output',
@@ -921,16 +921,25 @@ class CommandLineInterface(object):
                 catalog.add(message, None, [(filepath, lineno)],
                             auto_comments=comments, context=context)
 
+        catalog_charset = catalog.charset
         if options.output not in (None, '-'):
             self.log.info('writing PO template file to %s' % options.output)
             outfile = open(options.output, 'wb')
             close_output = True
         else:
             outfile = sys.stdout
+
+            # This is a bit of a hack on Python 3.  stdout is a text stream so
+            # we need to find the underlying file when we write the PO.  In
+            # later versions of Babel we want the write_po function to accept
+            # text or binary streams and automatically adjust the encoding.
+            if not PY2 and hasattr(outfile, 'buffer'):
+                catalog.charset = outfile.encoding
+                outfile = outfile.buffer.raw
+
             close_output = False
 
         try:
-            print(outfile)
             write_po(outfile, catalog, width=options.width,
                      no_location=options.no_location,
                      omit_header=options.omit_header,
@@ -939,6 +948,7 @@ class CommandLineInterface(object):
         finally:
             if close_output:
                 outfile.close()
+            catalog.charset = catalog_charset
 
     def init(self, argv):
         """Subcommand for creating new message catalogs from a template.