You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2018/08/16 14:05:08 UTC

[arrow] branch master updated: ARROW-3062: [Python] Fix python package finder to also work in Python 2.7

This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 51410bc  ARROW-3062: [Python] Fix python package finder to also work in Python 2.7
51410bc is described below

commit 51410bc17bf76f766ac94effb315bf038afb7479
Author: Philipp Moritz <pc...@gmail.com>
AuthorDate: Thu Aug 16 10:05:00 2018 -0400

    ARROW-3062: [Python] Fix python package finder to also work in Python 2.7
    
    Author: Philipp Moritz <pc...@gmail.com>
    
    Closes #2435 from pcmoritz/fix-python-pkg-finder and squashes the following commits:
    
    3c6ccfe9 <Philipp Moritz> Update compat.py
    b1cd26a2 <Philipp Moritz> fix
    1ef2a3fb <Philipp Moritz> fix _iterate_python_module_paths for Python 2
---
 python/pyarrow/compat.py | 57 ++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/python/pyarrow/compat.py b/python/pyarrow/compat.py
index 85ad91e..16e8ce6 100644
--- a/python/pyarrow/compat.py
+++ b/python/pyarrow/compat.py
@@ -164,34 +164,43 @@ def _iterate_python_module_paths(package_name):
     """
     Return an iterator to full paths of a python package.
 
-    This is a best effort and might fail (for example on Python 2).
+    This is a best effort and might fail.
     It uses the official way of loading modules from
     https://docs.python.org/3/library/importlib.html#approximating-importlib-import-module
     """
-    try:
-        import importlib
-        absolute_name = importlib.util.resolve_name(package_name, None)
-    except (ImportError, AttributeError):
-        # Sometimes, importlib is not available (e.g. Python 2)
-        # or importlib.util is not available (e.g. Python 2.7)
-        spec = None
+    if PY2:
+        import imp
+        try:
+            _, pathname, _ = imp.find_module(package_name)
+        except ImportError:
+            return
+        else:
+            yield pathname
     else:
-        import sys
-        for finder in sys.meta_path:
-            try:
-                spec = finder.find_spec(absolute_name, None)
-            except AttributeError:
-                # On Travis (Python 3.5) the above produced:
-                # AttributeError: 'VendorImporter' object has no
-                # attribute 'find_spec'
-                spec = None
-            if spec is not None:
-                break
-
-    if spec:
-        module = importlib.util.module_from_spec(spec)
-        for path in module.__path__:
-            yield path
+        try:
+            import importlib
+            absolute_name = importlib.util.resolve_name(package_name, None)
+        except (ImportError, AttributeError):
+            # Sometimes, importlib is not available (e.g. Python 2)
+            # or importlib.util is not available (e.g. Python 2.7)
+            spec = None
+        else:
+            import sys
+            for finder in sys.meta_path:
+                try:
+                    spec = finder.find_spec(absolute_name, None)
+                except AttributeError:
+                    # On Travis (Python 3.5) the above produced:
+                    # AttributeError: 'VendorImporter' object has no
+                    # attribute 'find_spec'
+                    spec = None
+                if spec is not None:
+                    break
+
+        if spec:
+            module = importlib.util.module_from_spec(spec)
+            for path in module.__path__:
+                yield path
 
 def import_tensorflow_extension():
     """