# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2017 GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import sys

try:
    import __builtin__
except ImportError:
    # Python 3
    import builtins as __builtin__


def print(*args, **kwargs):
    conv_str = ()
    for s in args:
        conv_str = s.encode('utf-8').decode(sys.stdout.encoding, 'ignore')
    return __builtin__.print(conv_str, **kwargs)

@@ -789,16 +789,25 @@
 ion%0A
+from sys 
 import s
 ys%0A%0A
@@ -802,18 +802,21 @@
 import s
-ys
+tdout
 %0A%0Atry:%0A 
@@ -945,21 +945,246 @@
     
-conv_str = ()
+ret_str = ()%0A    # when stdout is redirected to a file, python 2 uses ascii for the writer;%0A    # python 3 uses what is configured in the system (i.e. 'utf-8')%0A    str_encoding = stdout.encoding if stdout.encoding is not None else 'ascii'
 %0A   
@@ -1207,20 +1207,19 @@
         
-conv
+ret
 _str = s
@@ -1247,18 +1247,11 @@
 de(s
-ys.stdout.
+tr_
 enco
@@ -1266,16 +1266,17 @@
 gnore')%0A
+%0A
     retu
@@ -1300,12 +1300,11 @@
 int(
-conv
+ret
 _str

Fix out redirection in python2