Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c7a90a

Browse files
gh-122273: Support PyREPL history on Windows (#127141)
Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
1 parentf46d847 commit3c7a90a

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

‎Lib/_pyrepl/readline.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None:
450450
defwrite_history_file(self,filename:str=gethistoryfile())->None:
451451
maxlength=self.saved_history_length
452452
history=self.get_reader().get_trimmed_history(maxlength)
453-
withopen(os.path.expanduser(filename),"w",encoding="utf-8")asf:
453+
f=open(os.path.expanduser(filename),"w",
454+
encoding="utf-8",newline="\n")
455+
withf:
454456
forentryinhistory:
455457
entry=entry.replace("\n","\r\n")# multiline history support
456458
f.write(entry+"\n")

‎Lib/site.py‎

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -498,40 +498,55 @@ def register_readline():
498498
PYTHON_BASIC_REPL=False
499499

500500
importatexit
501+
502+
try:
503+
try:
504+
importreadline
505+
exceptImportError:
506+
readline=None
507+
else:
508+
importrlcompleter# noqa: F401
509+
exceptImportError:
510+
return
511+
501512
try:
502-
importreadline
503-
importrlcompleter# noqa: F401
504513
ifPYTHON_BASIC_REPL:
505514
CAN_USE_PYREPL=False
506515
else:
507516
original_path=sys.path
508517
sys.path= [pforpinoriginal_pathifp!='']
509518
try:
510519
import_pyrepl.readline
511-
import_pyrepl.unix_console
520+
ifos.name=="nt":
521+
import_pyrepl.windows_console
522+
console_errors= (_pyrepl.windows_console._error,)
523+
else:
524+
import_pyrepl.unix_console
525+
console_errors=_pyrepl.unix_console._error
512526
from_pyrepl.mainimportCAN_USE_PYREPL
513527
finally:
514528
sys.path=original_path
515529
exceptImportError:
516530
return
517531

518-
# Reading the initialization (config) file may not be enough to set a
519-
# completion key, so we set one first and then read the file.
520-
ifreadline.backend=='editline':
521-
readline.parse_and_bind('bind ^I rl_complete')
522-
else:
523-
readline.parse_and_bind('tab: complete')
532+
ifreadlineisnotNone:
533+
# Reading the initialization (config) file may not be enough to set a
534+
# completion key, so we set one first and then read the file.
535+
ifreadline.backend=='editline':
536+
readline.parse_and_bind('bind ^I rl_complete')
537+
else:
538+
readline.parse_and_bind('tab: complete')
524539

525-
try:
526-
readline.read_init_file()
527-
exceptOSError:
528-
# An OSError here could have many causes, but the most likely one
529-
# is that there's no .inputrc file (or .editrc file in the case of
530-
# Mac OS X + libedit) in the expected location. In that case, we
531-
# want to ignore the exception.
532-
pass
540+
try:
541+
readline.read_init_file()
542+
exceptOSError:
543+
# An OSError here could have many causes, but the most likely one
544+
# is that there's no .inputrc file (or .editrc file in the case of
545+
# Mac OS X + libedit) in the expected location. In that case, we
546+
# want to ignore the exception.
547+
pass
533548

534-
ifreadline.get_current_history_length()==0:
549+
ifreadlineisNoneorreadline.get_current_history_length()==0:
535550
# If no history was loaded, default to .python_history,
536551
# or PYTHON_HISTORY.
537552
# The guard is necessary to avoid doubling history size at
@@ -542,8 +557,10 @@ def register_readline():
542557

543558
ifCAN_USE_PYREPL:
544559
readline_module=_pyrepl.readline
545-
exceptions= (OSError,*_pyrepl.unix_console._error)
560+
exceptions= (OSError,*console_errors)
546561
else:
562+
ifreadlineisNone:
563+
return
547564
readline_module=readline
548565
exceptions=OSError
549566

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support PyREPL history on Windows. Patch by devdanzin and Victor Stinner.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp