Test for try_import with reload(sys) (PY-14649, PY-14779)

This commit is contained in:
Elizaveta Shashkova
2015-01-15 20:50:41 +03:00
parent 069859f4df
commit 61ff694d65
2 changed files with 52 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
import sys
try:
# Python2
reload(sys)
except:
# Python3
import importlib
importlib.reload(sys)
def try_import(module_name):
"""
Import and return *module_name*.
Unlike the standard try/except approach to optional imports, inspect
the stack to avoid catching ImportErrors raised from **within** the
module. Only return None if *module_name* itself cannot be imported.
It is used in rapidsms, django-oscar and may be somewhere else.
"""
try:
__import__(module_name)
return sys.modules[module_name]
except ImportError:
type, info, traceback = sys.exc_info()
if traceback.tb_next:
raise
return None
result = try_import("datetime")
result = try_import("nonexistent_module")
result = None
@@ -630,6 +630,27 @@ public class PythonDebuggerTest extends PyEnvTestCase {
});
}
public void testTryImportWithReload() throws Exception {
runPythonTest(new PyDebuggerTask("/debug", "test_try_import.py") {
@Override
public void before() throws Exception {
toggleBreakpoint(getScriptPath(), 29);
toggleBreakpoint(getScriptPath(), 30);
}
@Override
public void testing() throws Exception {
waitForPause();
eval("result.__name__").hasValue("'datetime'");
resume();
waitForPause();
eval("result").hasValue("None");
resume();
}
});
}
//TODO: fix me as I don't work properly sometimes (something connected with process termination on agent)
//public void testResume() throws Exception {