Merge pull request #410 from paultag/paultag/bugfix/python3.4
Support Python 3.4 (initial cut)
This commit is contained in:
commit
7c2a76f7e6
@ -10,15 +10,15 @@ import sys
|
|||||||
module_name = "<STDIN>"
|
module_name = "<STDIN>"
|
||||||
|
|
||||||
hst = import_file_to_hst(sys.argv[1])
|
hst = import_file_to_hst(sys.argv[1])
|
||||||
print(hst)
|
print(str(hst).encode("utf-8"))
|
||||||
print("")
|
print("")
|
||||||
print("")
|
print("")
|
||||||
_ast = import_file_to_ast(sys.argv[1], module_name)
|
_ast = import_file_to_ast(sys.argv[1], module_name)
|
||||||
print("")
|
print("")
|
||||||
print("")
|
print("")
|
||||||
print(astor.dump(_ast))
|
print(astor.dump(_ast).encode("utf-8"))
|
||||||
print("")
|
print("")
|
||||||
print("")
|
print("")
|
||||||
print(astor.codegen.to_source(_ast))
|
print(astor.codegen.to_source(_ast).encode("utf-8"))
|
||||||
|
|
||||||
import_file_to_module(module_name, sys.argv[1])
|
import_file_to_module(module_name, sys.argv[1])
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
||||||
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
||||||
|
# Copyright (c) 2013 Berker Peksag <berker.peksag@gmail.com>
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
# copy of this software and associated documentation files (the "Software"),
|
# copy of this software and associated documentation files (the "Software"),
|
||||||
@ -23,9 +24,23 @@ try:
|
|||||||
import __builtin__ as builtins
|
import __builtin__ as builtins
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import builtins # NOQA
|
import builtins # NOQA
|
||||||
|
try:
|
||||||
|
from py_compile import MAGIC, wr_long
|
||||||
|
except ImportError:
|
||||||
|
# py_compile.MAGIC removed and imp.get_magic() deprecated in Python 3.4
|
||||||
|
from importlib.util import MAGIC_NUMBER as MAGIC # NOQA
|
||||||
|
|
||||||
|
def wr_long(f, x):
|
||||||
|
"""Internal; write a 32-bit int to a file in little-endian order."""
|
||||||
|
f.write(bytes([x & 0xff,
|
||||||
|
(x >> 8) & 0xff,
|
||||||
|
(x >> 16) & 0xff,
|
||||||
|
(x >> 24) & 0xff]))
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
PY3 = sys.version_info[0] >= 3
|
PY3 = sys.version_info[0] >= 3
|
||||||
|
PY33 = sys.version_info >= (3, 3)
|
||||||
|
PY34 = sys.version_info >= (3, 4)
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
str_type = str
|
str_type = str
|
||||||
|
@ -39,7 +39,7 @@ from hy.errors import HyCompileError, HyTypeError
|
|||||||
|
|
||||||
import hy.macros
|
import hy.macros
|
||||||
from hy.macros import require, macroexpand
|
from hy.macros import require, macroexpand
|
||||||
from hy._compat import str_type, long_type
|
from hy._compat import str_type, long_type, PY33, PY3, PY34
|
||||||
import hy.importer
|
import hy.importer
|
||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
@ -77,7 +77,7 @@ _compile_table = {}
|
|||||||
|
|
||||||
|
|
||||||
def ast_str(foobar):
|
def ast_str(foobar):
|
||||||
if sys.version_info[0] >= 3:
|
if PY3:
|
||||||
return str(foobar)
|
return str(foobar)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -754,7 +754,7 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
ret = handler_results
|
ret = handler_results
|
||||||
|
|
||||||
if sys.version_info[0] >= 3 and sys.version_info[1] >= 3:
|
if PY33:
|
||||||
# Python 3.3 features a merge of TryExcept+TryFinally into Try.
|
# Python 3.3 features a merge of TryExcept+TryFinally into Try.
|
||||||
return ret + ast.Try(
|
return ret + ast.Try(
|
||||||
lineno=expr.start_line,
|
lineno=expr.start_line,
|
||||||
@ -831,7 +831,7 @@ class HyASTCompiler(object):
|
|||||||
exceptions,
|
exceptions,
|
||||||
"Exception storage target name must be a symbol.")
|
"Exception storage target name must be a symbol.")
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
if PY3:
|
||||||
# Python3 features a change where the Exception handler
|
# Python3 features a change where the Exception handler
|
||||||
# moved the name from a Name() to a pure Python String type.
|
# moved the name from a Name() to a pure Python String type.
|
||||||
#
|
#
|
||||||
@ -1199,7 +1199,7 @@ class HyASTCompiler(object):
|
|||||||
optional_vars=thing,
|
optional_vars=thing,
|
||||||
body=body.stmts)
|
body=body.stmts)
|
||||||
|
|
||||||
if sys.version_info[0] >= 3 and sys.version_info[1] >= 3:
|
if PY33:
|
||||||
the_with.items = [ast.withitem(context_expr=ctx.force_expr,
|
the_with.items = [ast.withitem(context_expr=ctx.force_expr,
|
||||||
optional_vars=thing)]
|
optional_vars=thing)]
|
||||||
|
|
||||||
@ -1699,12 +1699,32 @@ class HyASTCompiler(object):
|
|||||||
arglist = expression.pop(0)
|
arglist = expression.pop(0)
|
||||||
ret, args, defaults, stararg, kwargs = self._parse_lambda_list(arglist)
|
ret, args, defaults, stararg, kwargs = self._parse_lambda_list(arglist)
|
||||||
|
|
||||||
args = ast.arguments(
|
if PY34:
|
||||||
args=[ast.Name(arg=ast_str(x), id=ast_str(x),
|
# Python 3.4+ requres that args are an ast.arg object, rather
|
||||||
|
# than an ast.Name or bare string.
|
||||||
|
args = [ast.arg(arg=ast_str(x),
|
||||||
|
annotation=None, # Fix me!
|
||||||
|
lineno=x.start_line,
|
||||||
|
col_offset=x.start_column) for x in args]
|
||||||
|
|
||||||
|
# XXX: Beware. Beware. This wasn't put into the parse lambda
|
||||||
|
# list because it's really just an internal parsing thing.
|
||||||
|
|
||||||
|
if kwargs:
|
||||||
|
kwargs = ast.arg(arg=kwargs, annotation=None)
|
||||||
|
|
||||||
|
if stararg:
|
||||||
|
stararg = ast.arg(arg=stararg, annotation=None)
|
||||||
|
|
||||||
|
# Let's find a better home for these guys.
|
||||||
|
else:
|
||||||
|
args = [ast.Name(arg=ast_str(x), id=ast_str(x),
|
||||||
ctx=ast.Param(),
|
ctx=ast.Param(),
|
||||||
lineno=x.start_line,
|
lineno=x.start_line,
|
||||||
col_offset=x.start_column)
|
col_offset=x.start_column) for x in args]
|
||||||
for x in args],
|
|
||||||
|
args = ast.arguments(
|
||||||
|
args=args,
|
||||||
vararg=stararg,
|
vararg=stararg,
|
||||||
kwarg=kwargs,
|
kwarg=kwargs,
|
||||||
kwonlyargs=[],
|
kwonlyargs=[],
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
# DEALINGS IN THE SOFTWARE.
|
# DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
from py_compile import wr_long, MAGIC
|
|
||||||
from hy.compiler import hy_compile, HyTypeError
|
from hy.compiler import hy_compile, HyTypeError
|
||||||
from hy.models import HyObject
|
from hy.models import HyObject
|
||||||
from hy.lex import tokenize, LexException
|
from hy.lex import tokenize, LexException
|
||||||
@ -32,7 +31,7 @@ import ast
|
|||||||
import os
|
import os
|
||||||
import __future__
|
import __future__
|
||||||
|
|
||||||
from hy._compat import builtins, long_type
|
from hy._compat import PY3, PY33, MAGIC, builtins, long_type, wr_long
|
||||||
|
|
||||||
|
|
||||||
def ast_compile(ast, filename, mode):
|
def ast_compile(ast, filename, mode):
|
||||||
@ -139,12 +138,12 @@ def write_hy_as_pyc(fname):
|
|||||||
open_ = builtins.open
|
open_ = builtins.open
|
||||||
|
|
||||||
with open_(cfile, 'wb') as fc:
|
with open_(cfile, 'wb') as fc:
|
||||||
if sys.version_info[0] >= 3:
|
if PY3:
|
||||||
fc.write(b'\0\0\0\0')
|
fc.write(b'\0\0\0\0')
|
||||||
else:
|
else:
|
||||||
fc.write('\0\0\0\0')
|
fc.write('\0\0\0\0')
|
||||||
wr_long(fc, timestamp)
|
wr_long(fc, timestamp)
|
||||||
if (sys.version_info[0] >= 3 and sys.version_info[1] >= 3):
|
if PY33:
|
||||||
wr_long(fc, st.st_size)
|
wr_long(fc, st.st_size)
|
||||||
marshal.dump(code, fc)
|
marshal.dump(code, fc)
|
||||||
fc.flush()
|
fc.flush()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user