@@ -1182,6 +1182,8 @@ pub(crate) enum RunCommand {
11821182 PythonZipapp ( PathBuf , Vec < OsString > ) ,
11831183 /// Execute a `python` script provided via `stdin`.
11841184 PythonStdin ( Vec < u8 > , Vec < OsString > ) ,
1185+ /// Execute a `pythonw` script provided via `stdin`.
1186+ PythonGuiStdin ( Vec < u8 > , Vec < OsString > ) ,
11851187 /// Execute a Python script provided via a remote URL.
11861188 PythonRemote ( tempfile:: NamedTempFile , Vec < OsString > ) ,
11871189 /// Execute an external command.
@@ -1209,6 +1211,13 @@ impl RunCommand {
12091211 }
12101212 }
12111213 Self :: PythonStdin ( ..) => Cow :: Borrowed ( "python -c" ) ,
1214+ Self :: PythonGuiStdin ( ..) => {
1215+ if cfg ! ( windows) {
1216+ Cow :: Borrowed ( "pythonw -c" )
1217+ } else {
1218+ Cow :: Borrowed ( "python -c" )
1219+ }
1220+ }
12121221 Self :: External ( executable, _) => executable. to_string_lossy ( ) ,
12131222 }
12141223 }
@@ -1280,6 +1289,38 @@ impl RunCommand {
12801289
12811290 process
12821291 }
1292+ Self :: PythonGuiStdin ( script, args) => {
1293+ let python_executable = interpreter. sys_executable ( ) ;
1294+
1295+ // Use `pythonw.exe` if it exists, otherwise fall back to `python.exe`.
1296+ // See `install-wheel-rs::get_script_executable`.gd
1297+ let pythonw_executable = python_executable
1298+ . file_name ( )
1299+ . map ( |name| {
1300+ let new_name = name. to_string_lossy ( ) . replace ( "python" , "pythonw" ) ;
1301+ python_executable. with_file_name ( new_name)
1302+ } )
1303+ . filter ( |path| path. is_file ( ) )
1304+ . unwrap_or_else ( || python_executable. to_path_buf ( ) ) ;
1305+
1306+ let mut process = Command :: new ( & pythonw_executable) ;
1307+ process. arg ( "-c" ) ;
1308+
1309+ #[ cfg( unix) ]
1310+ {
1311+ use std:: os:: unix:: ffi:: OsStringExt ;
1312+ process. arg ( OsString :: from_vec ( script. clone ( ) ) ) ;
1313+ }
1314+
1315+ #[ cfg( not( unix) ) ]
1316+ {
1317+ let script = String :: from_utf8 ( script. clone ( ) ) . expect ( "script is valid UTF-8" ) ;
1318+ process. arg ( script) ;
1319+ }
1320+ process. args ( args) ;
1321+
1322+ process
1323+ }
12831324 Self :: External ( executable, args) => {
12841325 let mut process = Command :: new ( executable) ;
12851326 process. args ( args) ;
@@ -1328,6 +1369,10 @@ impl std::fmt::Display for RunCommand {
13281369 write ! ( f, "python -c" ) ?;
13291370 Ok ( ( ) )
13301371 }
1372+ Self :: PythonGuiStdin ( ..) => {
1373+ write ! ( f, "pythonw -c" ) ?;
1374+ Ok ( ( ) )
1375+ }
13311376 Self :: External ( executable, args) => {
13321377 write ! ( f, "{}" , executable. to_string_lossy( ) ) ?;
13331378 for arg in args {
@@ -1360,6 +1405,19 @@ impl RunCommand {
13601405 return Ok ( Self :: Empty ) ;
13611406 } ;
13621407
1408+ if target. eq_ignore_ascii_case ( "-" ) {
1409+ let mut buf = Vec :: with_capacity ( 1024 ) ;
1410+ std:: io:: stdin ( ) . read_to_end ( & mut buf) ?;
1411+
1412+ return if module {
1413+ Err ( anyhow ! ( "Cannot run a Python module from stdin" ) )
1414+ } else if gui_script {
1415+ Ok ( Self :: PythonGuiStdin ( buf, args. to_vec ( ) ) )
1416+ } else {
1417+ Ok ( Self :: PythonStdin ( buf, args. to_vec ( ) ) )
1418+ } ;
1419+ }
1420+
13631421 let target_path = PathBuf :: from ( target) ;
13641422
13651423 // Determine whether the user provided a remote script.
@@ -1402,21 +1460,17 @@ impl RunCommand {
14021460
14031461 if module {
14041462 return Ok ( Self :: PythonModule ( target. clone ( ) , args. to_vec ( ) ) ) ;
1405- } else if script {
1406- return Ok ( Self :: PythonScript ( target. clone ( ) . into ( ) , args. to_vec ( ) ) ) ;
14071463 } else if gui_script {
14081464 return Ok ( Self :: PythonGuiScript ( target. clone ( ) . into ( ) , args. to_vec ( ) ) ) ;
1465+ } else if script {
1466+ return Ok ( Self :: PythonScript ( target. clone ( ) . into ( ) , args. to_vec ( ) ) ) ;
14091467 }
14101468
14111469 let metadata = target_path. metadata ( ) ;
14121470 let is_file = metadata. as_ref ( ) . map_or ( false , std:: fs:: Metadata :: is_file) ;
14131471 let is_dir = metadata. as_ref ( ) . map_or ( false , std:: fs:: Metadata :: is_dir) ;
14141472
1415- if target. eq_ignore_ascii_case ( "-" ) {
1416- let mut buf = Vec :: with_capacity ( 1024 ) ;
1417- std:: io:: stdin ( ) . read_to_end ( & mut buf) ?;
1418- Ok ( Self :: PythonStdin ( buf, args. to_vec ( ) ) )
1419- } else if target. eq_ignore_ascii_case ( "python" ) {
1473+ if target. eq_ignore_ascii_case ( "python" ) {
14201474 Ok ( Self :: Python ( args. to_vec ( ) ) )
14211475 } else if target_path
14221476 . extension ( )
0 commit comments