New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3: file() function is no more #9038
Comments
Hi, The affected files are:
Thanks. |
This comment has been minimized.
This comment has been minimized.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 2, 2019
…function for reading from and writing to pickle files. Re nvaccess#9038.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 2, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 2, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 2, 2019
…d/commit for version information construction. Re nvaccess#9038. Without modifying head/commit values for version info, NVDA will report 'year.major.minor' when running from source. Therefore edit this routine.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 2, 2019
…d encoding issue. Re nvaccess#9038. When saving logs, a log file object will be created. The routine responsible for this is eligible for conversion to 'with open' statement, but there is a note about UTF-8 codecs conversion problem. Thus add a note regarding this and exclude that routine from conversion for now.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 4, 2019
…function for reading from and writing to pickle files. Re nvaccess#9038.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 4, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 4, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 4, 2019
…d/commit for version information construction. Re nvaccess#9038. Without modifying head/commit values for version info, NVDA will report 'year.major.minor' when running from source. Therefore edit this routine.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 4, 2019
…d encoding issue. Re nvaccess#9038. When saving logs, a log file object will be created. The routine responsible for this is eligible for conversion to 'with open' statement, but there is a note about UTF-8 codecs conversion problem. Thus add a note regarding this and exclude that routine from conversion for now.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
…function for reading from and writing to pickle files. Re nvaccess#9038.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
…d/commit for version information construction. Re nvaccess#9038. Without modifying head/commit values for version info, NVDA will report 'year.major.minor' when running from source. Therefore edit this routine.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
…d encoding issue. Re nvaccess#9038. When saving logs, a log file object will be created. The routine responsible for this is eligible for conversion to 'with open' statement, but there is a note about UTF-8 codecs conversion problem. Thus add a note regarding this and exclude that routine from conversion for now.
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
…ing with pickles. Re nvaccess#9038. Clarified by Mick Curran (NV Access): in Python 3, when loading and dumping pickles, binary format must be used (rb/wb).
josephsl
added a commit
to josephsl/nvda
that referenced
this issue
Jun 5, 2019
Reviewed by Mick Curran (NV Access): open log file for saving with UTF-8 encoding from the start. Note that Python 2's open function does not include encoding parameter directly, but Python 3 does.
michaelDCurran
pushed a commit
that referenced
this issue
Jun 8, 2019
* Add-on handler and update check/Python 3: file function -> with open function for reading from and writing to pickle files. Re #9038. * NVDA helper and watchdog/Python 3: with file -> with open function. Re #9038. * Config/Python 3: use open function when creating an empty profile data file. Re #9038. * Build version/Python 3: use with open function when obtaining Git head/commit for version information construction. Re #9038. Without modifying head/commit values for version info, NVDA will report 'year.major.minor' when running from source. Therefore edit this routine. * Log viewer/Python 3: add an explanatory note on with open function and encoding issue. Re #9038. When saving logs, a log file object will be created. The routine responsible for this is eligible for conversion to 'with open' statement, but there is a note about UTF-8 codecs conversion problem. Thus add a note regarding this and exclude that routine from conversion for now. * Add-on handler and update check/Python 3: use binary format when working with pickles. Re #9038. Clarified by Mick Curran (NV Access): in Python 3, when loading and dumping pickles, binary format must be used (rb/wb). * Log viewer/Python 3: save log file in UTF-8 format. Re #9038. Reviewed by Mick Curran (NV Access): open log file for saving with UTF-8 encoding from the start. Note that Python 2's open function does not include encoding parameter directly, but Python 3 does.
Closed with #9663 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Continuing research on road blocks to Python 3 transition (see #7105 for rationale).
Is your feature request related to a problem? Please describe.
Problem: in Python 3, file() function is no more.
Steps to reproduce:
something = file(somefilename)
python 2: works as advertised.
Python 3: name error is thrown.
This is because in python 2, file() function is really a constructor for file objects (or a stream object treated as a file). The general recommendation from various sources is to use open() function to create a file object, and close it once finished. Another way is through a context manager (with statement).
Describe the solution you'd like
The ideal solution is use of context manager syntax of the form:
with open(somefile, mode) as someMeaningfulName:
dosomething …
Note: spaces are used for indentation.
Describe alternatives you've considered
Another alternative is using the form:
something = open(somefile, mode)
code …
something.close()
Although applying this solution everywhere may sound consistent, there are cases (such as when opening and closing pickle files) where context manager syntax makes more sense.
Additional context
The solutions proposed above are both Python 2 and 3 compliant, which should assist with #7105. So far, tests with a Python 3 version of NVDA indicate that open()/code/close() will work for the most part, but some code paths may benefit from use of context manager (I would say most would).
Thanks.
The text was updated successfully, but these errors were encountered: