@@ -128,45 +128,9 @@ Traceback (most recent call last):
128128UnicodeDecodeError : ' utf-8' codec can' t decode byte 0xb2 in position 0: invalid start byte
129129```
130130
131- Of course, `xcp/ net/ ifrename` won' t be affected but it would be good to fix the
132- warning for them as well in an intelligent way. See the proposal for that below.
133-
134- There are a couple of possibilities and Python because 2.7 does not support the
135- arguments we need to pass to ensure that all users of open () will work, we need
136- to make passing the arguments conditional on Python >= 3 .
137-
138- 1 . Overriding `open ()` , while technically working would not only affect xcp.python but the entire program:
139-
140- ```py
141- if sys.version_info >= (3 , 0 ):
142- original_open = __builtins__[" open" ]
143- def uopen(* args, ** kwargs):
144- if " b" not in (args[1 ] \
145- if len (args) >= 2 else kwargs.get(" mode" , " " )):
146- kwargs.setdefault(" encoding" , " UTF-8" )
147- kwargs.setdefault(" errors" , " replace" )
148- return original_open(* args, ** kwargs)
149- __builtins__[" open" ] = uopen
150- ```
151-
152- 2 . This is sufficient but is not very nice:
153-
154- ```py
155- # xcp/utf8mode.py
156- if sys.version_info >= (3 , 0 ):
157- open_utf8args = {" encoding" : " utf-8" , " errors" : " replace" }
158- else :
159- open_utf8args = {}
160- # xcp/{cmd,pci,environ?,logger?}.py tests/test_{pci,biodevname?,...?}.py
161- + from .utf8mode import open_utf8args
162- ...
163- - open (filename)
164- + open (filename, ** open_utf8args)
165- ```
166-
167- But, `pylint` will still warn about these lines, so I propose:
168-
169- 3 . Instead, use a wrapper function, which will also silence the `pylint` warnings at the locations which have been changed to use it:
131+ To fix these issues, `xcp.compat` , provides a wrapper for `open ()` .
132+ It adds `encoding=" utf-8" , errors=" replace" `
133+ to enable UTF - 8 conversion and handle encoding errors:
170134
171135 ```py
172136 # xcp/utf8mode.py
@@ -184,9 +148,3 @@ to make passing the arguments conditional on Python >= 3.
184148 + utf8open(filename)
185149 ```
186150
187- Using the 3rd option, the `pylint` warnings for the changed locations
188- `unspecified- encoding` and `consider- using- with ` don' t appear without
189- explicitly disabling them.
190-
191- PS : Since utf8open() still returns a context- manager, `with open (... ) as f:`
192- would still work.
0 commit comments