1+ from __future__ import annotations
2+
13import importlib
24import io
35import logging
68from urllib .request import urlopen
79
810
9- def open_read_file (path ):
11+ def open_read_file (path , encoding : str | None = None ):
1012 """
1113 Return a file object from the path. Automatically detects and supports
1214 URLs and compression. If path is pathlike, it's converted to a string.
1315 If path is not a string nor pathlike, it's passed through without
14- modification.
16+ modification. Use encoding to set the text character set encoding.
17+ Use `encoding=None` to use the platform-dependent default locale encoding.
1518 """
1619 # Convert pathlike objects to string paths
1720 if hasattr (path , "__fspath__" ):
@@ -29,16 +32,17 @@ def open_read_file(path):
2932 with urlopen (path ) as response :
3033 content = response .read ()
3134 if opener == io .open :
32- encoding = response .headers .get_content_charset (failobj = "utf-8" )
35+ if not encoding :
36+ encoding = response .headers .get_content_charset (failobj = "utf-8" )
3337 logging .info (f"Will decode content from { path } using { encoding } charset." )
3438 text = content .decode (encoding )
3539 return io .StringIO (text )
3640 else :
3741 compressed_bytes = io .BytesIO (content )
38- return opener (compressed_bytes , "rt" )
42+ return opener (compressed_bytes , "rt" , encoding = encoding )
3943
4044 # Read from file
41- return opener (path , "rt" )
45+ return opener (path , "rt" , encoding = encoding )
4246
4347
4448compression_to_module = {
0 commit comments