@@ -346,7 +346,7 @@ def _args_from_interpreter_flags():
346346 if dev_mode :
347347 args .extend (('-X' , 'dev' ))
348348 for opt in ('faulthandler' , 'tracemalloc' , 'importtime' ,
349- 'showrefcount' , 'utf8' ):
349+ 'frozen_modules' , ' showrefcount' , 'utf8' ):
350350 if opt in xoptions :
351351 value = xoptions [opt ]
352352 if value is True :
@@ -872,37 +872,6 @@ def __init__(self, args, bufsize=-1, executable=None,
872872 'and universal_newlines are supplied but '
873873 'different. Pass one or the other.' )
874874
875- # Input and output objects. The general principle is like
876- # this:
877- #
878- # Parent Child
879- # ------ -----
880- # p2cwrite ---stdin---> p2cread
881- # c2pread <--stdout--- c2pwrite
882- # errread <--stderr--- errwrite
883- #
884- # On POSIX, the child objects are file descriptors. On
885- # Windows, these are Windows file handles. The parent objects
886- # are file descriptors on both platforms. The parent objects
887- # are -1 when not using PIPEs. The child objects are -1
888- # when not redirecting.
889-
890- (p2cread , p2cwrite ,
891- c2pread , c2pwrite ,
892- errread , errwrite ) = self ._get_handles (stdin , stdout , stderr )
893-
894- # We wrap OS handles *before* launching the child, otherwise a
895- # quickly terminating child could make our fds unwrappable
896- # (see #8458).
897-
898- if _mswindows :
899- if p2cwrite != - 1 :
900- p2cwrite = msvcrt .open_osfhandle (p2cwrite .Detach (), 0 )
901- if c2pread != - 1 :
902- c2pread = msvcrt .open_osfhandle (c2pread .Detach (), 0 )
903- if errread != - 1 :
904- errread = msvcrt .open_osfhandle (errread .Detach (), 0 )
905-
906875 self .text_mode = encoding or errors or text or universal_newlines
907876 if self .text_mode and encoding is None :
908877 self .encoding = encoding = _text_encoding ()
@@ -1003,6 +972,39 @@ def __init__(self, args, bufsize=-1, executable=None,
1003972 if uid < 0 :
1004973 raise ValueError (f"User ID cannot be negative, got { uid } " )
1005974
975+ # Input and output objects. The general principle is like
976+ # this:
977+ #
978+ # Parent Child
979+ # ------ -----
980+ # p2cwrite ---stdin---> p2cread
981+ # c2pread <--stdout--- c2pwrite
982+ # errread <--stderr--- errwrite
983+ #
984+ # On POSIX, the child objects are file descriptors. On
985+ # Windows, these are Windows file handles. The parent objects
986+ # are file descriptors on both platforms. The parent objects
987+ # are -1 when not using PIPEs. The child objects are -1
988+ # when not redirecting.
989+
990+ (p2cread , p2cwrite ,
991+ c2pread , c2pwrite ,
992+ errread , errwrite ) = self ._get_handles (stdin , stdout , stderr )
993+
994+ # From here on, raising exceptions may cause file descriptor leakage
995+
996+ # We wrap OS handles *before* launching the child, otherwise a
997+ # quickly terminating child could make our fds unwrappable
998+ # (see #8458).
999+
1000+ if _mswindows :
1001+ if p2cwrite != - 1 :
1002+ p2cwrite = msvcrt .open_osfhandle (p2cwrite .Detach (), 0 )
1003+ if c2pread != - 1 :
1004+ c2pread = msvcrt .open_osfhandle (c2pread .Detach (), 0 )
1005+ if errread != - 1 :
1006+ errread = msvcrt .open_osfhandle (errread .Detach (), 0 )
1007+
10061008 try :
10071009 if p2cwrite != - 1 :
10081010 self .stdin = io .open (p2cwrite , 'wb' , bufsize )
@@ -1306,6 +1308,26 @@ def _close_pipe_fds(self,
13061308 # Prevent a double close of these handles/fds from __init__ on error.
13071309 self ._closed_child_pipe_fds = True
13081310
1311+ @contextlib .contextmanager
1312+ def _on_error_fd_closer (self ):
1313+ """Helper to ensure file descriptors opened in _get_handles are closed"""
1314+ to_close = []
1315+ try :
1316+ yield to_close
1317+ except :
1318+ if hasattr (self , '_devnull' ):
1319+ to_close .append (self ._devnull )
1320+ del self ._devnull
1321+ for fd in to_close :
1322+ try :
1323+ if _mswindows and isinstance (fd , Handle ):
1324+ fd .Close ()
1325+ else :
1326+ os .close (fd )
1327+ except OSError :
1328+ pass
1329+ raise
1330+
13091331 if _mswindows :
13101332 #
13111333 # Windows methods
@@ -1321,61 +1343,68 @@ def _get_handles(self, stdin, stdout, stderr):
13211343 c2pread , c2pwrite = - 1 , - 1
13221344 errread , errwrite = - 1 , - 1
13231345
1324- if stdin is None :
1325- p2cread = _winapi .GetStdHandle (_winapi .STD_INPUT_HANDLE )
1326- if p2cread is None :
1327- p2cread , _ = _winapi .CreatePipe (None , 0 )
1328- p2cread = Handle (p2cread )
1329- _winapi .CloseHandle (_ )
1330- elif stdin == PIPE :
1331- p2cread , p2cwrite = _winapi .CreatePipe (None , 0 )
1332- p2cread , p2cwrite = Handle (p2cread ), Handle (p2cwrite )
1333- elif stdin == DEVNULL :
1334- p2cread = msvcrt .get_osfhandle (self ._get_devnull ())
1335- elif isinstance (stdin , int ):
1336- p2cread = msvcrt .get_osfhandle (stdin )
1337- else :
1338- # Assuming file-like object
1339- p2cread = msvcrt .get_osfhandle (stdin .fileno ())
1340- p2cread = self ._make_inheritable (p2cread )
1341-
1342- if stdout is None :
1343- c2pwrite = _winapi .GetStdHandle (_winapi .STD_OUTPUT_HANDLE )
1344- if c2pwrite is None :
1345- _ , c2pwrite = _winapi .CreatePipe (None , 0 )
1346- c2pwrite = Handle (c2pwrite )
1347- _winapi .CloseHandle (_ )
1348- elif stdout == PIPE :
1349- c2pread , c2pwrite = _winapi .CreatePipe (None , 0 )
1350- c2pread , c2pwrite = Handle (c2pread ), Handle (c2pwrite )
1351- elif stdout == DEVNULL :
1352- c2pwrite = msvcrt .get_osfhandle (self ._get_devnull ())
1353- elif isinstance (stdout , int ):
1354- c2pwrite = msvcrt .get_osfhandle (stdout )
1355- else :
1356- # Assuming file-like object
1357- c2pwrite = msvcrt .get_osfhandle (stdout .fileno ())
1358- c2pwrite = self ._make_inheritable (c2pwrite )
1359-
1360- if stderr is None :
1361- errwrite = _winapi .GetStdHandle (_winapi .STD_ERROR_HANDLE )
1362- if errwrite is None :
1363- _ , errwrite = _winapi .CreatePipe (None , 0 )
1364- errwrite = Handle (errwrite )
1365- _winapi .CloseHandle (_ )
1366- elif stderr == PIPE :
1367- errread , errwrite = _winapi .CreatePipe (None , 0 )
1368- errread , errwrite = Handle (errread ), Handle (errwrite )
1369- elif stderr == STDOUT :
1370- errwrite = c2pwrite
1371- elif stderr == DEVNULL :
1372- errwrite = msvcrt .get_osfhandle (self ._get_devnull ())
1373- elif isinstance (stderr , int ):
1374- errwrite = msvcrt .get_osfhandle (stderr )
1375- else :
1376- # Assuming file-like object
1377- errwrite = msvcrt .get_osfhandle (stderr .fileno ())
1378- errwrite = self ._make_inheritable (errwrite )
1346+ with self ._on_error_fd_closer () as err_close_fds :
1347+ if stdin is None :
1348+ p2cread = _winapi .GetStdHandle (_winapi .STD_INPUT_HANDLE )
1349+ if p2cread is None :
1350+ p2cread , _ = _winapi .CreatePipe (None , 0 )
1351+ p2cread = Handle (p2cread )
1352+ err_close_fds .append (p2cread )
1353+ _winapi .CloseHandle (_ )
1354+ elif stdin == PIPE :
1355+ p2cread , p2cwrite = _winapi .CreatePipe (None , 0 )
1356+ p2cread , p2cwrite = Handle (p2cread ), Handle (p2cwrite )
1357+ err_close_fds .extend ((p2cread , p2cwrite ))
1358+ elif stdin == DEVNULL :
1359+ p2cread = msvcrt .get_osfhandle (self ._get_devnull ())
1360+ elif isinstance (stdin , int ):
1361+ p2cread = msvcrt .get_osfhandle (stdin )
1362+ else :
1363+ # Assuming file-like object
1364+ p2cread = msvcrt .get_osfhandle (stdin .fileno ())
1365+ p2cread = self ._make_inheritable (p2cread )
1366+
1367+ if stdout is None :
1368+ c2pwrite = _winapi .GetStdHandle (_winapi .STD_OUTPUT_HANDLE )
1369+ if c2pwrite is None :
1370+ _ , c2pwrite = _winapi .CreatePipe (None , 0 )
1371+ c2pwrite = Handle (c2pwrite )
1372+ err_close_fds .append (c2pwrite )
1373+ _winapi .CloseHandle (_ )
1374+ elif stdout == PIPE :
1375+ c2pread , c2pwrite = _winapi .CreatePipe (None , 0 )
1376+ c2pread , c2pwrite = Handle (c2pread ), Handle (c2pwrite )
1377+ err_close_fds .extend ((c2pread , c2pwrite ))
1378+ elif stdout == DEVNULL :
1379+ c2pwrite = msvcrt .get_osfhandle (self ._get_devnull ())
1380+ elif isinstance (stdout , int ):
1381+ c2pwrite = msvcrt .get_osfhandle (stdout )
1382+ else :
1383+ # Assuming file-like object
1384+ c2pwrite = msvcrt .get_osfhandle (stdout .fileno ())
1385+ c2pwrite = self ._make_inheritable (c2pwrite )
1386+
1387+ if stderr is None :
1388+ errwrite = _winapi .GetStdHandle (_winapi .STD_ERROR_HANDLE )
1389+ if errwrite is None :
1390+ _ , errwrite = _winapi .CreatePipe (None , 0 )
1391+ errwrite = Handle (errwrite )
1392+ err_close_fds .append (errwrite )
1393+ _winapi .CloseHandle (_ )
1394+ elif stderr == PIPE :
1395+ errread , errwrite = _winapi .CreatePipe (None , 0 )
1396+ errread , errwrite = Handle (errread ), Handle (errwrite )
1397+ err_close_fds .extend ((errread , errwrite ))
1398+ elif stderr == STDOUT :
1399+ errwrite = c2pwrite
1400+ elif stderr == DEVNULL :
1401+ errwrite = msvcrt .get_osfhandle (self ._get_devnull ())
1402+ elif isinstance (stderr , int ):
1403+ errwrite = msvcrt .get_osfhandle (stderr )
1404+ else :
1405+ # Assuming file-like object
1406+ errwrite = msvcrt .get_osfhandle (stderr .fileno ())
1407+ errwrite = self ._make_inheritable (errwrite )
13791408
13801409 return (p2cread , p2cwrite ,
13811410 c2pread , c2pwrite ,
@@ -1480,7 +1509,23 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
14801509 if shell :
14811510 startupinfo .dwFlags |= _winapi .STARTF_USESHOWWINDOW
14821511 startupinfo .wShowWindow = _winapi .SW_HIDE
1483- comspec = os .environ .get ("COMSPEC" , "cmd.exe" )
1512+ if not executable :
1513+ # gh-101283: without a fully-qualified path, before Windows
1514+ # checks the system directories, it first looks in the
1515+ # application directory, and also the current directory if
1516+ # NeedCurrentDirectoryForExePathW(ExeName) is true, so try
1517+ # to avoid executing unqualified "cmd.exe".
1518+ comspec = os .environ .get ('ComSpec' )
1519+ if not comspec :
1520+ system_root = os .environ .get ('SystemRoot' , '' )
1521+ comspec = os .path .join (system_root , 'System32' , 'cmd.exe' )
1522+ if not os .path .isabs (comspec ):
1523+ raise FileNotFoundError ('shell not found: neither %ComSpec% nor %SystemRoot% is set' )
1524+ if os .path .isabs (comspec ):
1525+ executable = comspec
1526+ else :
1527+ comspec = executable
1528+
14841529 args = '{} /c "{}"' .format (comspec , args )
14851530
14861531 if cwd is not None :
@@ -1536,6 +1581,8 @@ def _wait(self, timeout):
15361581 """Internal implementation of wait() on Windows."""
15371582 if timeout is None :
15381583 timeout_millis = _winapi .INFINITE
1584+ elif timeout <= 0 :
1585+ timeout_millis = 0
15391586 else :
15401587 timeout_millis = int (timeout * 1000 )
15411588 if self .returncode is None :
@@ -1646,52 +1693,56 @@ def _get_handles(self, stdin, stdout, stderr):
16461693 c2pread , c2pwrite = - 1 , - 1
16471694 errread , errwrite = - 1 , - 1
16481695
1649- if stdin is None :
1650- pass
1651- elif stdin == PIPE :
1652- p2cread , p2cwrite = os .pipe ()
1653- if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1654- fcntl .fcntl (p2cwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1655- elif stdin == DEVNULL :
1656- p2cread = self ._get_devnull ()
1657- elif isinstance (stdin , int ):
1658- p2cread = stdin
1659- else :
1660- # Assuming file-like object
1661- p2cread = stdin .fileno ()
1696+ with self ._on_error_fd_closer () as err_close_fds :
1697+ if stdin is None :
1698+ pass
1699+ elif stdin == PIPE :
1700+ p2cread , p2cwrite = os .pipe ()
1701+ err_close_fds .extend ((p2cread , p2cwrite ))
1702+ if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1703+ fcntl .fcntl (p2cwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1704+ elif stdin == DEVNULL :
1705+ p2cread = self ._get_devnull ()
1706+ elif isinstance (stdin , int ):
1707+ p2cread = stdin
1708+ else :
1709+ # Assuming file-like object
1710+ p2cread = stdin .fileno ()
16621711
1663- if stdout is None :
1664- pass
1665- elif stdout == PIPE :
1666- c2pread , c2pwrite = os .pipe ()
1667- if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1668- fcntl .fcntl (c2pwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1669- elif stdout == DEVNULL :
1670- c2pwrite = self ._get_devnull ()
1671- elif isinstance (stdout , int ):
1672- c2pwrite = stdout
1673- else :
1674- # Assuming file-like object
1675- c2pwrite = stdout .fileno ()
1712+ if stdout is None :
1713+ pass
1714+ elif stdout == PIPE :
1715+ c2pread , c2pwrite = os .pipe ()
1716+ err_close_fds .extend ((c2pread , c2pwrite ))
1717+ if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1718+ fcntl .fcntl (c2pwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1719+ elif stdout == DEVNULL :
1720+ c2pwrite = self ._get_devnull ()
1721+ elif isinstance (stdout , int ):
1722+ c2pwrite = stdout
1723+ else :
1724+ # Assuming file-like object
1725+ c2pwrite = stdout .fileno ()
16761726
1677- if stderr is None :
1678- pass
1679- elif stderr == PIPE :
1680- errread , errwrite = os .pipe ()
1681- if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1682- fcntl .fcntl (errwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1683- elif stderr == STDOUT :
1684- if c2pwrite != - 1 :
1685- errwrite = c2pwrite
1686- else : # child's stdout is not set, use parent's stdout
1687- errwrite = sys .__stdout__ .fileno ()
1688- elif stderr == DEVNULL :
1689- errwrite = self ._get_devnull ()
1690- elif isinstance (stderr , int ):
1691- errwrite = stderr
1692- else :
1693- # Assuming file-like object
1694- errwrite = stderr .fileno ()
1727+ if stderr is None :
1728+ pass
1729+ elif stderr == PIPE :
1730+ errread , errwrite = os .pipe ()
1731+ err_close_fds .extend ((errread , errwrite ))
1732+ if self .pipesize > 0 and hasattr (fcntl , "F_SETPIPE_SZ" ):
1733+ fcntl .fcntl (errwrite , fcntl .F_SETPIPE_SZ , self .pipesize )
1734+ elif stderr == STDOUT :
1735+ if c2pwrite != - 1 :
1736+ errwrite = c2pwrite
1737+ else : # child's stdout is not set, use parent's stdout
1738+ errwrite = sys .__stdout__ .fileno ()
1739+ elif stderr == DEVNULL :
1740+ errwrite = self ._get_devnull ()
1741+ elif isinstance (stderr , int ):
1742+ errwrite = stderr
1743+ else :
1744+ # Assuming file-like object
1745+ errwrite = stderr .fileno ()
16951746
16961747 return (p2cread , p2cwrite ,
16971748 c2pread , c2pwrite ,
@@ -1889,16 +1940,21 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
18891940 SubprocessError )
18901941 if issubclass (child_exception_type , OSError ) and hex_errno :
18911942 errno_num = int (hex_errno , 16 )
1892- child_exec_never_called = (err_msg == "noexec" )
1893- if child_exec_never_called :
1943+ if err_msg == "noexec:chdir" :
18941944 err_msg = ""
18951945 # The error must be from chdir(cwd).
18961946 err_filename = cwd
1947+ elif err_msg == "noexec" :
1948+ err_msg = ""
1949+ err_filename = None
18971950 else :
18981951 err_filename = orig_executable
18991952 if errno_num != 0 :
19001953 err_msg = os .strerror (errno_num )
1901- raise child_exception_type (errno_num , err_msg , err_filename )
1954+ if err_filename is not None :
1955+ raise child_exception_type (errno_num , err_msg , err_filename )
1956+ else :
1957+ raise child_exception_type (errno_num , err_msg )
19021958 raise child_exception_type (err_msg )
19031959
19041960
0 commit comments