From a99b61456e8a9317e7ac6f0532a20937ebb5b8c8 Mon Sep 17 00:00:00 2001 From: Matthew Rocklin Date: Tue, 2 May 2017 11:01:08 -0400 Subject: [PATCH] Assume modules with __file__ attribute are not dynamic Fixes https://github.com/cloudpipe/cloudpickle/issues/84 If a module has a `__file__` attribute then we assume that it is not dynamically generated. This allows the common case to be much faster than otherwise. --- cloudpickle/cloudpickle.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index fb40342e8..ed137aefe 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -176,11 +176,14 @@ def save_module(self, obj): """ mod_name = obj.__name__ # If module is successfully found then it is not a dynamically created module - try: - _find_module(mod_name) + if hasattr(obj, '__file__'): is_dynamic = False - except ImportError: - is_dynamic = True + else: + try: + _find_module(mod_name) + is_dynamic = False + except ImportError: + is_dynamic = True self.modules.add(obj) if is_dynamic: