nodes: 0, module; 1, function_definition; 2, function_name:_decode_filename_to_unicode; 3, parameters; 4, block; 5, identifier:f; 6, expression_statement; 7, expression_statement; 8, if_statement; 9, try_statement; 10, string; 11, call; 12, call; 13, block; 14, block; 15, except_clause; 16, string_content:Get bytestring filename and return unicode.
      First, try to decode from default file system encoding
      If that fails, use ``chardet`` module to guess encoding.
      As a last resort, try to decode as utf-8.
  
      If the argument already is unicode, return as is; 17, attribute; 18, argument_list; 19, identifier:isinstance; 20, argument_list; 21, return_statement; 22, return_statement; 23, identifier:UnicodeDecodeError; 24, block; 25, identifier:log; 26, identifier:debug; 27, string; 28, call; 29, identifier:f; 30, identifier:unicode; 31, identifier:f; 32, call; 33, expression_statement; 34, expression_statement; 35, if_statement; 36, expression_statement; 37, expression_statement; 38, try_statement; 39, expression_statement; 40, try_statement; 41, string_content:_decode_filename_to_unicode(%s); 42, identifier:repr; 43, argument_list; 44, attribute; 45, argument_list; 46, assignment; 47, call; 48, comparison_operator:charguess['encoding'] is not None; 49, block; 50, call; 51, call; 52, block; 53, except_clause; 54, call; 55, block; 56, except_clause; 57, identifier:f; 58, identifier:f; 59, identifier:decode; 60, call; 61, identifier:charguess; 62, call; 63, attribute; 64, argument_list; 65, subscript; 66, None; 67, try_statement; 68, attribute; 69, argument_list; 70, attribute; 71, argument_list; 72, return_statement; 73, identifier:UnicodeDecodeError; 74, block; 75, attribute; 76, argument_list; 77, return_statement; 78, identifier:UnicodeDecodeError; 79, block; 80, attribute; 81, argument_list; 82, attribute; 83, argument_list; 84, identifier:log; 85, identifier:debug; 86, string:"chardet filename: %r -> %r"; 87, identifier:f; 88, identifier:charguess; 89, identifier:charguess; 90, string; 91, block; 92, except_clause; 93, identifier:log; 94, identifier:warning; 95, string; 96, identifier:f; 97, identifier:charguess; 98, identifier:log; 99, identifier:debug; 100, string; 101, identifier:f; 102, call; 103, pass_statement; 104, identifier:log; 105, identifier:debug; 106, string; 107, identifier:f; 108, call; 109, expression_statement; 110, return_statement; 111, identifier:sys; 112, identifier:getfilesystemencoding; 113, identifier:chardet; 114, identifier:detect; 115, identifier:f; 116, string_content:encoding; 117, return_statement; 118, identifier:UnicodeDecodeError; 119, block; 120, string_content:Cannot understand decoding of this filename: %r (guessed %r, but was wrong); 121, string_content:Trying utf-8 to decode %r; 122, attribute; 123, argument_list; 124, string_content:Trying latin1 to decode %r; 125, attribute; 126, argument_list; 127, call; 128, call; 129, call; 130, pass_statement; 131, identifier:f; 132, identifier:decode; 133, string; 134, identifier:f; 135, identifier:decode; 136, string; 137, attribute; 138, argument_list; 139, attribute; 140, argument_list; 141, attribute; 142, argument_list; 143, string_content:utf-8; 144, string_content:latin1; 145, identifier:log; 146, identifier:warning; 147, string; 148, identifier:f; 149, identifier:f; 150, identifier:decode; 151, string; 152, keyword_argument; 153, identifier:f; 154, identifier:decode; 155, subscript; 156, string_content:Exhausted all options. Decoding %r to safe ascii; 157, string_content:ascii; 158, identifier:errors; 159, string; 160, identifier:charguess; 161, string; 162, string_content:ignore; 163, string_content:encoding
code: def _decode_filename_to_unicode(f):
      '''Get bytestring filename and return unicode.
      First, try to decode from default file system encoding
      If that fails, use ``chardet`` module to guess encoding.
      As a last resort, try to decode as utf-8.
  
      If the argument already is unicode, return as is'''
  
      log.debug('_decode_filename_to_unicode(%s)', repr(f))
      if isinstance(f, unicode):
          return f
      try:
          return f.decode(sys.getfilesystemencoding())
      except UnicodeDecodeError:
          charguess = chardet.detect(f)
          log.debug("chardet filename: %r -> %r", f, charguess)
          if charguess['encoding'] is not None:
              try:
                  return f.decode(charguess['encoding'])
              except UnicodeDecodeError:
                  pass
          log.warning('Cannot understand decoding of this filename: %r (guessed %r, but was wrong)',
                      f, charguess)
          log.debug('Trying utf-8 to decode %r', f)
          try:
              return f.decode('utf-8')
          except UnicodeDecodeError:
              pass
          log.debug('Trying latin1 to decode %r', f)
          try:
              return f.decode('latin1')
          except UnicodeDecodeError:
              log.warning('Exhausted all options. Decoding %r to safe ascii', f)
              return f.decode('ascii', errors='ignore')