2013-04-24 08:32:03 -04:00
|
|
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
2013-04-24 09:03:56 -04:00
|
|
|
# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
|
|
|
|
# Copyright (c) 2013 James King <james@agentultra.com>
|
|
|
|
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
|
|
|
# Copyright (c) 2013 Konrad Hinsen <konrad.hinsen@fastmail.net>
|
|
|
|
# Copyright (c) 2013 Thom Neale <twneale@gmail.com>
|
2013-04-24 08:57:51 -04:00
|
|
|
# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
|
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 12:56:03 -07:00
|
|
|
# Copyright (c) 2013 Bob Tolbert <bob@tolbert.org>
|
2013-04-24 08:32:03 -04: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-06-29 15:56:58 -06:00
|
|
|
import argparse
|
2013-07-04 21:31:17 -04:00
|
|
|
import code
|
|
|
|
import ast
|
2013-04-23 21:57:13 -04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import hy
|
|
|
|
|
2013-04-12 17:30:13 +02:00
|
|
|
from hy.lex import LexException, PrematureEndOfInput, tokenize
|
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 12:56:03 -07:00
|
|
|
from hy.compiler import hy_compile, HyTypeError
|
2013-04-12 17:30:13 +02:00
|
|
|
from hy.importer import ast_compile, import_buffer_to_module
|
2013-07-06 17:43:51 +02:00
|
|
|
from hy.completer import completion
|
2013-04-23 21:57:13 -04:00
|
|
|
|
2013-04-12 17:30:13 +02:00
|
|
|
from hy.macros import macro, require
|
2013-04-23 21:57:13 -04:00
|
|
|
from hy.models.expression import HyExpression
|
|
|
|
from hy.models.string import HyString
|
|
|
|
from hy.models.symbol import HySymbol
|
|
|
|
|
2013-09-24 10:27:30 +03:00
|
|
|
from hy._compat import builtins
|
2013-06-25 17:02:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HyQuitter(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Use (%s) or Ctrl-D (i.e. EOF) to exit" % (self.name)
|
|
|
|
|
|
|
|
__str__ = __repr__
|
|
|
|
|
|
|
|
def __call__(self, code=None):
|
|
|
|
try:
|
|
|
|
sys.stdin.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
raise SystemExit(code)
|
|
|
|
|
|
|
|
builtins.quit = HyQuitter('quit')
|
|
|
|
builtins.exit = HyQuitter('exit')
|
|
|
|
|
2013-08-24 08:33:13 -06:00
|
|
|
|
2013-08-19 15:27:55 +02:00
|
|
|
def print_python_code(_ast):
|
|
|
|
import astor.codegen
|
|
|
|
# astor cannot handle ast.Interactive, so disguise it as a module
|
|
|
|
_ast_for_print = ast.Module()
|
|
|
|
_ast_for_print.body = _ast.body
|
|
|
|
print(astor.codegen.to_source(_ast_for_print))
|
2013-04-23 21:57:13 -04:00
|
|
|
|
2013-08-24 08:33:13 -06:00
|
|
|
|
2013-04-23 21:57:13 -04:00
|
|
|
class HyREPL(code.InteractiveConsole):
|
2013-12-29 00:25:55 +02:00
|
|
|
def __init__(self, spy=False, locals=None, filename="<input>"):
|
2013-08-19 15:27:55 +02:00
|
|
|
self.spy = spy
|
2013-12-29 00:25:55 +02:00
|
|
|
code.InteractiveConsole.__init__(self, locals=locals,
|
|
|
|
filename=filename)
|
2013-08-19 15:27:55 +02:00
|
|
|
|
2013-04-23 21:57:13 -04:00
|
|
|
def runsource(self, source, filename='<input>', symbol='single'):
|
2013-12-26 09:36:45 -07:00
|
|
|
global SIMPLE_TRACEBACKS
|
2013-04-23 21:57:13 -04:00
|
|
|
try:
|
2013-04-12 17:30:13 +02:00
|
|
|
tokens = tokenize(source)
|
|
|
|
except PrematureEndOfInput:
|
|
|
|
return True
|
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 12:56:03 -07:00
|
|
|
except LexException as e:
|
|
|
|
if e.source is None:
|
|
|
|
e.source = source
|
|
|
|
e.filename = filename
|
2013-12-26 09:36:45 -07:00
|
|
|
sys.stderr.write(str(e))
|
2013-04-23 21:57:13 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
2013-05-16 15:34:14 +02:00
|
|
|
_ast = hy_compile(tokens, "__console__", root=ast.Interactive)
|
2013-08-19 15:27:55 +02:00
|
|
|
if self.spy:
|
|
|
|
print_python_code(_ast)
|
2013-04-23 21:57:13 -04:00
|
|
|
code = ast_compile(_ast, filename, symbol)
|
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 12:56:03 -07:00
|
|
|
except HyTypeError as e:
|
|
|
|
if e.source is None:
|
|
|
|
e.source = source
|
|
|
|
e.filename = filename
|
2013-12-26 09:36:45 -07:00
|
|
|
if SIMPLE_TRACEBACKS:
|
|
|
|
sys.stderr.write(str(e))
|
|
|
|
else:
|
|
|
|
self.showtraceback()
|
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 12:56:03 -07:00
|
|
|
return False
|
2013-04-23 21:57:13 -04:00
|
|
|
except Exception:
|
|
|
|
self.showtraceback()
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.runcode(code)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@macro("koan")
|
2013-05-11 09:09:34 +02:00
|
|
|
def koan_macro():
|
2013-04-23 21:57:13 -04:00
|
|
|
return HyExpression([HySymbol('print'),
|
|
|
|
HyString("""
|
|
|
|
Ummon asked the head monk, "What sutra are you lecturing on?"
|
|
|
|
"The Nirvana Sutra."
|
|
|
|
"The Nirvana Sutra has the Four Virtues, hasn't it?"
|
|
|
|
"It has."
|
|
|
|
Ummon asked, picking up a cup, "How many virtues has this?"
|
|
|
|
"None at all, " said the monk.
|
|
|
|
"But ancient people said it had, didn't they?" said Ummon.
|
|
|
|
"Whatdo you think of what they said?"
|
|
|
|
Ummon struck the cup and asked, "You understand?"
|
|
|
|
"No," said the monk.
|
|
|
|
"Then," said Ummon, "You'd better go on with your lectures on the sutra."
|
|
|
|
""")])
|
|
|
|
|
|
|
|
|
|
|
|
@macro("ideas")
|
2013-05-11 09:09:34 +02:00
|
|
|
def ideas_macro():
|
2013-04-23 21:57:13 -04:00
|
|
|
return HyExpression([HySymbol('print'),
|
|
|
|
HyString("""
|
|
|
|
|
|
|
|
=> (import [sh [figlet]])
|
|
|
|
=> (figlet "Hi, Hy!")
|
|
|
|
_ _ _ _ _ _
|
|
|
|
| | | (_) | | | |_ _| |
|
|
|
|
| |_| | | | |_| | | | | |
|
|
|
|
| _ | |_ | _ | |_| |_|
|
|
|
|
|_| |_|_( ) |_| |_|\__, (_)
|
|
|
|
|/ |___/
|
|
|
|
|
|
|
|
|
|
|
|
;;; string things
|
|
|
|
(.join ", " ["what" "the" "heck"])
|
|
|
|
|
|
|
|
|
|
|
|
;;; this one plays with command line bits
|
|
|
|
(import [sh [cat grep]])
|
|
|
|
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))
|
|
|
|
|
|
|
|
|
|
|
|
;;; filtering a list w/ a lambda
|
|
|
|
(filter (lambda [x] (= (% x 2) 0)) (range 0 10))
|
|
|
|
|
|
|
|
|
|
|
|
;;; swaggin' functional bits (Python rulez)
|
|
|
|
(max (map (lambda [x] (len x)) ["hi" "my" "name" "is" "paul"]))
|
|
|
|
|
|
|
|
""")])
|
|
|
|
|
2013-05-16 15:34:14 +02:00
|
|
|
require("hy.cmdline", "__console__")
|
|
|
|
require("hy.cmdline", "__main__")
|
|
|
|
|
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 12:56:03 -07:00
|
|
|
SIMPLE_TRACEBACKS = True
|
|
|
|
|
2013-04-23 21:57:13 -04:00
|
|
|
|
|
|
|
def run_command(source):
|
2013-04-24 09:21:42 -04:00
|
|
|
try:
|
|
|
|
import_buffer_to_module("__main__", source)
|
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 12:56:03 -07:00
|
|
|
except (HyTypeError, LexException) as e:
|
|
|
|
if SIMPLE_TRACEBACKS:
|
2013-12-26 09:36:45 -07:00
|
|
|
sys.stderr.write(str(e))
|
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 12:56:03 -07:00
|
|
|
return 1
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
raise
|
2013-04-23 21:57:13 -04:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def run_file(filename):
|
|
|
|
from hy.importer import import_file_to_module
|
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 12:56:03 -07:00
|
|
|
try:
|
|
|
|
import_file_to_module("__main__", filename)
|
|
|
|
except (HyTypeError, LexException) as e:
|
|
|
|
if SIMPLE_TRACEBACKS:
|
2013-12-26 09:36:45 -07:00
|
|
|
sys.stderr.write(str(e))
|
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 12:56:03 -07:00
|
|
|
return 1
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
return 0
|
2013-04-23 21:57:13 -04:00
|
|
|
|
|
|
|
|
2013-08-19 15:27:55 +02:00
|
|
|
def run_repl(hr=None, spy=False):
|
2013-04-23 21:57:13 -04:00
|
|
|
sys.ps1 = "=> "
|
|
|
|
sys.ps2 = "... "
|
|
|
|
|
2013-07-06 17:43:51 +02:00
|
|
|
with completion():
|
|
|
|
if not hr:
|
2013-08-19 15:27:55 +02:00
|
|
|
hr = HyREPL(spy)
|
2013-06-29 16:23:48 +02:00
|
|
|
|
2013-07-06 17:43:51 +02:00
|
|
|
hr.interact("{appname} {version}".format(
|
|
|
|
appname=hy.__appname__,
|
|
|
|
version=hy.__version__
|
2013-07-06 13:43:05 -04:00
|
|
|
))
|
2013-06-29 16:23:48 +02:00
|
|
|
|
2013-04-23 21:57:13 -04:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2014-01-13 16:15:43 +01:00
|
|
|
def run_icommand(source, spy=False):
|
|
|
|
hr = HyREPL(spy)
|
2013-04-23 21:57:13 -04:00
|
|
|
hr.runsource(source, filename='<input>', symbol='single')
|
|
|
|
return run_repl(hr)
|
|
|
|
|
|
|
|
|
2013-06-29 15:56:58 -06:00
|
|
|
USAGE = "%(prog)s [-h | -i cmd | -c cmd | file | -] [arg] ..."
|
|
|
|
VERSION = "%(prog)s " + hy.__version__
|
2013-04-23 21:57:13 -04:00
|
|
|
EPILOG = """ file program read from script
|
|
|
|
- program read from stdin
|
2013-06-29 15:56:58 -06:00
|
|
|
[arg] ... arguments passed to program in sys.argv[1:]
|
2013-04-23 21:57:13 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def cmdline_handler(scriptname, argv):
|
2013-06-29 15:56:58 -06:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="hy",
|
|
|
|
usage=USAGE,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
epilog=EPILOG)
|
|
|
|
parser.add_argument("-c", dest="command",
|
|
|
|
help="program passed in as a string")
|
|
|
|
parser.add_argument(
|
|
|
|
"-i", dest="icommand",
|
|
|
|
help="program passed in as a string, then stay in REPL")
|
2013-08-19 15:27:55 +02:00
|
|
|
parser.add_argument("--spy", action="store_true",
|
|
|
|
help="print equivalent Python code before executing")
|
2013-04-23 21:57:13 -04:00
|
|
|
|
2013-06-29 15:56:58 -06:00
|
|
|
parser.add_argument("-v", action="version", version=VERSION)
|
2013-04-23 21:57:13 -04:00
|
|
|
|
2013-12-28 06:58:08 -07:00
|
|
|
parser.add_argument("--show-tracebacks", action="store_true",
|
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 12:56:03 -07:00
|
|
|
help="show complete tracebacks for Hy exceptions")
|
|
|
|
|
2013-06-29 15:56:58 -06:00
|
|
|
# this will contain the script/program name and any arguments for it.
|
|
|
|
parser.add_argument('args', nargs=argparse.REMAINDER,
|
|
|
|
help=argparse.SUPPRESS)
|
|
|
|
|
|
|
|
# stash the hy exectuable in case we need it later
|
|
|
|
# mimics Python sys.executable
|
|
|
|
hy.executable = argv[0]
|
|
|
|
|
|
|
|
options = parser.parse_args(argv[1:])
|
|
|
|
|
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 12:56:03 -07:00
|
|
|
if options.show_tracebacks:
|
|
|
|
global SIMPLE_TRACEBACKS
|
|
|
|
SIMPLE_TRACEBACKS = False
|
|
|
|
|
2013-06-29 15:56:58 -06:00
|
|
|
# reset sys.argv like Python
|
2013-11-29 23:51:41 -02:00
|
|
|
sys.argv = options.args or [""]
|
2013-04-23 21:57:13 -04:00
|
|
|
|
|
|
|
if options.command:
|
|
|
|
# User did "hy -c ..."
|
|
|
|
return run_command(options.command)
|
|
|
|
|
|
|
|
if options.icommand:
|
|
|
|
# User did "hy -i ..."
|
2014-01-13 16:15:43 +01:00
|
|
|
return run_icommand(options.icommand, spy=options.spy)
|
2013-04-23 21:57:13 -04:00
|
|
|
|
2013-06-29 15:56:58 -06:00
|
|
|
if options.args:
|
|
|
|
if options.args[0] == "-":
|
2013-04-23 21:57:13 -04:00
|
|
|
# Read the program from stdin
|
|
|
|
return run_command(sys.stdin.read())
|
|
|
|
|
|
|
|
else:
|
|
|
|
# User did "hy <filename>"
|
2013-07-25 23:33:14 -06:00
|
|
|
try:
|
|
|
|
return run_file(options.args[0])
|
|
|
|
except IOError as x:
|
|
|
|
sys.stderr.write("hy: Can't open file '%s': [Errno %d] %s\n" %
|
|
|
|
(x.filename, x.errno, x.strerror))
|
|
|
|
sys.exit(x.errno)
|
2013-04-23 21:57:13 -04:00
|
|
|
|
|
|
|
# User did NOTHING!
|
2013-08-19 15:27:55 +02:00
|
|
|
return run_repl(spy=options.spy)
|
2013-06-29 15:56:58 -06:00
|
|
|
|
|
|
|
|
|
|
|
# entry point for cmd line script "hy"
|
|
|
|
def hy_main():
|
|
|
|
sys.exit(cmdline_handler("hy", sys.argv))
|
|
|
|
|
|
|
|
|
|
|
|
# entry point for cmd line script "hyc"
|
|
|
|
def hyc_main():
|
|
|
|
from hy.importer import write_hy_as_pyc
|
2013-07-25 23:33:14 -06:00
|
|
|
parser = argparse.ArgumentParser(prog="hyc")
|
|
|
|
parser.add_argument("files", metavar="FILE", nargs='+',
|
|
|
|
help="file to compile")
|
|
|
|
parser.add_argument("-v", action="version", version=VERSION)
|
|
|
|
|
|
|
|
options = parser.parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
for file in options.files:
|
|
|
|
try:
|
|
|
|
write_hy_as_pyc(file)
|
|
|
|
print("Compiling %s" % file)
|
|
|
|
except IOError as x:
|
|
|
|
sys.stderr.write("hyc: Can't open file '%s': [Errno %d] %s\n" %
|
|
|
|
(x.filename, x.errno, x.strerror))
|
|
|
|
sys.exit(x.errno)
|