2013-03-18 15:27:14 +01:00
|
|
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
2014-12-07 19:02:48 +01:00
|
|
|
# Copyright (c) 2013, 2014 Bob Tolbert <bob@tolbert.org>
|
2013-03-04 01:40:46 +01:00
|
|
|
#
|
2013-03-18 15:27:14 +01:00
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
# DEALINGS IN THE SOFTWARE.
|
2013-03-04 01:40:46 +01:00
|
|
|
|
Much better version of new error messages.
This version is much simpler.
At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.
Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.
And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.
The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.
I'm sure there is a better way to do the markup, the current method
is kludgy but works.
I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.
This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.
Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-22 20:56:03 +01:00
|
|
|
from hy.compiler import hy_compile, HyTypeError
|
2015-01-24 01:16:38 +01:00
|
|
|
from hy.models import HyObject, replace_hy_obj
|
Much better version of new error messages.
This version is much simpler.
At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.
Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.
And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.
The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.
I'm sure there is a better way to do the markup, the current method
is kludgy but works.
I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.
This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.
Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-22 20:56:03 +01:00
|
|
|
from hy.lex import tokenize, LexException
|
2015-02-16 23:27:18 +01:00
|
|
|
from hy.errors import HyIOError
|
2013-03-05 04:35:07 +01:00
|
|
|
|
2013-04-07 03:33:52 +02:00
|
|
|
from io import open
|
2017-04-10 02:27:51 +02:00
|
|
|
import re
|
2013-03-22 00:27:34 +01:00
|
|
|
import marshal
|
2017-04-10 02:27:51 +02:00
|
|
|
import struct
|
2013-03-04 01:40:46 +01:00
|
|
|
import imp
|
2013-03-05 04:35:07 +01:00
|
|
|
import sys
|
2013-04-10 03:33:09 +02:00
|
|
|
import ast
|
2013-03-05 04:35:07 +01:00
|
|
|
import os
|
2013-04-12 05:23:25 +02:00
|
|
|
import __future__
|
2013-03-04 01:40:46 +01:00
|
|
|
|
2017-04-14 01:17:32 +02:00
|
|
|
from hy._compat import PY3, PY34, MAGIC, builtins, long_type, wr_long
|
2015-08-13 09:59:58 +02:00
|
|
|
from hy._compat import string_types
|
2013-03-10 15:58:31 +01:00
|
|
|
|
|
|
|
|
2013-04-20 22:11:53 +02:00
|
|
|
def ast_compile(ast, filename, mode):
|
|
|
|
"""Compile AST.
|
|
|
|
Like Python's compile, but with some special flags."""
|
2013-09-22 15:31:15 +02:00
|
|
|
flags = (__future__.CO_FUTURE_DIVISION |
|
|
|
|
__future__.CO_FUTURE_PRINT_FUNCTION)
|
|
|
|
return compile(ast, filename, mode, flags)
|
2013-04-12 05:23:25 +02:00
|
|
|
|
|
|
|
|
2013-04-20 22:11:53 +02:00
|
|
|
def import_buffer_to_hst(buf):
|
2016-07-01 16:44:12 +02:00
|
|
|
"""Import content from buf and return a Hy AST."""
|
2013-05-01 19:50:06 +02:00
|
|
|
return tokenize(buf + "\n")
|
2013-03-09 00:18:43 +01:00
|
|
|
|
|
|
|
|
2013-03-10 15:58:31 +01:00
|
|
|
def import_file_to_hst(fpath):
|
2016-07-01 16:44:12 +02:00
|
|
|
"""Import content from fpath and return a Hy AST."""
|
2015-02-16 23:27:18 +01:00
|
|
|
try:
|
|
|
|
with open(fpath, 'r', encoding='utf-8') as f:
|
|
|
|
return import_buffer_to_hst(f.read())
|
|
|
|
except IOError as e:
|
|
|
|
raise HyIOError(e.errno, e.strerror, e.filename)
|
2013-03-10 15:58:31 +01:00
|
|
|
|
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
def import_buffer_to_ast(buf, module_name):
|
2013-04-20 22:11:53 +02:00
|
|
|
""" Import content from buf and return a Python AST."""
|
2013-05-16 15:34:14 +02:00
|
|
|
return hy_compile(import_buffer_to_hst(buf), module_name)
|
2013-04-20 22:11:53 +02:00
|
|
|
|
2013-03-09 00:18:43 +01:00
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
def import_file_to_ast(fpath, module_name):
|
2013-04-20 22:11:53 +02:00
|
|
|
"""Import content from fpath and return a Python AST."""
|
2013-05-16 15:34:14 +02:00
|
|
|
return hy_compile(import_file_to_hst(fpath), module_name)
|
2013-03-09 00:18:43 +01:00
|
|
|
|
2013-03-10 15:58:31 +01:00
|
|
|
|
2017-04-10 02:27:51 +02:00
|
|
|
def import_file_to_module(module_name, fpath, loader=None):
|
|
|
|
"""Import Hy source from fpath and put it into a Python module.
|
2013-03-10 15:58:31 +01:00
|
|
|
|
2017-04-10 02:27:51 +02:00
|
|
|
If there's an up-to-date byte-compiled version of this module, load that
|
|
|
|
instead. Otherwise, byte-compile the module once we're done loading it, if
|
|
|
|
we can.
|
|
|
|
|
|
|
|
Return the module."""
|
|
|
|
|
|
|
|
module = None
|
|
|
|
|
|
|
|
bytecode_path = get_bytecode_path(fpath)
|
2013-07-06 20:35:26 +02:00
|
|
|
try:
|
2017-04-10 02:27:51 +02:00
|
|
|
source_mtime = int(os.stat(fpath).st_mtime)
|
|
|
|
with open(bytecode_path, 'rb') as bc_f:
|
2017-04-12 03:53:35 +02:00
|
|
|
# The first 4 bytes are the magic number for the version of Python
|
|
|
|
# that compiled this bytecode.
|
|
|
|
bytecode_magic = bc_f.read(4)
|
|
|
|
# The next 4 bytes, interpreted as a little-endian 32-bit integer,
|
|
|
|
# are the mtime of the corresponding source file.
|
|
|
|
bytecode_mtime, = struct.unpack('<i', bc_f.read(4))
|
2017-04-10 02:27:51 +02:00
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
else:
|
2017-04-12 03:53:35 +02:00
|
|
|
if bytecode_magic == MAGIC and bytecode_mtime >= source_mtime:
|
2017-04-10 02:27:51 +02:00
|
|
|
# It's a cache hit. Load the byte-compiled version.
|
|
|
|
if PY3:
|
|
|
|
# As of Python 3.6, imp.load_compiled still exists, but it's
|
|
|
|
# deprecated. So let's use SourcelessFileLoader instead.
|
|
|
|
from importlib.machinery import SourcelessFileLoader
|
|
|
|
module = (SourcelessFileLoader(module_name, bytecode_path).
|
|
|
|
load_module(module_name))
|
|
|
|
else:
|
|
|
|
module = imp.load_compiled(module_name, bytecode_path)
|
|
|
|
|
|
|
|
if not module:
|
|
|
|
# It's a cache miss, so load from source.
|
|
|
|
sys.modules[module_name] = None
|
|
|
|
try:
|
|
|
|
_ast = import_file_to_ast(fpath, module_name)
|
|
|
|
module = imp.new_module(module_name)
|
|
|
|
module.__file__ = fpath
|
|
|
|
code = ast_compile(_ast, fpath, "exec")
|
2017-04-26 23:00:11 +02:00
|
|
|
try:
|
|
|
|
write_code_as_pyc(fpath, code)
|
|
|
|
except (IOError, OSError):
|
|
|
|
# We failed to save the bytecode, probably because of a
|
|
|
|
# permissions issue. The user only asked to import the
|
|
|
|
# file, so don't bug them about it.
|
|
|
|
pass
|
2017-04-10 02:27:51 +02:00
|
|
|
eval(code, module.__dict__)
|
|
|
|
except (HyTypeError, LexException) as e:
|
|
|
|
if e.source is None:
|
|
|
|
with open(fpath, 'rt') as fp:
|
|
|
|
e.source = fp.read()
|
|
|
|
e.filename = fpath
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
sys.modules.pop(module_name, None)
|
|
|
|
raise
|
|
|
|
sys.modules[module_name] = module
|
|
|
|
module.__name__ = module_name
|
|
|
|
|
2017-04-16 00:50:26 +02:00
|
|
|
module.__file__ = fpath
|
2017-04-10 02:27:51 +02:00
|
|
|
if loader:
|
|
|
|
module.__loader__ = loader
|
|
|
|
if is_package(module_name):
|
|
|
|
module.__path__ = []
|
|
|
|
module.__package__ = module_name
|
|
|
|
else:
|
|
|
|
module.__package__ = module_name.rpartition('.')[0]
|
|
|
|
|
|
|
|
return module
|
2013-03-05 04:35:07 +01:00
|
|
|
|
|
|
|
|
2013-04-24 15:21:16 +02:00
|
|
|
def import_buffer_to_module(module_name, buf):
|
Much better version of new error messages.
This version is much simpler.
At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.
Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.
And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.
The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.
I'm sure there is a better way to do the markup, the current method
is kludgy but works.
I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.
This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.
Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-22 20:56:03 +01:00
|
|
|
try:
|
|
|
|
_ast = import_buffer_to_ast(buf, module_name)
|
|
|
|
mod = imp.new_module(module_name)
|
|
|
|
eval(ast_compile(_ast, "", "exec"), mod.__dict__)
|
|
|
|
except (HyTypeError, LexException) as e:
|
|
|
|
if e.source is None:
|
|
|
|
e.source = buf
|
|
|
|
e.filename = '<stdin>'
|
|
|
|
raise
|
2013-04-24 15:21:16 +02:00
|
|
|
return mod
|
|
|
|
|
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
def hy_eval(hytree, namespace, module_name, ast_callback=None):
|
2013-04-10 03:33:09 +02:00
|
|
|
foo = HyObject()
|
|
|
|
foo.start_line = 0
|
|
|
|
foo.end_line = 0
|
|
|
|
foo.start_column = 0
|
|
|
|
foo.end_column = 0
|
2015-01-24 01:16:38 +01:00
|
|
|
replace_hy_obj(hytree, foo)
|
2015-08-12 19:00:03 +02:00
|
|
|
|
2015-08-13 09:59:58 +02:00
|
|
|
if not isinstance(module_name, string_types):
|
2015-08-12 19:00:03 +02:00
|
|
|
raise HyTypeError(foo, "Module name must be a string")
|
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
_ast, expr = hy_compile(hytree, module_name, get_expr=True)
|
2013-05-03 17:49:45 +02:00
|
|
|
|
|
|
|
# Spoof the positions in the generated ast...
|
|
|
|
for node in ast.walk(_ast):
|
|
|
|
node.lineno = 1
|
|
|
|
node.col_offset = 1
|
|
|
|
|
|
|
|
for node in ast.walk(expr):
|
|
|
|
node.lineno = 1
|
|
|
|
node.col_offset = 1
|
|
|
|
|
2017-03-24 17:03:12 +01:00
|
|
|
if ast_callback:
|
|
|
|
ast_callback(_ast, expr)
|
|
|
|
|
2015-08-12 19:00:03 +02:00
|
|
|
if not isinstance(namespace, dict):
|
|
|
|
raise HyTypeError(foo, "Globals must be a dictionary")
|
|
|
|
|
2013-05-03 17:49:45 +02:00
|
|
|
# Two-step eval: eval() the body of the exec call
|
|
|
|
eval(ast_compile(_ast, "<eval_body>", "exec"), namespace)
|
|
|
|
|
|
|
|
# Then eval the expression context and return that
|
|
|
|
return eval(ast_compile(expr, "<eval>", "eval"), namespace)
|
2013-04-10 03:33:09 +02:00
|
|
|
|
|
|
|
|
2013-03-22 00:27:34 +01:00
|
|
|
def write_hy_as_pyc(fname):
|
2013-05-16 15:34:14 +02:00
|
|
|
_ast = import_file_to_ast(fname,
|
|
|
|
os.path.basename(os.path.splitext(fname)[0]))
|
2013-04-20 22:11:53 +02:00
|
|
|
code = ast_compile(_ast, fname, "exec")
|
2017-04-10 02:27:51 +02:00
|
|
|
write_code_as_pyc(fname, code)
|
|
|
|
|
2013-03-22 00:27:34 +01:00
|
|
|
|
2017-04-10 02:27:51 +02:00
|
|
|
def write_code_as_pyc(fname, code):
|
|
|
|
st = os.stat(fname)
|
|
|
|
timestamp = long_type(st.st_mtime)
|
2013-04-13 13:39:18 +02:00
|
|
|
|
2017-04-10 02:27:51 +02:00
|
|
|
cfile = get_bytecode_path(fname)
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(cfile))
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with builtins.open(cfile, 'wb') as fc:
|
|
|
|
fc.write(MAGIC)
|
2013-03-22 00:27:34 +01:00
|
|
|
wr_long(fc, timestamp)
|
2017-04-14 01:17:32 +02:00
|
|
|
if PY3:
|
2013-04-13 13:39:18 +02:00
|
|
|
wr_long(fc, st.st_size)
|
2013-03-22 00:27:34 +01:00
|
|
|
marshal.dump(code, fc)
|
|
|
|
|
|
|
|
|
2016-12-27 16:09:58 +01:00
|
|
|
class MetaLoader(object):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def load_module(self, fullname):
|
|
|
|
if fullname in sys.modules:
|
|
|
|
return sys.modules[fullname]
|
|
|
|
|
|
|
|
if not self.path:
|
|
|
|
return
|
|
|
|
|
2017-04-10 02:27:51 +02:00
|
|
|
return import_file_to_module(fullname, self.path, self)
|
2016-12-27 16:09:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MetaImporter(object):
|
|
|
|
def find_on_path(self, fullname):
|
|
|
|
fls = ["%s/__init__.hy", "%s.hy"]
|
|
|
|
dirpath = "/".join(fullname.split("."))
|
|
|
|
|
|
|
|
for pth in sys.path:
|
|
|
|
pth = os.path.abspath(pth)
|
|
|
|
for fp in fls:
|
|
|
|
composed_path = fp % ("%s/%s" % (pth, dirpath))
|
|
|
|
if os.path.exists(composed_path):
|
|
|
|
return composed_path
|
|
|
|
|
|
|
|
def find_module(self, fullname, path=None):
|
|
|
|
path = self.find_on_path(fullname)
|
|
|
|
if path:
|
|
|
|
return MetaLoader(path)
|
|
|
|
|
2013-03-05 04:35:07 +01:00
|
|
|
|
2016-12-27 16:09:58 +01:00
|
|
|
sys.meta_path.insert(0, MetaImporter())
|
|
|
|
sys.path.insert(0, "")
|
2017-04-10 02:27:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_package(module_name):
|
|
|
|
mpath = os.path.join(*module_name.split("."))
|
|
|
|
for path in map(os.path.abspath, sys.path):
|
|
|
|
if os.path.exists(os.path.join(path, mpath, "__init__.hy")):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_bytecode_path(source_path):
|
|
|
|
if PY34:
|
|
|
|
import importlib.util
|
|
|
|
return importlib.util.cache_from_source(source_path)
|
|
|
|
elif hasattr(imp, "cache_from_source"):
|
|
|
|
return imp.cache_from_source(source_path)
|
|
|
|
else:
|
|
|
|
# If source_path has a file extension, replace it with ".pyc".
|
|
|
|
# Otherwise, just append ".pyc".
|
|
|
|
d, f = os.path.split(source_path)
|
|
|
|
return os.path.join(d, re.sub(r"(?:\.[^.]+)?\Z", ".pyc", f))
|