Skip to content

Commit

Permalink
Code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldoussoren committed Sep 25, 2023
1 parent c13c1b9 commit 466e6ea
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
56 changes: 35 additions & 21 deletions modulegraph/_imp.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
try:
from imp import find_module, PY_SOURCE, PY_COMPILED, C_EXTENSION, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN, get_magic, get_suffixes
from imp import (
C_BUILTIN,
C_EXTENSION,
PKG_DIRECTORY,
PY_COMPILED,
PY_FROZEN,
PY_SOURCE,
find_module,
get_magic,
get_suffixes,
)

except ImportError:
# Compatibility stub for Python 3.12 or later.
#
#
# The code below was copied from the Python 3.10 standard library with
# some minor edits.
#
# As such the license for this code is the Python license:
# https://docs.python.org/3/license.html

import sys
import os
import importlib.util
import importlib.machinery
import importlib.util
import os
import sys
import tokenize
from importlib._bootstrap import _ERR_MSG

from _imp import is_builtin, is_frozen
import tokenize

SEARCH_ERROR = 0
PY_SOURCE = 1
Expand All @@ -32,45 +43,48 @@
def get_magic():
return importlib.util.MAGIC_NUMBER


def get_suffixes():
extensions = [(s, 'rb', C_EXTENSION) for s in importlib.machinery.EXTENSION_SUFFIXES]
source = [(s, 'r', PY_SOURCE) for s in importlib.machinery.SOURCE_SUFFIXES]
bytecode = [(s, 'rb', PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]

extensions = [
(s, "rb", C_EXTENSION) for s in importlib.machinery.EXTENSION_SUFFIXES
]
source = [(s, "r", PY_SOURCE) for s in importlib.machinery.SOURCE_SUFFIXES]
bytecode = [
(s, "rb", PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES
]

return extensions + source + bytecode

def find_module(name, path=None):
if path is None:
if is_builtin(name):
return (None, None, ('', '', C_BUILTIN))
return (None, None, ("", "", C_BUILTIN))
elif is_frozen(name):
return (None, None, ('', '', PY_FROZEN))
return (None, None, ("", "", PY_FROZEN))
else:
path = sys.path

for entry in path:
package_directory = os.path.join(entry, name)
for suffix in ['.py', importlib.machinery.BYTECODE_SUFFIXES[0]]:
package_file_name = '__init__' + suffix
for suffix in [".py", importlib.machinery.BYTECODE_SUFFIXES[0]]:
package_file_name = "__init__" + suffix
file_path = os.path.join(package_directory, package_file_name)
if os.path.isfile(file_path):
return None, package_directory, ('', '', PKG_DIRECTORY)
return None, package_directory, ("", "", PKG_DIRECTORY)

for suffix, mode, type_ in get_suffixes():
file_name = name + suffix
file_path = os.path.join(entry, file_name)
if os.path.isfile(file_path):
if os.path.isfile(file_path):
break
else:
continue
break # Break out of outer loop when breaking out of inner loop.
else:
raise ImportError(_ERR_MSG.format(name), name=name)
raise ImportError(_ERR_MSG.format(name), name=name)

encoding = None
if 'b' not in mode:
with open(file_path, 'rb') as file:
if "b" not in mode:
with open(file_path, "rb") as file:
encoding = tokenize.detect_encoding(file.readline)[0]
file = open(file_path, mode, encoding=encoding)
return file, file_path, (suffix, mode, type_)
return file, file_path, (suffix, mode, type_)
4 changes: 1 addition & 3 deletions modulegraph/find_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import sys
import warnings

from . import _imp as imp
from . import modulegraph
from .modulegraph import Alias, Extension, Script
from .util import imp_find_module

from . import _imp as imp

__all__ = ["find_modules", "parse_mf_results"]

_PLATFORM_MODULES = {"posix", "nt", "os2", "mac", "ce", "riscos"}
Expand Down Expand Up @@ -284,7 +283,6 @@ def find_needed_modules(
]



#
# side-effects
#
Expand Down
8 changes: 4 additions & 4 deletions modulegraph/modulegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from altgraph.ObjectGraph import ObjectGraph

from . import _imp as imp

from . import util, zipio

if sys.version_info[0] == 2:
Expand Down Expand Up @@ -174,6 +173,7 @@ def _code_to_file(co):
"""Convert code object to a .pyc pseudo-file"""
return BytesIO(imp.get_magic() + b"\0\0\0\0" + marshal.dumps(co))


def find_module(name, path=None):
"""
Get a 3-tuple detailing the physical location of the Python module with
Expand Down Expand Up @@ -279,11 +279,11 @@ def find_module(name, path=None):
else:
for sfx in importlib.machinery.EXTENSION_SUFFIXES:
if filename.endswith(sfx):
description = (_sfx, "rb", imp.C_EXTENSION)
break
description = (_sfx, "rb", imp.C_EXTENSION)
break
else:
raise RuntimeError("Don't know how to handle %r" % (importer,))

return (None, filename, description)

elif isinstance(importer, ImpImporter):
Expand Down
5 changes: 3 additions & 2 deletions modulegraph/util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import absolute_import

from . import _imp as imp
import marshal
import os
import re
import sys
import warnings

from . import _imp as imp

try:
unicode
except NameError:
Expand Down Expand Up @@ -114,7 +115,7 @@ def imp_walk(name):
raise ImportError("No module named %s" % (name,))


cookie_re = re.compile(br"coding[:=]\s*([-\w.]+)")
cookie_re = re.compile(rb"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
default_encoding = "ascii"
else:
Expand Down

0 comments on commit 466e6ea

Please sign in to comment.