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

bpo-39468: Improve the site module's error handling while writing .python_history #18299

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

opensource-assist
Copy link
Contributor

@opensource-assist opensource-assist commented Jan 31, 2020

Lib/site.py Outdated Show resolved Hide resolved
@opensource-assist opensource-assist changed the title bpo-39468: Improved the site module's permission handling while writing .python_history bpo-39468: Improved the site module's error handling while writing .python_history Jan 31, 2020
@opensource-assist opensource-assist changed the title bpo-39468: Improved the site module's error handling while writing .python_history bpo-39468: Improve the site module's error handling while writing .python_history Jan 31, 2020
@eryksun
Copy link
Contributor

eryksun commented Jan 31, 2020

@eryksun
If I was to report this bug in the GNU readline's tracker, what should I have called the title?

I'd title it "errno not returned for some errors in write_history, append_history, and history_truncate_file". In particular these calls return -1 when the internal histfile_restore call fails because rename fails. It's fine for histfile_restore to return the result from rename, but this should be checked for failure (e.g. -1) and handled appropriately by the caller in history_do_write and history_truncate_file. For example, in history_do_write they do the following:

if (rv == 0 && histname && tempname)
    rv = histfile_restore (tempname, histname);

if (rv != 0)
    {
      if (tempname)
	unlink (tempname);
      history_lines_written_to_file = 0;
    }

This needs a simple fix to update the value of rv when histfile_restore fails:

if (rv == 0 && histname && tempname)
    rv = histfile_restore (tempname, histname);

if (rv != 0) {
    rv = errno;
    if (tempname)
        unlink(tempname);
    history_lines_written_to_file = 0;
}

@codecov
Copy link

codecov bot commented Jan 31, 2020

Codecov Report

Merging #18299 into master will decrease coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18299      +/-   ##
==========================================
- Coverage   82.20%   82.12%   -0.08%     
==========================================
  Files        1957     1954       -3     
  Lines      589079   583367    -5712     
  Branches    44401    44401              
==========================================
- Hits       484247   479093    -5154     
+ Misses      95174    94636     -538     
+ Partials     9658     9638      -20     
Impacted Files Coverage Δ
Lib/distutils/tests/test_bdist_rpm.py 30.00% <0.00%> (-65.00%) ⬇️
Lib/distutils/command/bdist_rpm.py 7.63% <0.00%> (-56.88%) ⬇️
Lib/test/test_urllib2net.py 76.92% <0.00%> (-13.85%) ⬇️
Lib/test/test_smtpnet.py 78.57% <0.00%> (-12.86%) ⬇️
Lib/ftplib.py 63.85% <0.00%> (-6.06%) ⬇️
Lib/test/test_ftplib.py 87.11% <0.00%> (-4.72%) ⬇️
Lib/dbm/__init__.py 66.66% <0.00%> (-4.45%) ⬇️
Tools/scripts/db2pickle.py 17.82% <0.00%> (-3.97%) ⬇️
Lib/test/test_socket.py 71.94% <0.00%> (-3.87%) ⬇️
Tools/scripts/pickle2db.py 16.98% <0.00%> (-3.78%) ⬇️
... and 312 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 58a4054...087f9da. Read the comment docs.

Lib/site.py Outdated Show resolved Hide resolved
@opensource-assist
Copy link
Contributor Author

I still want to notice the users that the .python_history file is not writable, so that they wouldn't guess there's something wrong somewhere else.

# https://bugs.python.org/issue19891
pass
elif isinstance(e, (OSError)) and e.errno == -1:
print("Warning: unable to write into .python_history")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use two except clauses:

except (FileNotFoundError, PermissionError):
   pass
except OsError:
    if e.errno == -1:
       print(...)
    else:
        raise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, are you sure that printing to stdout is the right thing to do here?

@MaxwellDupre
Copy link
Contributor

I am getting an error trying to check out this branch:
gh pr checkout 18299
From https://github.com/python/cpython
! [rejected] refs/pull/18299/head -> master (non-fast-forward)
exit status 1

My GH clone is up to date with cpython and my local repro is up to date with my clone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants