Skip to content
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

Closed
josephsl opened this issue Dec 9, 2018 · 3 comments
Closed

Python 3: file() function is no more #9038

josephsl opened this issue Dec 9, 2018 · 3 comments
Assignees
Labels
Milestone

Comments

@josephsl
Copy link
Collaborator

josephsl commented Dec 9, 2018

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:

  1. Run Python 2 and 3 interpreters.
  2. Try the following code path:

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.

@josephsl josephsl added the z Python 3 transition (archived) Python 3 transition label Dec 9, 2018
@josephsl
Copy link
Collaborator Author

josephsl commented Dec 9, 2018

Hi,

The affected files are:

  • source/addonHandler/init.py
  • source/buildVersion.py
  • source/config/init.py
  • source/gui/logViewer.py
  • source/logHandler.py
  • source/NVDAHelper.py
  • source/updateCheck.py
  • source/watchdog.py
  • Any module (NVDA Core or add-ons) that deals with file I/O and uses file() function

Thanks.

@Brian1Gaff

This comment has been minimized.

josephsl added a commit to josephsl/nvda that referenced this issue May 25, 2019
.

Even in Python 2, open() function is used, and in Python 3, file() is gone. Thus unify under open() with explanatory comments added.
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
…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
…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
…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.
@nvaccessAuto nvaccessAuto added this to the 2019.3 milestone Jun 8, 2019
@feerrenrut feerrenrut moved this from To do to Done in Update NVDA to Python 3 Jun 12, 2019
@feerrenrut
Copy link
Contributor

Closed with #9663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

No branches or pull requests

4 participants