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