Skip to content

Commit

Permalink
Merge branch 'daritter-compat_struct'
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Apr 29, 2015
2 parents 68c4dc2 + f94d08b commit 8a47e99
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
42 changes: 42 additions & 0 deletions pyvisa/compat/struct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""
pyvisa.compat.struct
~~~~~~~~~~~~~~~~~~~~~~~~~
Python 2/3 compatibility for struct module
:copyright: 2015, PSF
:license: PSF License
"""

from __future__ import division, unicode_literals, print_function, absolute_import

import sys
import struct

# we always want the exception to be able to catch it
error = struct.error

# compatibility for unicode literals was introduced in 2.7.8
# if we're above that there is nothing to do except aliasing
if sys.hexversion >= 0x02070800:
pack = struct.pack
pack_into = struct.pack_into
unpack = struct.unpack
unpack_from = struct.unpack_from
calcsize = struct.calcsize
else:
def pack(fmt, *args):
return struct.pack(str(fmt), *args)

def pack_into(fmt, *args, **argk):
return struct.pack_into(str(fmt), *args, **argk)

def unpack(fmt, string):
return struct.unpack(str(fmt), string)

def unpack_from(fmt, *args, **kwargs):
return struct.unpack_from(str(fmt), *args, **kwargs)

def calcsize(fmt):
return struct.calcsize(str(fmt))
29 changes: 7 additions & 22 deletions pyvisa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,12 @@
import os
import platform
import sys
import struct
import subprocess
import warnings

from .compat import check_output, string_types, OrderedDict
from .compat import check_output, string_types, OrderedDict, struct
from . import __version__, logger

if sys.version >= '3':
_struct_unpack = struct.unpack
_struct_unpack_from = struct.unpack_from
_struct_pack = struct.pack
else:
def _struct_unpack(fmt, string):
return struct.unpack(str(fmt), string)

def _struct_unpack_from(fmt, *args, **kwargs):
return struct.unpack_from(str(fmt), *args, **kwargs)

def _struct_pack(fmt, *values):
return struct.pack(str(fmt), *values)


def read_user_library_path():
"""Return the library path stored in one of the following configuration files:
Expand Down Expand Up @@ -264,7 +249,7 @@ def parse_binary(bytes_data, is_big_endian=False, is_single=False):
else:
fmt = endianess + str(data_length // 8) + 'd'

result = list(_struct_unpack(fmt, data))
result = list(struct.unpack(fmt, data))
except struct.error:
raise ValueError("Binary data itself was malformed")

Expand Down Expand Up @@ -367,7 +352,7 @@ def from_binary_block(block, offset=0, data_length=None, datatype='f', is_big_en
fullfmt = '%s%d%s' % (endianess, array_length, datatype)

try:
return container(_struct_unpack_from(fullfmt, block, offset))
return container(struct.unpack_from(fullfmt, block, offset))
except struct.error:
raise ValueError("Binary data was malformed")

Expand All @@ -392,9 +377,9 @@ def to_ieee_block(iterable, datatype='f', is_big_endian=False):
fullfmt = '%s%d%s' % (endianess, array_length, datatype)

if sys.version >= '3':
return bytes('#%d%d' % (len(header), data_length), 'ascii') + _struct_pack(fullfmt, *iterable)
return bytes('#%d%d' % (len(header), data_length), 'ascii') + struct.pack(fullfmt, *iterable)
else:
return str('#%d%d' % (len(header), data_length)) + _struct_pack(fullfmt, *iterable)
return str('#%d%d' % (len(header), data_length)) + struct.pack(fullfmt, *iterable)


def get_system_details(backends=True):
Expand Down Expand Up @@ -561,15 +546,15 @@ def get_shared_library_arch(filename):
dos_headers = fp.read(64)
_ = fp.read(4)

magic, skip, offset = _struct_unpack(str('2s58sl'), dos_headers)
magic, skip, offset = struct.unpack(str('2s58sl'), dos_headers)

if magic != b'MZ':
raise Exception('Not an executable')

fp.seek(offset, io.SEEK_SET)
pe_header = fp.read(6)

sig, skip, machine = _struct_unpack(str('2s2sH'), pe_header)
sig, skip, machine = struct.unpack(str('2s2sH'), pe_header)

if sig != b'PE':
raise Exception('Not a PE executable')
Expand Down

0 comments on commit 8a47e99

Please sign in to comment.