Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13a7a21

Browse files
committed
Issue #1762972: Readded the reload() function as imp.reload()
1 parent 8de8e03 commit 13a7a21

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lib/test/test_imp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def test_issue1267(self):
6060
'"""Tokenization help for Python programs.\n')
6161
fp.close()
6262

63+
def test_reload(self):
64+
import marshal
65+
imp.reload(marshal)
66+
import string
67+
imp.reload(string)
68+
## import sys
69+
## self.assertRaises(ImportError, reload, sys)
70+
6371

6472
def test_main():
6573
test_support.run_unittest(

Lib/test/test_import.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import py_compile
99
import warnings
10+
import imp
1011
from test.test_support import unlink, TESTFN, unload
1112

1213

@@ -160,6 +161,45 @@ def test_import_initless_directory_warning(self):
160161
warnings.simplefilter('error', ImportWarning)
161162
self.assertRaises(ImportWarning, __import__, "site-packages")
162163

164+
def test_failing_reload(self):
165+
# A failing reload should leave the module object in sys.modules.
166+
source = TESTFN + ".py"
167+
with open(source, "w") as f:
168+
f.write("a = 1\nb=2\n")
169+
170+
sys.path.insert(0, os.curdir)
171+
try:
172+
mod = __import__(TESTFN)
173+
self.assert_(TESTFN in sys.modules, "expected module in sys.modules")
174+
self.assertEquals(mod.a, 1, "module has wrong attribute values")
175+
self.assertEquals(mod.b, 2, "module has wrong attribute values")
176+
177+
# On WinXP, just replacing the .py file wasn't enough to
178+
# convince reload() to reparse it. Maybe the timestamp didn't
179+
# move enough. We force it to get reparsed by removing the
180+
# compiled file too.
181+
remove_files(TESTFN)
182+
183+
# Now damage the module.
184+
with open(source, "w") as f:
185+
f.write("a = 10\nb=20//0\n")
186+
187+
self.assertRaises(ZeroDivisionError, imp.reload, mod)
188+
# But we still expect the module to be in sys.modules.
189+
mod = sys.modules.get(TESTFN)
190+
self.failIf(mod is None, "expected module to still be in sys.modules")
191+
192+
# We should have replaced a w/ 10, but the old b value should
193+
# stick.
194+
self.assertEquals(mod.a, 10, "module has wrong attribute values")
195+
self.assertEquals(mod.b, 2, "module has wrong attribute values")
196+
197+
finally:
198+
sys.path.pop(0)
199+
remove_files(TESTFN)
200+
if TESTFN in sys.modules:
201+
del sys.modules[TESTFN]
202+
163203
class PathsTests(unittest.TestCase):
164204
SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
165205
'test\u00b0\u00b3\u00b2')

Python/import.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,6 +2931,17 @@ imp_new_module(PyObject *self, PyObject *args)
29312931
return PyModule_New(name);
29322932
}
29332933

2934+
static PyObject *
2935+
imp_reload(PyObject *self, PyObject *v)
2936+
{
2937+
return PyImport_ReloadModule(v);
2938+
}
2939+
2940+
PyDoc_STRVAR(doc_reload,
2941+
"reload(module) -> module\n\
2942+
\n\
2943+
Reload the module. The module must have been successfully imported before.");
2944+
29342945
/* Doc strings */
29352946

29362947
PyDoc_STRVAR(doc_imp,
@@ -2989,6 +3000,7 @@ static PyMethodDef imp_methods[] = {
29893000
{"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
29903001
{"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
29913002
{"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3003+
{"reload", imp_reload, METH_O, doc_reload},
29923004
/* The rest are obsolete */
29933005
{"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
29943006
{"init_builtin", imp_init_builtin, METH_VARARGS},

0 commit comments

Comments
 (0)