// examples/test_io_os_error translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "Error closing descriptor %d"); GLOBAL_STR(str1, ""); GLOBAL_STR(str2, "osh I/O error (main)"); GLOBAL_STR(str3, "TerminalControl init"); GLOBAL_STR(str4, "TerminalControl exit"); GLOBAL_STR(str5, "Throw and Catch within destructor seems OK"); GLOBAL_STR(str6, "hi"); GLOBAL_STR(str7, ""); GLOBAL_STR(str8, "TestDestructor"); GLOBAL_STR(str9, ""); namespace test_io_os_error { // forward declare class ctx_TerminalControl; } // forward declare namespace test_io_os_error namespace test_io_os_error { // declare void Close(int fd); void Pop(int fd); int AppBundleMain(List* argv); int TestRethrow(); class ctx_TerminalControl { public: ctx_TerminalControl(); ~ctx_TerminalControl(); DISALLOW_COPY_AND_ASSIGN(ctx_TerminalControl) }; void TestDestructor(); int run_tests(); void run_benchmarks(); } // declare namespace test_io_os_error namespace test_io_os_error { // define using mylib::print_stderr; void Close(int fd) { throw Alloc(0); } void Pop(int fd) { try { Close(fd); } catch (IOError_OSError* e) { mylib::print_stderr(StrFormat("Error closing descriptor %d", fd)); throw ; } } int AppBundleMain(List* argv) { StackRoot _root0(&argv); Pop(3); return 0; } int TestRethrow() { List* argv = nullptr; StackRoot _root0(&argv); argv = Alloc>(); try { return AppBundleMain(argv); } catch (ValueError* e) { return 2; } catch (KeyboardInterrupt*) { print(str1); return 130; } catch (IOError_OSError* e) { print_stderr(str2); return 2; } } ctx_TerminalControl::ctx_TerminalControl() { mylib::print_stderr(str3); } ctx_TerminalControl::~ctx_TerminalControl() { mylib::print_stderr(str4); TestRethrow(); mylib::print_stderr(str5); } void TestDestructor() { { // with ctx_TerminalControl ctx{}; mylib::print_stderr(str6); } } int run_tests() { TestRethrow(); mylib::print_stderr(str7); mylib::print_stderr(str8); mylib::print_stderr(str9); TestDestructor(); return 0; } void run_benchmarks() { FAIL(kNotImplemented); // Python NotImplementedError } } // define namespace test_io_os_error int main(int argc, char **argv) { gHeap.Init(); char* b = getenv("BENCHMARK"); if (b && strlen(b)) { // match Python's logic fprintf(stderr, "Benchmarking...\n"); test_io_os_error::run_benchmarks(); } else { test_io_os_error::run_tests(); } gHeap.CleanProcessExit(); }