2424#define PY_WINDOWED 0
2525#endif
2626
27+ // Uncomment to add the --__fix-cwd private argument, which will reset the
28+ // working directory to the script location or user documents/profile before
29+ // running Python.
30+ // #define ENABLE_FIX_CWD
31+
2732struct {
2833 PyObject *mod;
2934 PyObject *no_install_found_error;
@@ -484,6 +489,7 @@ locate_runtime(
484489}
485490
486491
492+ #ifdef ENABLE_FIX_CWD
487493static int
488494fix_working_directory (const std::wstring &script)
489495{
@@ -512,6 +518,7 @@ fix_working_directory(const std::wstring &script)
512518 }
513519 return hr;
514520}
521+ #endif
515522
516523
517524int
@@ -535,7 +542,9 @@ wmain(int argc, wchar_t **argv)
535542
536543 const wchar_t *default_cmd;
537544 bool use_commands, use_cli_tag, use_shebangs, use_autoinstall;
545+ #ifdef ENABLE_FIX_CWD
538546 bool fix_cwd = false ;
547+ #endif
539548 per_exe_settings (argc, argv, &default_cmd, &use_commands, &use_cli_tag, &use_shebangs, &use_autoinstall);
540549
541550 if (use_commands) {
@@ -551,10 +560,12 @@ wmain(int argc, wchar_t **argv)
551560 // We handle 'exec' in native code, so it won't be in the above list
552561 if (!wcscmp (argv[1 ], L" exec" )) {
553562 skip_argc += 1 ;
563+ #ifdef ENABLE_FIX_CWD
554564 if (!wcscmp (argv[2 ], L" --__fix-cwd" )) {
555565 fix_cwd = true ;
556566 skip_argc += 1 ;
557567 }
568+ #endif
558569 use_cli_tag = argc >= 3 ;
559570 use_shebangs = argc >= 3 ;
560571 default_cmd = NULL ;
@@ -606,12 +617,14 @@ wmain(int argc, wchar_t **argv)
606617 // Theoretically shouldn't matter, but might help reduce memory usage.
607618 close_python ();
608619
620+ #ifdef ENABLE_FIX_CWD
609621 if (fix_cwd) {
610622 err = fix_working_directory (script);
611623 if (err) {
612624 fprintf (stderr, " [WARN] Failed to fix working directory (0x%08X).\n " , err);
613625 }
614626 }
627+ #endif
615628
616629 err = launch (executable.c_str (), args.c_str (), skip_argc, &exitCode);
617630
0 commit comments