remove numpy dependency
RFC PATCH 1/1 remove numpy dependency
Users tend to hit an ImportError when running analyze-migration.py due to the numpy dependency. numpy functionality isn't actually used, just binary serialization that the standard library 'struct' module already provides. Removing the dependency allows the script to run out-of-the-box.
--- scripts/analyze-migration.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
scripts/analyze-migration.py +15 -8
@@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, see <http://www.gnu.org/licenses/>. -import numpy as np import json import os import argparse
@@ -36,23 +35,29 @@ class MigrationFile(object): self.file = open(self.filename, "rb") def read64(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0]) + buffer = file.read(64) + return struct.unpack('>i16', buffer)[0] def read32(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0]) + buffer = file.read(32) + return struct.unpack('>i8', buffer)[0] def read16(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0]) + buffer = file.read(16) + return struct.unpack('>i4', buffer)[0] def read8(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0]) + buffer = file.read(8) + return struct.unpack('>i2', buffer)[0] def readstr(self, len = None): + read_format = str(len) + 'd' if len is None: len = self.read8() if len == 0: return "" - return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0] + buffer = file.read(8) + return struct.unpack(read_format, buffer[0:(0 + struct.calcsize(read_format))]) def readvar(self, size = None): if size is None:
@@ -303,8 +308,10 @@ class VMSDFieldInt(VMSDFieldGeneric): def read(self): super(VMSDFieldInt, self).read() - self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0] - self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0] + buffer = file.read(self.data) + read_format = self.sdtype + self.sdata = struct.unpack(self.sdtype, buffer[0:(0 + struct.calcsize(self.sdtype))]) + self.sdata = struct.unpack(self.udtype, buffer[0:(0 + struct.calcsize(self.udtype))]) self.data = self.sdata return self.data
Signature · 1 line
2.7.4
This series seems to have some coding style problems. See output below for more information:
Hi,
This series seems to have some coding style problems. See output below for more information:
Subject: [Qemu-devel] [RFC PATCH] remove numpy dependency
Type: series
Message-id: [email protected]
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
git config --local diff.renamelimit 0
git config --local diff.renames True
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'd0c6cb1cd0 remove numpy dependency
=== OUTPUT BEGIN ===
Checking PATCH 1/1: remove numpy dependency...
WARNING: line over 80 characters
#58: FILE: scripts/analyze-migration.py:60:
+ return struct.unpack(read_format, buffer[0:(0 + struct.calcsize(read_format))])
ERROR: line over 90 characters
#70: FILE: scripts/analyze-migration.py:313:
+ self.sdata = struct.unpack(self.sdtype, buffer[0:(0 + struct.calcsize(self.sdtype))])
ERROR: line over 90 characters
#71: FILE: scripts/analyze-migration.py:314:
+ self.sdata = struct.unpack(self.udtype, buffer[0:(0 + struct.calcsize(self.udtype))])
total: 2 errors, 1 warnings, 53 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]
Quoted 24 lines · Joannah Nanjekye
Users tend to hit an ImportError when running
analyze-migration.pydue to the numpy dependency. numpy functionality isn't actually used, just binary serialization that the standard library 'struct' module already provides. Removing the dependency allows the script to run out-of-the-box.Signed-off-by Joannah Nanjekye <[email protected]>--- scripts/analyze-migration.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)scripts/analyze-migration.py +0
-1@@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, see <http://www.gnu.org/licenses/>. -import numpy as np import json import os import argparse
scripts/analyze-migration.py class MigrationFile(object): · line 39
Quoted 4 lines
scripts/analyze-migration.py +0
-1@@ -36,23 +35,29 @@ class MigrationFile(object): self.file = open(self.filename, "rb") def read64(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0])
dtype='>i8' is a 64-bit (8 bytes) big-endian signed integer according to
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html#arrays-dtypes-constructing.
scripts/analyze-migration.py class MigrationFile(object): · line 38
Quoted 1 line
scripts/analyze-migration.py +1
-0+ buffer = file.read(64)
This reads 64 bytes (not bits!). It should read 8 bytes.
scripts/analyze-migration.py class MigrationFile(object): · line 39
Quoted 1 line
scripts/analyze-migration.py +1
-0+ return struct.unpack('>i16', buffer)[0]
This should be '>q' according to
https://docs.python.org/2.7/library/struct.html#format-strings.
scripts/analyze-migration.py class MigrationFile(object): · line 42
Quoted 4 lines
scripts/analyze-migration.py +1
-1def read32(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0]) + buffer = file.read(32)
read(4)
scripts/analyze-migration.py class MigrationFile(object): · line 43
Quoted 1 line
scripts/analyze-migration.py +1
-0+ return struct.unpack('>i8', buffer)[0]
'>i'
scripts/analyze-migration.py class MigrationFile(object): · line 46
Quoted 4 lines
scripts/analyze-migration.py +1
-1def read16(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0]) + buffer = file.read(16)
read(2)
scripts/analyze-migration.py class MigrationFile(object): · line 47
Quoted 1 line
scripts/analyze-migration.py +1
-0+ return struct.unpack('>i4', buffer)[0]
'>h'
scripts/analyze-migration.py class MigrationFile(object): · line 50
Quoted 4 lines
scripts/analyze-migration.py +1
-1def read8(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0]) + buffer = file.read(8)
read(1)
scripts/analyze-migration.py class MigrationFile(object): · line 51
Quoted 1 line
scripts/analyze-migration.py +1
-0+ return struct.unpack('>i2', buffer)[0]
'b'
scripts/analyze-migration.py class MigrationFile(object): · line 60
Quoted 10 lines
scripts/analyze-migration.py +3
-1def readstr(self, len = None): + read_format = str(len) + 'd' if len is None: len = self.read8() if len == 0: return "" - return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0] + buffer = file.read(8) + return struct.unpack(read_format, buffer[0:(0 + struct.calcsize(read_format))])
According to the numpy documentation 'S' produces raw bytes (not a unicode string). To get the raw bytes we just need file.read(len). The struct module isn't needed.
Something like this should work:
if len is None:
len = self.read8()
if len == 0:
return ""
return file.read(len).split(b'\0')[0]
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 311
Quoted 10 lines
scripts/analyze-migration.py +1
-2def readvar(self, size = None): if size is None:@@ -303,8 +308,10 @@ class VMSDFieldInt(VMSDFieldGeneric): def read(self): super(VMSDFieldInt, self).read() - self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0] - self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0] + buffer = file.read(self.data)
This statement doesn't make sense. According to the Python documentation:
read([size]) -> read at most size bytes, returned as a string.
self.data *is* the buffer. There's no need to call file.read().
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 312
Quoted 1 line
scripts/analyze-migration.py +1
-0+ read_format = self.sdtype
Unused.
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 313
Quoted 1 line
scripts/analyze-migration.py +1
-0+ self.sdata = struct.unpack(self.sdtype, buffer[0:(0 + struct.calcsize(self.sdtype))])
struct.calcsize() is unnecessary since self.size already contains the size in bytes.
struct.unpack returns a tuple and we need the first element, so it should be:
self.sdata = struct.unpack(...)[0]
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 314
Quoted 1 line
scripts/analyze-migration.py +1
-0+ self.sdata = struct.unpack(self.udtype, buffer[0:(0 + struct.calcsize(self.udtype))])
The data type specifiers are incorrect because the struct module uses different syntax than numpy:
self.sdtype = '>i%d' % self.size
self.udtype = '>u%d' % self.size
The struct data type specifiers can be looked up like this:
sdtypes = {1: 'b', 2: '>h', 4: '>i', 8: '>q'}
udtypes = {1: 'B', 2: '>H', 4: '>I', 8: '>Q'}
self.sdtype = sdtypes[self.size]
self.udtype = udtypes[self.size]