1

I have a problem to run ndarray.dot(array-like object) either in Python 2.7 and 3.4 in exec() function - python hangs and I need to close it. Based on Numpy "dot" hangs or multiprocessing with numpy quits Python it is a known issue. When multiprocessing library is used, python hangs because of the deadlock.

How the code below behaves using exec()? Does exec() create a new thread? Is there any solution to fix it?

The actual code is more complex, so I need to use exec().

test_dot.py:

test_global_env = {
    '__builtins__': None,
    'np': numpy
}
test_local_env = {}
test = '''
result = np.array([[1, 2], [3, 4]]).dot([0.14, 0.1])
'''
if __name__ == '__main__':
    exec(test, test_global_env, test_local_env)

Thank you.

Community
  • 1
  • 1
regiea
  • 121
  • 1
  • 5
  • 18

1 Answers1

1

This is probably problem with line '__builtins__': None,. If you comment this line out, your code will work just fine. Btw. what was the purpose of this line?

exec does not create another thread, it executes in the current thread and any other function. The rest of the code simply waits for exec to be finished before is continues.

  • Great, you are right. Since I am creating a "IDE"/"sandbox" to run miscellaneous python code, I can't allow the user to use every function or class from builtins. I want to allow only functions like min, max, tuple, dict, range, iter, isinstance, enumerate, iter. But don't want to allow __import__. – regiea Jan 18 '17 at 14:27
  • Well, I am not sure if everything will work, what if the code you call will need some import in background in order to run (I mean what if for example `np.dot()` imports something)? – HiFile.app - best file manager Jan 18 '17 at 16:03