Added more exception handling in c_compiler_cmd
If there is any error, even writing the simple.cpp file, catch it and just return a "I don't know" diagnosis
This commit is contained in:
118
week4/day4.ipynb
118
week4/day4.ipynb
@@ -455,7 +455,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 111,
|
||||||
"id": "e42286bc-085c-45dc-b101-234308e58269",
|
"id": "e42286bc-085c-45dc-b101-234308e58269",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -482,75 +482,75 @@
|
|||||||
" return \"\"\n",
|
" return \"\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def c_compiler_cmd(filename_base):\n",
|
"def c_compiler_cmd(filename_base):\n",
|
||||||
"\n",
|
|
||||||
" with open(\"simple.cpp\", \"w\") as f:\n",
|
|
||||||
" f.write(simple_cpp)\n",
|
|
||||||
"\n",
|
|
||||||
" \n",
|
|
||||||
" my_platform = platform.system()\n",
|
" my_platform = platform.system()\n",
|
||||||
" my_compiler = []\n",
|
" my_compiler = []\n",
|
||||||
" \n",
|
"\n",
|
||||||
" \n",
|
" try:\n",
|
||||||
" if my_platform == \"Windows\":\n",
|
" with open(\"simple.cpp\", \"w\") as f:\n",
|
||||||
" if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
|
" f.write(simple_cpp)\n",
|
||||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
" \n",
|
||||||
" os.remove(\"./simple.exe\")\n",
|
" if my_platform == \"Windows\":\n",
|
||||||
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
" if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
|
||||||
" my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
|
||||||
" \n",
|
|
||||||
" if not my_compiler:\n",
|
|
||||||
" if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
|
|
||||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||||
" os.remove(\"./simple.exe\")\n",
|
" os.remove(\"./simple.exe\")\n",
|
||||||
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
" if run_cmd(compile_cmd):\n",
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
" my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
" my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
||||||
"\n",
|
" \n",
|
||||||
" if not my_compiler:\n",
|
" if not my_compiler:\n",
|
||||||
" if os.path.isfile(\"./simple.exe\"):\n",
|
" if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
|
||||||
" os.remove(\"./simple.exe\")\n",
|
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||||
" compile_cmd = [\"cmd\", \"/c\", \"gcc\", \"simple.cpp\"]\n",
|
" os.remove(\"./simple.exe\")\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
|
||||||
" print(\"here\")\n",
|
" if run_cmd(compile_cmd):\n",
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
" my_compiler = [\"Windows\", \"GCC\", [\"cmd\", \"/c\", \"gcc\", f\"{filename_base}.cpp\"]]\n",
|
" my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
|
||||||
"\n",
|
" \n",
|
||||||
" if not my_compiler:\n",
|
" if not my_compiler:\n",
|
||||||
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
" if os.path.isfile(\"./simple.exe\"):\n",
|
||||||
" \n",
|
" os.remove(\"./simple.exe\")\n",
|
||||||
" elif my_platform == \"Linux\":\n",
|
" compile_cmd = [\"cmd\", \"/c\", \"gcc\", \"simple.cpp\"]\n",
|
||||||
" if os.path.isfile(\"./simple\"):\n",
|
" if run_cmd(compile_cmd):\n",
|
||||||
" os.remove(\"./simple\")\n",
|
" print(\"here\")\n",
|
||||||
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
" my_compiler = [\"Windows\", \"GCC\", [\"cmd\", \"/c\", \"gcc\", f\"{filename_base}.cpp\"]]\n",
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
" \n",
|
||||||
" my_compiler = [\"Linux\", \"GCC\", [\"gcc\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
" if not my_compiler:\n",
|
||||||
"\n",
|
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
||||||
" if not my_compiler:\n",
|
" \n",
|
||||||
|
" elif my_platform == \"Linux\":\n",
|
||||||
" if os.path.isfile(\"./simple\"):\n",
|
" if os.path.isfile(\"./simple\"):\n",
|
||||||
" os.remove(\"./simple\")\n",
|
" os.remove(\"./simple\")\n",
|
||||||
" compile_cmd = [\"clang\", \"simple.cpp\", \"-o\", \"simple\"]\n",
|
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
" if run_cmd(compile_cmd):\n",
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
" my_compiler = [\"Linux\", \"CLang\", [\"clang\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
" my_compiler = [\"Linux\", \"GCC\", [\"gcc\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
||||||
" \n",
|
" \n",
|
||||||
" if not my_compiler:\n",
|
" if not my_compiler:\n",
|
||||||
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
" if os.path.isfile(\"./simple\"):\n",
|
||||||
"\n",
|
" os.remove(\"./simple\")\n",
|
||||||
" elif my_platform == \"Darwin\":\n",
|
" compile_cmd = [\"clang\", \"simple.cpp\", \"-o\", \"simple\"]\n",
|
||||||
" if os.path.isfile(\"./simple\"):\n",
|
" if run_cmd(compile_cmd):\n",
|
||||||
" os.remove(\"./simple\")\n",
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
" my_compiler = [\"Linux\", \"CLang\", [\"clang\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
|
||||||
" if run_cmd(compile_cmd):\n",
|
" \n",
|
||||||
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
" if not my_compiler:\n",
|
||||||
" my_compiler = [\"Linux\", \"CLang\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
|
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
||||||
"\n",
|
" \n",
|
||||||
" if not my_compiler:\n",
|
" elif my_platform == \"Darwin\":\n",
|
||||||
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
" if os.path.isfile(\"./simple\"):\n",
|
||||||
"\n",
|
" os.remove(\"./simple\")\n",
|
||||||
|
" compile_cmd = [\"gcc\", \"simple.cpp\"]\n",
|
||||||
|
" if run_cmd(compile_cmd):\n",
|
||||||
|
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
|
||||||
|
" my_compiler = [\"Linux\", \"CLang\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
|
||||||
|
" \n",
|
||||||
|
" if not my_compiler:\n",
|
||||||
|
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
||||||
|
" except:\n",
|
||||||
|
" my_compiler=[my_platform, \"Unavailable\", []]\n",
|
||||||
|
" \n",
|
||||||
" if my_compiler:\n",
|
" if my_compiler:\n",
|
||||||
" return my_compiler\n",
|
" return my_compiler\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
|
|||||||
Reference in New Issue
Block a user