@@ -577,81 +577,81 @@ run_guile_cli (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **ar
577
577
}
578
578
579
579
580
+ #ifdef HAVE_PYTHON_H
581
+ static void
582
+ python_cleanup (PyConfig& config, PyStatus& status)
583
+ {
584
+ if (qof_book_session_not_saved (gnc_get_current_book ()))
585
+ std::cerr << _ (" Book is readonly. Unsaved changes will be lost." ) << std::endl;
586
+ gnc_clear_current_session ();
587
+
588
+ PyConfig_Clear (&config);
589
+ if (PyStatus_IsExit (status))
590
+ gnc_shutdown_cli (status.exitcode );
591
+
592
+ Py_ExitStatusException (status);
593
+ }
594
+
580
595
static void
581
596
run_python_cli (int argc, char **argv, scripting_args* args)
582
597
{
583
- #ifdef HAVE_PYTHON_H
584
598
PyConfig config;
585
599
PyConfig_InitPythonConfig (&config);
586
600
587
601
PyStatus status = PyConfig_SetBytesArgv (&config, argc, argv);
588
- try
589
- {
590
- if (PyStatus_Exception (status))
591
- throw ;
602
+ if (PyStatus_Exception (status))
603
+ python_cleanup (config, status);
592
604
593
- status = Py_InitializeFromConfig (&config);
594
- if (PyStatus_Exception (status))
595
- throw ;
605
+ status = Py_InitializeFromConfig (&config);
606
+ if (PyStatus_Exception (status))
607
+ python_cleanup (config, status) ;
596
608
597
- PyConfig_Clear (&config);
609
+ PyConfig_Clear (&config);
598
610
599
- if (args->script )
600
- {
601
- auto script_filename = args->script ->c_str ();
602
- PINFO (" Running python script %s..." , script_filename);
603
- auto fp = fopen (script_filename, " r" );
604
- if (fp)
605
- PyRun_SimpleFileEx (fp, script_filename, 1 );
606
- else
607
- std::cerr << bl::format (_ (" Unable to load Python script {1}" )) % script_filename
608
- << std::endl;
609
- }
610
- if (args->interactive )
611
- {
612
- std::cout << _ (" Welcome to Gnucash Interactive Python Session" ) << std::endl;
613
- PyRun_InteractiveLoop (stdin, " foo" );
614
- }
615
- Py_Finalize ();
616
- cleanup_and_exit_with_save ();
611
+ if (args->script )
612
+ {
613
+ auto script_filename = args->script ->c_str ();
614
+ PINFO (" Running python script %s..." , script_filename);
615
+ auto fp = fopen (script_filename, " rb" );
616
+ if (!fp)
617
+ std::cerr << bl::format (_ (" Unable to load Python script {1}" )) % script_filename
618
+ << std::endl;
619
+ else if (PyRun_SimpleFileEx (fp, script_filename, 1 ) != 0 )
620
+ std::cerr << bl::format (_ (" Python script {1} execution failed." )) % script_filename
621
+ << std::endl;
617
622
}
618
- catch ( const std:: exception & )
623
+ if (args-> interactive )
619
624
{
620
- if (qof_book_session_not_saved (gnc_get_current_book ()))
621
- std::cerr << _ (" Book is readonly. Unsaved changes will be lost." ) << std::endl;
622
- gnc_clear_current_session ();
623
-
624
- PyConfig_Clear (&config);
625
- if (PyStatus_IsExit (status))
626
- gnc_shutdown_cli (status.exitcode );
627
-
628
- Py_ExitStatusException (status);
625
+ std::cout << _ (" Welcome to Gnucash Interactive Python Session" ) << std::endl;
626
+ PyRun_InteractiveLoop (stdin, " foo" );
629
627
}
630
- #endif
628
+ Py_Finalize ();
629
+ cleanup_and_exit_with_save ();
631
630
}
631
+ #endif
632
632
633
633
int
634
- Gnucash::run_scripting (std::vector<std::string> m_script_args ,
635
- const bo_str& m_file_to_load ,
636
- std::string& m_language ,
637
- const bo_str& m_script ,
638
- bool m_open_readwrite ,
639
- bool m_interactive )
634
+ Gnucash::run_scripting (std::vector<std::string> script_args ,
635
+ const bo_str& file_to_load ,
636
+ std::string& language ,
637
+ const bo_str& script ,
638
+ bool open_readwrite ,
639
+ bool interactive )
640
640
{
641
641
std::vector<std::string> errors;
642
642
static const std::vector<std::string> languages = { " guile" , " python" };
643
643
644
- if (m_open_readwrite && !m_file_to_load )
644
+ if (open_readwrite && !file_to_load )
645
645
errors.push_back (_ (" --readwrite: missing datafile!" ));
646
646
647
- if (m_script && (!boost::filesystem::is_regular_file (*m_script )))
648
- errors.push_back ((bl::format (_ (" --script: {1} is not a file" )) % *m_script ).str ());
647
+ if (script && (!boost::filesystem::is_regular_file (*script )))
648
+ errors.push_back ((bl::format (_ (" --script: {1} is not a file" )) % *script ).str ());
649
649
650
650
if (std::none_of (languages.begin (), languages.end (),
651
- [&m_language ](auto & lang){ return m_language == lang; }))
651
+ [&language ](auto & lang){ return language == lang; }))
652
652
errors.push_back (_ (" --language: must be 'python' or 'guile'" ));
653
653
#ifndef HAVE_PYTHON_H
654
- else if (m_language == " python" )
654
+ else if (language == " python" )
655
655
errors.push_back (_ (" --language: python wasn't compiled in this build" ));
656
656
#endif
657
657
@@ -663,28 +663,30 @@ Gnucash::run_scripting (std::vector<std::string> m_script_args,
663
663
gnc_shutdown_cli (1 );
664
664
}
665
665
666
- std::vector<const char *> newArgv {" gnucash-cli" };
667
- std::transform (m_script_args.begin (), m_script_args.end (), std::back_inserter (newArgv),
666
+ std::vector<const char *> newArgv =
667
+ { " gnucash-cli" , file_to_load ? file_to_load->c_str () : " " };
668
+ std::transform (script_args.begin (), script_args.end (), std::back_inserter (newArgv),
668
669
[](const std::string& s) { return s.c_str (); });
669
- // note the vector<const char*> is valid as long as m_script_args 's strings are not damaged!
670
+ // note the vector<const char*> is valid as long as script_args 's strings are not damaged!
670
671
671
672
gnc_prefs_init ();
672
673
gnc_ui_util_init ();
673
- if (m_file_to_load )
674
- load_file (*m_file_to_load, m_open_readwrite );
674
+ if (file_to_load && boost::filesystem::is_regular_file (*file_to_load) )
675
+ load_file (*file_to_load, open_readwrite );
675
676
676
- scripting_args args { m_script, m_interactive };
677
- if (m_language == " guile" )
677
+ scripting_args args { script, interactive };
678
+ if (language == " guile" )
678
679
{
679
680
scm_boot_guile (newArgv.size (), (char **)newArgv.data (), run_guile_cli, &args);
680
681
return 0 ; // never reached...
681
682
}
682
- else if (m_language == " python" )
683
+ #ifdef HAVE_PYTHON_H
684
+ else if (language == " python" )
683
685
{
684
686
run_python_cli (newArgv.size (), (char **)newArgv.data (), &args);
685
687
return 0 ; // never reached...
686
688
}
687
-
689
+ # endif
688
690
689
691
return 0 ; // never reached
690
692
}
0 commit comments