From 56f7ef1ac26595cf671c4acf6babe95e2ee712c2 Mon Sep 17 00:00:00 2001 From: Adam Gleave Date: Thu, 17 May 2018 19:29:51 -0700 Subject: [PATCH 1/3] Use source code in hash where possible (fix #2089) --- python/ray/remote_function.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/python/ray/remote_function.py b/python/ray/remote_function.py index 4e09e4016b1d..65ec17f7f3c7 100644 --- a/python/ray/remote_function.py +++ b/python/ray/remote_function.py @@ -14,15 +14,6 @@ DEFAULT_REMOTE_FUNCTION_MAX_CALLS = 0 -def in_ipython(): - """Return true if we are in an IPython interpreter and false otherwise.""" - try: - __IPYTHON__ - return True - except NameError: - return False - - def compute_function_id(function): """Compute an function ID for a function. @@ -36,14 +27,14 @@ def compute_function_id(function): # Include the function module and name in the hash. function_id_hash.update(function.__module__.encode("ascii")) function_id_hash.update(function.__name__.encode("ascii")) - # If we are running a script or are in IPython, include the source code in - # the hash. If we are in a regular Python interpreter we skip this part - # because the source code is not accessible. If the function is a built-in - # (e.g., Cython), the source code is not accessible. - import __main__ as main - if (hasattr(main, "__file__") or in_ipython()) \ - and inspect.isfunction(function): - function_id_hash.update(inspect.getsource(function).encode("ascii")) + try: + # If we are running a script or are in IPython, include the source code + # in the hash. + source = inspect.getsource(function).encode("ascii") + function_id_hash.update(source) + except OSError: + # Source code may not be available: e.g. Cython or Python interpreter. + pass # Compute the function ID. function_id = function_id_hash.digest() assert len(function_id) == 20 From 56a36b65d7f2fb50195cbfd420cbba13293a870a Mon Sep 17 00:00:00 2001 From: Adam Gleave Date: Thu, 17 May 2018 22:19:04 -0700 Subject: [PATCH 2/3] Compatibility with Python<3.3 --- python/ray/remote_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/remote_function.py b/python/ray/remote_function.py index 65ec17f7f3c7..97c9645063e0 100644 --- a/python/ray/remote_function.py +++ b/python/ray/remote_function.py @@ -32,7 +32,7 @@ def compute_function_id(function): # in the hash. source = inspect.getsource(function).encode("ascii") function_id_hash.update(source) - except OSError: + except (IOError, OSError): # Source code may not be available: e.g. Cython or Python interpreter. pass # Compute the function ID. From b199f5b1b56aa0a6cfecc339d7433b78437fb72e Mon Sep 17 00:00:00 2001 From: Robert Nishihara Date: Thu, 17 May 2018 23:39:40 -0700 Subject: [PATCH 3/3] Fix for cython functions. --- python/ray/remote_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/remote_function.py b/python/ray/remote_function.py index 97c9645063e0..1bd254cf4796 100644 --- a/python/ray/remote_function.py +++ b/python/ray/remote_function.py @@ -32,7 +32,7 @@ def compute_function_id(function): # in the hash. source = inspect.getsource(function).encode("ascii") function_id_hash.update(source) - except (IOError, OSError): + except (IOError, OSError, TypeError): # Source code may not be available: e.g. Cython or Python interpreter. pass # Compute the function ID.