I decided to experiment with overriding the built-in open function to detect problematic calls, by inserting this code at the beginning of every PRESUBMIT.py script (easily done given how presubmit_support.py works):
import traceback
old_open = open
def open(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
if not 'b' in mode and encoding == None:
print('No-encoding when opening %s with %s at:\n%s\n' % (file, mode, ''.join(traceback.format_stack(None, 8))))
return old_open(file, mode, buffering, encoding, errors, newline, closefd, opener)
When reading files this could even be smart enough to only report a problem if the file being read contains non-ASCII characters. So far it's found three errors, including this one:
No-encoding when opening build/OWNERS.setnoparent with r at:
File "c:\src\depot_tools\presubmit_support.py", line 2120, in main
return DoPresubmitChecks(
File "c:\src\depot_tools\presubmit_support.py", line 1817, in DoPresubmitChecks
results += executer.ExecPresubmitScript(presubmit_script, filename)
File "c:\src\depot_tools\presubmit_support.py", line 1586, in ExecPresubmitScript
return self._execute_with_local_working_directory(script_text,
File "c:\src\depot_tools\presubmit_support.py", line 1648, in _execute_with_local_working_directory
self._run_check_function(function_name, context, sink,
File "c:\src\depot_tools\presubmit_support.py", line 1687, in _run_check_function
result = eval(function_name + '(*__args)', context)
File "<string>", line 1, in <module>
File "c:\src\chromium\src\PRESUBMIT.py", line 3807, in CheckSetNoParent
with open(allowed_owners_files_file, 'r') as f:
File "c:\src\chromium\src\PRESUBMIT.py", line 15, in open
print('No-encoding when opening %s with %s at:\n%s\n' % (file, mode, ''.join(traceback.format_stack(None, 8))))
I'm not sure if there is any easy equivalent to this for scripts used during builds or tests.