def prints(*args, **kwargs):
 file = kwargs.get('file', sys.stdout)
 sep = kwargs.get('sep', ' ')
 end = kwargs.get('end', '\n')
 enc = preferred_encoding
 safe_encode = kwargs.get('safe_encode', False)
 if ('CALIBRE_WORKER' in os.environ):
  enc = 'utf-8'
 for (i, arg) in enumerate(args):
  if isinstance(arg, unicode):
   try:
    arg = arg.encode(enc)
   except UnicodeEncodeError:
    try:
     arg = arg.encode('utf-8')
    except:
     if (not safe_encode):
      raise
     arg = repr(arg)
  if (not isinstance(arg, str)):
   try:
    arg = str(arg)
   except ValueError:
    arg = unicode(arg)
   if isinstance(arg, unicode):
    try:
     arg = arg.encode(enc)
    except UnicodeEncodeError:
     try:
      arg = arg.encode('utf-8')
     except:
      if (not safe_encode):
       raise
      arg = repr(arg)
  try:
   file.write(arg)
  except:
   import repr as reprlib
   file.write(reprlib.repr(arg))
  if (i != (len(args) - 1)):
   file.write(bytes(sep))
 file.write(bytes(end))

def prints ( * args , ** kwargs ) : file = kwargs . get ( 'file' , sys . stdout ) sep = kwargs . get ( 'sep' , ' ' ) end = kwargs . get ( 'end' , '\n' ) enc = preferred_encoding safe_encode = kwargs . get ( 'safe_encode' , False ) if ( 'CALIBRE_WORKER' in os . environ ) : enc = 'utf-8' for ( i , arg ) in enumerate ( args ) : if isinstance ( arg , unicode ) : try : arg = arg . encode ( enc ) except UnicodeEncodeError : try : arg = arg . encode ( 'utf-8' ) except : if ( not safe_encode ) : raise arg = repr ( arg ) if ( not isinstance ( arg , str ) ) : try : arg = str ( arg ) except ValueError : arg = unicode ( arg ) if isinstance ( arg , unicode ) : try : arg = arg . encode ( enc ) except UnicodeEncodeError : try : arg = arg . encode ( 'utf-8' ) except : if ( not safe_encode ) : raise arg = repr ( arg ) try : file . write ( arg ) except : import repr as reprlib file . write ( reprlib . repr ( arg ) ) if ( i != ( len ( args ) - 1 ) ) : file . write ( bytes ( sep ) ) file . write ( bytes ( end ) )
print unicode arguments safely by encoding them to preferred_encoding has the same signature as the print function from python 3 .