From 8120a25c0822598c9ed974562f068a94b0097ace Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sun, 29 Dec 2013 12:57:48 +0200 Subject: [PATCH] Add py_compile.{MAGIC, wr_long} to hy._compat. Closes #344. --- hy/_compat.py | 14 ++++++++++++++ hy/importer.py | 8 +++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/hy/_compat.py b/hy/_compat.py index e30d0dc..65aa29b 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -1,5 +1,6 @@ # Copyright (c) 2013 Paul Tagliamonte # Copyright (c) 2013 Julien Danjou +# Copyright (c) 2013 Berker Peksag # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -23,9 +24,22 @@ try: import __builtin__ as builtins except ImportError: 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 PY3 = sys.version_info[0] >= 3 +PY33 = sys.version_info[0] >= 3 and sys.version_info[1] >= 3 if PY3: str_type = str diff --git a/hy/importer.py b/hy/importer.py index b32b71a..63cc55a 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -18,12 +18,10 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from py_compile import wr_long, MAGIC from hy.compiler import hy_compile from hy.models import HyObject from hy.lex import tokenize - from io import open import marshal import imp @@ -32,7 +30,7 @@ import ast import os 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): @@ -128,12 +126,12 @@ def write_hy_as_pyc(fname): open_ = builtins.open with open_(cfile, 'wb') as fc: - if sys.version_info[0] >= 3: + if PY3: fc.write(b'\0\0\0\0') else: fc.write('\0\0\0\0') wr_long(fc, timestamp) - if (sys.version_info[0] >= 3 and sys.version_info[1] >= 3): + if PY33: wr_long(fc, st.st_size) marshal.dump(code, fc) fc.flush()