def safe_text_dupfile(f, mode, default_encoding="<STR_LIT>"):
encoding = getattr(f, "<STR_LIT>", None)<EOL>try:<EOL><INDENT>fd = f.fileno()<EOL><DEDENT>except Exception:<EOL><INDENT>if "<STR_LIT:b>" not in getattr(f, "<STR_LIT>", "<STR_LIT>") and hasattr(f, "<STR_LIT>"):<EOL><INDENT>return f<EOL><DEDENT><DEDENT>else:<EOL><INDENT>newfd = os.dup(fd)<EOL>if "<STR_LIT:b>" not in mode:<EOL><INDENT>mode += "<STR_LIT:b>"<EOL><DEDENT>f = os.fdopen(newfd, mode, <NUM_LIT:0>)  <EOL><DEDENT>return EncodedFile(f, encoding or default_encoding)<EOL>
return an open text file object that's a duplicate of f on the
        FD-level if possible.