Add py_compile.{MAGIC, wr_long} to hy._compat.

Closes #344.
This commit is contained in:
Berker Peksag 2013-12-29 12:57:48 +02:00
parent f189f0a457
commit 8120a25c08
2 changed files with 17 additions and 5 deletions

View File

@ -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,22 @@ 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[0] >= 3 and sys.version_info[1] >= 3
if PY3: if PY3:
str_type = str str_type = str

View File

@ -18,12 +18,10 @@
# 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 from hy.compiler import hy_compile
from hy.models import HyObject from hy.models import HyObject
from hy.lex import tokenize from hy.lex import tokenize
from io import open from io import open
import marshal import marshal
import imp import imp
@ -32,7 +30,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):
@ -128,12 +126,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()