Skip to content

Commit

Permalink
Make Hy a Python-source module type
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Aug 26, 2018
1 parent 32033b0 commit d1236bc
Showing 1 changed file with 9 additions and 53 deletions.
62 changes: 9 additions & 53 deletions hy/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,15 @@ def _hy_source_to_code(self, data, path, _optimize=-1):

from pkgutil import ImpImporter, ImpLoader

# 100x better!
HY_SOURCE = imp.PY_SOURCE * 100

class HyLoader(ImpLoader, object):
def __init__(self, fullname, filename, fileobj=None, etc=None):
"""This constructor is designed for some compatibility with
SourceFileLoader."""
if etc is None and filename is not None:
if filename.endswith('.hy'):
etc = ('.hy', 'U', HY_SOURCE)
if fileobj is None:
fileobj = open(filename, 'rU')
etc = ('.hy', 'U', imp.PY_SOURCE)
if fileobj is None:
fileobj = io.open(filename, 'rU', encoding='utf-8')

super(HyLoader, self).__init__(fullname, fileobj, filename, etc)

Expand All @@ -266,10 +263,11 @@ def load_module(self, fullname=None):
"""Same as `pkgutil.ImpLoader`, with an extra check for Hy
source"""
fullname = self._fix_name(fullname)
ext_type = self.etc[0]
mod_type = self.etc[2]
mod = None
pkg_path = os.path.join(self.filename, '__init__.hy')
if mod_type == HY_SOURCE or (
if ext_type == '.hy' or (
mod_type == imp.PKG_DIRECTORY and
os.path.isfile(pkg_path)):

Expand Down Expand Up @@ -306,17 +304,6 @@ def load_module(self, fullname=None):
mod.__loader__ = self
return mod

def _reopen(self):
"""Same as `pkgutil.ImpLoader`, with an extra check for Hy
source"""
super(HyLoader, self)._reopen()

# Add the Hy case...
if self.file and self.file.closed:
mod_type = self.etc[2]
if mod_type == HY_SOURCE:
self.file = io.open(self.filename, 'rU', encoding='utf-8')

def byte_compile_hy(self, fullname=None):
fullname = self._fix_name(fullname)
if fullname is None:
Expand Down Expand Up @@ -344,8 +331,8 @@ def get_code(self, fullname=None):
"""Same as `pkgutil.ImpLoader`, with an extra check for Hy
source"""
fullname = self._fix_name(fullname)
mod_type = self.etc[2]
if mod_type == HY_SOURCE:
ext_type = self.etc[0]
if ext_type == '.hy':
# Looks like we have to manually check for--and update--
# the bytecode.
t_py = long(os.stat(self.filename).st_mtime)
Expand All @@ -370,40 +357,10 @@ def get_code(self, fullname=None):

return self.code

def get_source(self, fullname=None):
"""Same as `pkgutil.ImpLoader`, with an extra check for Hy
source"""
fullname = self._fix_name(fullname)
mod_type = self.etc[2]
if self.source is None and mod_type == HY_SOURCE:
self._reopen()
try:
self.source = self.file.read()
finally:
self.file.close()

if self.source is None:
super(HyLoader, self).get_source(fullname=fullname)

return self.source

def get_filename(self, fullname=None):
"""Same as `pkgutil.ImpLoader`, with an extra check for Hy
source"""
fullname = self._fix_name(fullname)
if self.etc[2] == HY_SOURCE:
return self.filename

if self.filename is None:
filename = super(HyLoader, self).get_filename(fullname=fullname)

return filename

def _get_delegate(self):
return HyImporter(self.filename).find_module('__init__')

class HyImporter(ImpImporter, object):

def __init__(self, path=None):
# We need to be strict about the types of files this importer will
# handle. To start, if the path is not the current directory in
Expand Down Expand Up @@ -444,7 +401,7 @@ def find_module(self, fullname, path=None):
entry.endswith('.hy')):
file_path = entry
fileobj = io.open(file_path, 'rU', encoding='utf-8')
etc = ('.hy', 'U', HY_SOURCE)
etc = ('.hy', 'U', imp.PY_SOURCE)
break
else:
file_path = os.path.join(entry, subname)
Expand All @@ -457,15 +414,14 @@ def find_module(self, fullname, path=None):
file_path = file_path + '.hy'
if os.path.isfile(file_path):
fileobj = io.open(file_path, 'rU', encoding='utf-8')
etc = ('.hy', 'U', HY_SOURCE)
etc = ('.hy', 'U', imp.PY_SOURCE)
break
else:
try:
fileobj, file_path, etc = imp.find_module(subname, path)
except (ImportError, IOError):
return None


return HyLoader(fullname, file_path, fileobj, etc)

sys.path_hooks.append(HyImporter)
Expand Down

0 comments on commit d1236bc

Please sign in to comment.