2012-03-07 19:52:36

by Weston Andros Adamson

[permalink] [raw]
Subject: [PATCH 1/3] pynfs: Fix bug in xdrgen __repr__ generation

If an enum type is defined as opaque the generated __repr__ function raises
an exception trying to use the list as a key for a dict.get() lookup.

Signed-off-by: Weston Andros Adamson <[email protected]>
---
xdr/xdrgen.py | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/xdr/xdrgen.py b/xdr/xdrgen.py
index 0148f96..6d89bfe 100755
--- a/xdr/xdrgen.py
+++ b/xdr/xdrgen.py
@@ -786,6 +786,9 @@ class Info(object):
if t.type in name_dict:
p = name_dict[t.type]
if p.parent and p.type == 'enum':
+ if t.array:
+ return "( '(' + ', '.join([ const.%s.get(x, x) " \
+ "for x in self.%s ]) + ')' )" % (p.id, t.id)
return "const.%s.get(self.%s, self.%s)" % (p.id, t.id, t.id)
return "repr(self.%s)" % t.id
indent2 = prefix + indent
--
1.7.4.4



2012-03-07 19:52:38

by Weston Andros Adamson

[permalink] [raw]
Subject: [PATCH 2/3] pynfs: use has_key for enum validation in rpcgen

Avoid building a new valid list every encode/decode of an enum,
also do a cheaper lookup using a dict rather than a list.

Signed-off-by: Weston Andros Adamson <[email protected]>
---
xdr/xdrgen.py | 12 ++++--------
1 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/xdr/xdrgen.py b/xdr/xdrgen.py
index 6d89bfe..963e01b 100755
--- a/xdr/xdrgen.py
+++ b/xdr/xdrgen.py
@@ -857,21 +857,17 @@ class Info(object):

def packenum(self, prefix, data='data'):
prefix, data, subheader, array = self._array_pack(prefix, data)
- varlist = ["const.%s" % l.id for l in self.body]
- check = "%sif self.check_enum and %s not in [%s]:\n" \
+ check = "%sif self.check_enum and not const.%s.has_key(%s):\n" \
"%s%sraise XDRError, 'value=%%s not in enum %s' %% %s\n" % \
- (prefix, data, ', '.join(varlist),
- prefix, indent, self.id, data)
+ (prefix, self.id, data, prefix, indent, self.id, data)
pack = check + "%sself.pack_int(%s)\n" % (prefix, data)
return subheader + pack + array

def unpackenum(self, prefix, data='data'):
prefix, data, subheader, array = self._array_unpack(prefix, data)
- varlist = ["const.%s" % l.id for l in self.body]
- check = "%sif self.check_enum and %s not in [%s]:\n" \
+ check = "%sif self.check_enum and not const.%s.has_key(%s):\n" \
"%s%sraise XDRError, 'value=%%s not in enum %s' %% %s\n" % \
- (prefix, data, ', '.join(varlist),
- prefix, indent, self.id, data)
+ (prefix, self.id, data, prefix, indent, self.id, data)
unpack = "%s%s = self.unpack_int()\n" % (prefix, data)
return subheader + unpack + check + array

--
1.7.4.4


2012-03-07 19:52:43

by Weston Andros Adamson

[permalink] [raw]
Subject: [PATCH 3/3] pynfs: use list.append() instead += list in xdrgen

list.append is MUCH faster even when dealing lists with few items

Signed-off-by: Weston Andros Adamson <[email protected]>
---
xdr/xdrgen.py | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/xdr/xdrgen.py b/xdr/xdrgen.py
index 963e01b..1f2a768 100755
--- a/xdr/xdrgen.py
+++ b/xdr/xdrgen.py
@@ -793,7 +793,7 @@ class Info(object):
return "repr(self.%s)" % t.id
indent2 = prefix + indent
reprbody = ''.join(["%sif self.%s is not None:\n" \
- "%s%sout += ['%s=%%s' %% %s]\n" %
+ "%s%sout.append('%s=%%s' %% %s)\n" %
(indent2, var.id, indent2, indent, var.id, special(var))
for var in varlist])
return "%sdef __repr__(self):\n%sout = []\n" \
--
1.7.4.4