diff options
author | Simon Feltman <sfeltman@src.gnome.org> | 2014-04-28 16:21:35 -0700 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2015-09-29 23:12:58 -0400 |
commit | d59b3827d2bb62c1ed4db8030ed9e8e753b7f52d (patch) | |
tree | 2ed3614ba3b281e4fd253d9e56897f49c5a9b3bf /giscanner/annotationmain.py | |
parent | 750060dc0211cfb5786ba39da7283e5885eac7ad (diff) | |
download | gobject-introspection-d59b3827d2bb62c1ed4db8030ed9e8e753b7f52d.tar.gz |
giscanner: Use unicode literals in all Python files
Add unicode_literals future import which turns any string literal
into a unicode string. Return unicode strings from the Python C extension
module. Force writing of annotations (g-ir-annotation-tool) to output utf8
encoded data to stdout.
This is an initial pass at following the "unicode sandwich"
model of programming (http://nedbatchelder.com/text/unipain.html)
needed for supporting Python 3.
https://bugzilla.gnome.org/show_bug.cgi?id=679438
Diffstat (limited to 'giscanner/annotationmain.py')
-rw-r--r-- | giscanner/annotationmain.py | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/giscanner/annotationmain.py b/giscanner/annotationmain.py index 190269e2..b82ff818 100644 --- a/giscanner/annotationmain.py +++ b/giscanner/annotationmain.py @@ -21,8 +21,12 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +from __future__ import unicode_literals +import sys import optparse +import codecs +from contextlib import contextmanager from giscanner import message from giscanner.annotationparser import GtkDocCommentBlockParser, GtkDocCommentBlockWriter @@ -31,6 +35,24 @@ from giscanner.scannermain import (get_preprocessor_option_group, process_packages) +@contextmanager +def encode_stdout(encoding): + """Force stdout into a specific encoding.""" + # Python 2 does not encode stdout writes so wrap it with 'encoding' encoded writer. + # Python 3 uses a io.TextIOBase wrapped stdout with the system default encoding. + # Re-wrap the underlying buffer with a new writer with the given 'encoding'. + # See: https://docs.python.org/3/library/sys.html#sys.stdout + old_stdout = sys.stdout + if sys.version_info.major < 3: + binary_stdout = sys.stdout + else: + binary_stdout = sys.stdout.buffer + + sys.stdout = codecs.getwriter(encoding)(binary_stdout) + yield + sys.stdout = old_stdout + + def annotation_main(args): parser = optparse.OptionParser('%prog [options] sources') @@ -65,16 +87,18 @@ def annotation_main(args): parser = GtkDocCommentBlockParser() writer = GtkDocCommentBlockWriter(indent=False) blocks = parser.parse_comment_blocks(ss.get_comments()) - print('/' + ('*' * 60) + '/') - print('/* THIS FILE IS GENERATED DO NOT EDIT */') - print('/' + ('*' * 60) + '/') - print('') - for block in sorted(blocks.values()): - print(writer.write(block)) + + with encode_stdout('utf-8'): + print('/' + ('*' * 60) + '/') + print('/* THIS FILE IS GENERATED DO NOT EDIT */') + print('/' + ('*' * 60) + '/') + print('') + for block in sorted(blocks.values()): + print(writer.write(block)) + print('') print('') - print('') - print('/' + ('*' * 60) + '/') - print('/* THIS FILE IS GENERATED DO NOT EDIT */') - print('/' + ('*' * 60) + '/') + print('/' + ('*' * 60) + '/') + print('/* THIS FILE IS GENERATED DO NOT EDIT */') + print('/' + ('*' * 60) + '/') return 0 |