Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor xdrlib #402

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyvisa_py/protocols/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import struct
import sys
import time
import xdrlib

from ..common import logger
from . import xdrlib

#: Version of the protocol
RPCVERSION = 2
Expand Down
249 changes: 249 additions & 0 deletions pyvisa_py/protocols/xdrlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
# Taken from Python 3.12 and vendored since xdrlib will be removed in 3.13
# Warning about deprecation was removed and code reformatted using black
"""Implements (a subset of) Sun XDR -- eXternal Data Representation.

See: RFC 1014

"""

import struct
from functools import wraps
from io import BytesIO

__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]


# exceptions
class Error(Exception):
"""Exception class for this module. Use:

except xdrlib.Error as var:
# var has the Error instance for the exception

Public ivars:
msg -- contains the message

"""

def __init__(self, msg):
self.msg = msg

Check warning on line 29 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L29

Added line #L29 was not covered by tests

def __repr__(self):
return repr(self.msg)

Check warning on line 32 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L32

Added line #L32 was not covered by tests

def __str__(self):
return str(self.msg)

Check warning on line 35 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L35

Added line #L35 was not covered by tests


class ConversionError(Error):
pass


def raise_conversion_error(function):
"""Wrap any raised struct.errors in a ConversionError."""

@wraps(function)
def result(self, value):
try:
return function(self, value)
except struct.error as e:
raise ConversionError(e.args[0]) from None

Check warning on line 50 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L47-L50

Added lines #L47 - L50 were not covered by tests

return result


class Packer:
"""Pack various data representations into a buffer."""

def __init__(self):
self.reset()

Check warning on line 59 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L59

Added line #L59 was not covered by tests

def reset(self):
self.__buf = BytesIO()

Check warning on line 62 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L62

Added line #L62 was not covered by tests

def get_buffer(self):
return self.__buf.getvalue()

Check warning on line 65 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L65

Added line #L65 was not covered by tests

# backwards compatibility
get_buf = get_buffer

@raise_conversion_error
def pack_uint(self, x):
self.__buf.write(struct.pack(">L", x))

Check warning on line 72 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L72

Added line #L72 was not covered by tests

@raise_conversion_error
def pack_int(self, x):
self.__buf.write(struct.pack(">l", x))

Check warning on line 76 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L76

Added line #L76 was not covered by tests

pack_enum = pack_int

def pack_bool(self, x):
if x:
self.__buf.write(b"\0\0\0\1")

Check warning on line 82 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L82

Added line #L82 was not covered by tests
else:
self.__buf.write(b"\0\0\0\0")

Check warning on line 84 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L84

Added line #L84 was not covered by tests

def pack_uhyper(self, x):
try:
self.pack_uint(x >> 32 & 0xFFFFFFFF)
except (TypeError, struct.error) as e:
raise ConversionError(e.args[0]) from None
try:
self.pack_uint(x & 0xFFFFFFFF)
except (TypeError, struct.error) as e:
raise ConversionError(e.args[0]) from None

Check warning on line 94 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L87-L94

Added lines #L87 - L94 were not covered by tests

pack_hyper = pack_uhyper

@raise_conversion_error
def pack_float(self, x):
self.__buf.write(struct.pack(">f", x))

Check warning on line 100 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L100

Added line #L100 was not covered by tests

@raise_conversion_error
def pack_double(self, x):
self.__buf.write(struct.pack(">d", x))

Check warning on line 104 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L104

Added line #L104 was not covered by tests

def pack_fstring(self, n, s):
if n < 0:
raise ValueError("fstring size must be nonnegative")
data = s[:n]
n = ((n + 3) // 4) * 4
data = data + (n - len(data)) * b"\0"
self.__buf.write(data)

Check warning on line 112 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L108-L112

Added lines #L108 - L112 were not covered by tests

pack_fopaque = pack_fstring

def pack_string(self, s):
n = len(s)
self.pack_uint(n)
self.pack_fstring(n, s)

Check warning on line 119 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L117-L119

Added lines #L117 - L119 were not covered by tests

pack_opaque = pack_string
pack_bytes = pack_string

def pack_list(self, list, pack_item):
for item in list:
self.pack_uint(1)
pack_item(item)
self.pack_uint(0)

Check warning on line 128 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L126-L128

Added lines #L126 - L128 were not covered by tests

def pack_farray(self, n, list, pack_item):
if len(list) != n:
raise ValueError("wrong array size")

Check warning on line 132 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L132

Added line #L132 was not covered by tests
for item in list:
pack_item(item)

Check warning on line 134 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L134

Added line #L134 was not covered by tests

def pack_array(self, list, pack_item):
n = len(list)
self.pack_uint(n)
self.pack_farray(n, list, pack_item)

Check warning on line 139 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L137-L139

Added lines #L137 - L139 were not covered by tests


class Unpacker:
"""Unpacks various data representations from the given buffer."""

def __init__(self, data):
self.reset(data)

Check warning on line 146 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L146

Added line #L146 was not covered by tests

def reset(self, data):
self.__buf = data
self.__pos = 0

Check warning on line 150 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L149-L150

Added lines #L149 - L150 were not covered by tests

def get_position(self):
return self.__pos

Check warning on line 153 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L153

Added line #L153 was not covered by tests

def set_position(self, position):
self.__pos = position

Check warning on line 156 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L156

Added line #L156 was not covered by tests

def get_buffer(self):
return self.__buf

Check warning on line 159 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L159

Added line #L159 was not covered by tests

def done(self):
if self.__pos < len(self.__buf):
raise Error("unextracted data remains")

Check warning on line 163 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L163

Added line #L163 was not covered by tests

def unpack_uint(self):
i = self.__pos
self.__pos = j = i + 4
data = self.__buf[i:j]

Check warning on line 168 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L166-L168

Added lines #L166 - L168 were not covered by tests
if len(data) < 4:
raise EOFError
return struct.unpack(">L", data)[0]

Check warning on line 171 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L170-L171

Added lines #L170 - L171 were not covered by tests

def unpack_int(self):
i = self.__pos
self.__pos = j = i + 4
data = self.__buf[i:j]

Check warning on line 176 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L174-L176

Added lines #L174 - L176 were not covered by tests
if len(data) < 4:
raise EOFError
return struct.unpack(">l", data)[0]

Check warning on line 179 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L178-L179

Added lines #L178 - L179 were not covered by tests

unpack_enum = unpack_int

def unpack_bool(self):
return bool(self.unpack_int())

Check warning on line 184 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L184

Added line #L184 was not covered by tests

def unpack_uhyper(self):
hi = self.unpack_uint()
lo = self.unpack_uint()
return int(hi) << 32 | lo

Check warning on line 189 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L187-L189

Added lines #L187 - L189 were not covered by tests

def unpack_hyper(self):
x = self.unpack_uhyper()

Check warning on line 192 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L192

Added line #L192 was not covered by tests
if x >= 0x8000000000000000:
x = x - 0x10000000000000000
return x

Check warning on line 195 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L194-L195

Added lines #L194 - L195 were not covered by tests

def unpack_float(self):
i = self.__pos
self.__pos = j = i + 4
data = self.__buf[i:j]

Check warning on line 200 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L198-L200

Added lines #L198 - L200 were not covered by tests
if len(data) < 4:
raise EOFError
return struct.unpack(">f", data)[0]

Check warning on line 203 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L202-L203

Added lines #L202 - L203 were not covered by tests

def unpack_double(self):
i = self.__pos
self.__pos = j = i + 8
data = self.__buf[i:j]

Check warning on line 208 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L206-L208

Added lines #L206 - L208 were not covered by tests
if len(data) < 8:
raise EOFError
return struct.unpack(">d", data)[0]

Check warning on line 211 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L210-L211

Added lines #L210 - L211 were not covered by tests

def unpack_fstring(self, n):
if n < 0:
raise ValueError("fstring size must be nonnegative")
i = self.__pos
j = i + (n + 3) // 4 * 4

Check warning on line 217 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L215-L217

Added lines #L215 - L217 were not covered by tests
if j > len(self.__buf):
raise EOFError
self.__pos = j
return self.__buf[i : i + n]

Check warning on line 221 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L219-L221

Added lines #L219 - L221 were not covered by tests

unpack_fopaque = unpack_fstring

def unpack_string(self):
n = self.unpack_uint()
return self.unpack_fstring(n)

Check warning on line 227 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L226-L227

Added lines #L226 - L227 were not covered by tests

unpack_opaque = unpack_string
unpack_bytes = unpack_string

def unpack_list(self, unpack_item):
list = []

Check warning on line 233 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L233

Added line #L233 was not covered by tests
while (x := self.unpack_uint()) != 0:
if x != 1:
raise ConversionError("0 or 1 expected, got %r" % (x,))
item = unpack_item()
list.append(item)
return list

Check warning on line 239 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L236-L239

Added lines #L236 - L239 were not covered by tests

def unpack_farray(self, n, unpack_item):
list = []

Check warning on line 242 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L242

Added line #L242 was not covered by tests
for i in range(n):
list.append(unpack_item())
return list

Check warning on line 245 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L244-L245

Added lines #L244 - L245 were not covered by tests

def unpack_array(self, unpack_item):
n = self.unpack_uint()
return self.unpack_farray(n, unpack_item)

Check warning on line 249 in pyvisa_py/protocols/xdrlib.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/protocols/xdrlib.py#L248-L249

Added lines #L248 - L249 were not covered by tests
Loading