def safe_text_dupfile(f, mode, default_encoding='UTF8'):
 encoding = getattr(f, 'encoding', None)
 try:
  fd = f.fileno()
 except Exception:
  if (('b' not in getattr(f, 'mode', '')) and hasattr(f, 'encoding')):
   return f
 else:
  newfd = os.dup(fd)
  if ('b' not in mode):
   mode += 'b'
  f = os.fdopen(newfd, mode, 0)
 return EncodedFile(f, (encoding or default_encoding))

def safe_text_dupfile ( f , mode , default_encoding = 'UTF8' ) : encoding = getattr ( f , 'encoding' , None ) try : fd = f . fileno ( ) except Exception : if ( ( 'b' not in getattr ( f , 'mode' , '' ) ) and hasattr ( f , 'encoding' ) ) : return f else : newfd = os . dup ( fd ) if ( 'b' not in mode ) : mode += 'b' f = os . fdopen ( newfd , mode , 0 ) return EncodedFile ( f , ( encoding or default_encoding ) )
return a open text file object thats a duplicate of f on the fd-level if possible .