Commit 7eadf62c by Alain Takoudjou

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.
1 parent 36698874
......@@ -198,7 +198,7 @@ class PromiseProcess(Process):
raise RuntimeError("RunPromise class is not a subclass of " \
"GenericPromise class.")
if promise_module.__file__ != self.promise_path:
if 'py'.join(promise_module.__file__.rsplit('pyc')) != self.promise_path:
# cached module need to be updated
promise_module = reload_module(promise_module)
# load extra parameters
......
  • 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')
    
  • 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.

Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!