Skip to content

Commit

Permalink
Fix importlib use on Py27
Browse files Browse the repository at this point in the history
Also fixes bug in `imp`-based initialization
  • Loading branch information
etingof committed Apr 12, 2019
1 parent 5b903a7 commit 827a9a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
9 changes: 7 additions & 2 deletions pysmi/borrower/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
try:
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES

except Exception:
raise ImportError()

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]
SOURCE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_SOURCE]

from pysmi.borrower.base import AbstractBorrower

Expand Down
14 changes: 10 additions & 4 deletions pysmi/searcher/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES
try:
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES

except Exception:
raise ImportError()

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]
BYTECODE_SUFFIXES = [imp.PY_COMPILED]
SOURCE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_SOURCE]
BYTECODE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_COMPILED]

from pysmi.searcher.base import AbstractSearcher
from pysmi.compat import decode
Expand Down
16 changes: 11 additions & 5 deletions pysmi/searcher/pypackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
try:
import importlib

PY_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES
try:
PY_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES

except Exception:
raise ImportError()

except ImportError:
import imp

PY_MAGIC_NUMBER = imp.get_magic()
SOURCE_SUFFIXES = [imp.PY_SOURCE]
BYTECODE_SUFFIXES = [imp.PY_COMPILED]
SOURCE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_SOURCE]
BYTECODE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_COMPILED]

from pysmi.searcher.base import AbstractSearcher
from pysmi.searcher.pyfile import PyFileSearcher
Expand Down
10 changes: 8 additions & 2 deletions pysmi/writer/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
import sys
import tempfile
import py_compile

try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
try:
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES

except Exception:
raise ImportError()

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]
SOURCE_SUFFIXES = [s[0] for s in imp.get_suffixes()
if s[2] == imp.PY_SOURCE]

from pysmi.writer.base import AbstractWriter
from pysmi.compat import encode, decode
Expand Down

0 comments on commit 827a9a3

Please sign in to comment.