mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	bpo-42278: Use tempfile.TemporaryDirectory rather than tempfile.mktemp in pydoc (GH-23200)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
		
							parent
							
								
									2a8127cafe
								
							
						
					
					
						commit
						c9227df5a9
					
				
					 2 changed files with 9 additions and 6 deletions
				
			
		
							
								
								
									
										13
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								Lib/pydoc.py
									
										
									
									
									
								
							| 
						 | 
					@ -1618,13 +1618,14 @@ def pipepager(text, cmd):
 | 
				
			||||||
def tempfilepager(text, cmd):
 | 
					def tempfilepager(text, cmd):
 | 
				
			||||||
    """Page through text by invoking a program on a temporary file."""
 | 
					    """Page through text by invoking a program on a temporary file."""
 | 
				
			||||||
    import tempfile
 | 
					    import tempfile
 | 
				
			||||||
    filename = tempfile.mktemp()
 | 
					    with tempfile.TemporaryDirectory() as tempdir:
 | 
				
			||||||
    with open(filename, 'w', errors='backslashreplace') as file:
 | 
					        filename = os.path.join(tempdir, 'pydoc.out')
 | 
				
			||||||
        file.write(text)
 | 
					        with open(filename, 'w', errors='backslashreplace',
 | 
				
			||||||
    try:
 | 
					                  encoding=os.device_encoding(0) if
 | 
				
			||||||
 | 
					                  sys.platform == 'win32' else None
 | 
				
			||||||
 | 
					                  ) as file:
 | 
				
			||||||
 | 
					            file.write(text)
 | 
				
			||||||
        os.system(cmd + ' "' + filename + '"')
 | 
					        os.system(cmd + ' "' + filename + '"')
 | 
				
			||||||
    finally:
 | 
					 | 
				
			||||||
        os.unlink(filename)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _escape_stdout(text):
 | 
					def _escape_stdout(text):
 | 
				
			||||||
    # Escape non-encodable characters to avoid encoding errors later
 | 
					    # Escape non-encodable characters to avoid encoding errors later
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Replaced usage of :func:`tempfile.mktemp` with
 | 
				
			||||||
 | 
					:class:`~tempfile.TemporaryDirectory` to avoid a potential race condition.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue