grid.promise: do not always reload promise module
The reload part is mainly used for tests, module is reloaded if the promise file has changed. Here, __file__ ends with pyc when promise_path ends with py.
Showing
-
Developer
Wouldn't it fail if the path contains
pyc
? If this code really does what I think it does (I may be wrong, I don't have a full picture of the context), using os.path.splitext will be safer.For exemple :
>>> 'py'.join('aaa/bbb/pyc.pyc'.split('pyc')) 'aaa/bbb/py.py' >>> os.path.splitext('aaa/bbb/pyc.pyc') ('aaa/bbb/pyc', '.pyc')
-
Maintainer
The file is loaded before as a python module so it's a .py. If the file is
aaa/bbb/pyc.pyc
it will raise before reaches this if condition. You can see bellow:$ ls __init__.py __init__.pyc test.py
then in a python console:
>>> import os, importlib >>> promise_module = importlib.import_module(os.path.splitext("testpyc")[0]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named testpyc
I try with
test.pyc
$ mv testpyc test.pyc >>> promise_module = importlib.import_module(os.path.splitext("test.pyc")[0]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: Bad magic number in test.pyc
A file
test.py
is accepted$ mv test.pyc test.py >>> promise_module = importlib.import_module(os.path.splitext("test.py")[0]) <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'> >>> promise_module.testing() yes $ ls test* test.py test.pyc
I can change anyway to use splitext if you think it's better.
Please register or sign in to comment