def __new__(cls, number, *args, **kwargs): if isinstance(number, str_type): bases = {"0x": 16, "0o": 8, "0b": 2} for leader, base in bases.items(): if number.startswith(leader): # We've got a string, known leader, set base. number = long_type(number, base=base) break else: # We've got a string, no known leader; base 10. number = long_type(number, base=10) else: # We've got a non-string; convert straight. number = long_type(number) return super(HyInteger, cls).__new__(cls, number)
def write_hy_as_pyc(fname): with open(fname, 'U') as f: try: st = os.fstat(f.fileno()) except AttributeError: st = os.stat(fname) timestamp = long_type(st.st_mtime) _ast = import_file_to_ast(fname, os.path.basename(os.path.splitext(fname)[0])) code = ast_compile(_ast, fname, "exec") cfile = "%s.pyc" % fname[:-len(".hy")] open_ = builtins.open with open_(cfile, 'wb') as fc: if PY3: fc.write(b'\0\0\0\0') else: fc.write('\0\0\0\0') wr_long(fc, timestamp) if PY33: wr_long(fc, st.st_size) marshal.dump(code, fc) fc.flush() fc.seek(0, 0) fc.write(MAGIC)
def write_code_as_pyc(fname, code): st = os.stat(fname) timestamp = long_type(st.st_mtime) 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) wr_long(fc, timestamp) if PY3: wr_long(fc, st.st_size) marshal.dump(code, fc)
def write_code_as_pyc(fname, code): st = os.stat(fname) timestamp = long_type(st.st_mtime) 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) if PY37: # With PEP 552, the header structure has a new flags field # that we need to fill in. All zeros preserve the legacy # behaviour, but should we implement reproducible builds, # this is where we'd add the information. wr_long(fc, 0) wr_long(fc, timestamp) if PY3: wr_long(fc, st.st_size) marshal.dump(code, fc)
def test_replace_long_type(): """ Test replacing integers.""" replaced = replace_hy_obj(long_type(0), HyInteger(13)) assert replaced == HyInteger(0)
def test_wrap_nested_expr(): """ Test conversion of HyExpressions with embedded non-HyObjects.""" wrapped = wrap_value(HyExpression([long_type(0)])) assert type(wrapped) == HyExpression assert type(wrapped[0]) == HyInteger assert wrapped == HyExpression([HyInteger(0)])
def test_replace_tuple(): """ Test replacing tuples.""" replaced = replace_hy_obj((long_type(0), ), HyInteger(13)) assert type(replaced) == HyList assert type(replaced[0]) == HyInteger assert replaced == HyList([HyInteger(0)])
def test_wrap_long_type(): """ Test conversion of integers.""" wrapped = wrap_value(long_type(0)) assert type(wrapped) == HyInteger
def __new__(cls, number, *args, **kwargs): number = long_type(number) return super(HyInteger, cls).__new__(cls, number)