[Spice-devel,v2,14/43] Decorate writer class to make easier ifdef/endif handling

Submitted by Frediano Ziglio on July 8, 2015, 1:53 p.m.

Details

Message ID 1436363656-4266-15-git-send-email-fziglio@redhat.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Frediano Ziglio July 8, 2015, 1:53 p.m.
I'm generating code for dissector from demarshaller.
Make simple to hangle ifdef/endif not having to check manually attribute.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
---
 python_modules/dissector.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Patch hide | download patch | download mbox

diff --git a/python_modules/dissector.py b/python_modules/dissector.py
index 52234fc..f9ad08a 100644
--- a/python_modules/dissector.py
+++ b/python_modules/dissector.py
@@ -2,6 +2,8 @@ 
 from . import codegen
 import re
 
+import types
+
 
 # generate a new tree identifier
 ett_writer = None
@@ -95,10 +97,26 @@  def write_protocol_definitions(writer):
     writer.end_block()
 
 
+def decorate_writer(writer):
+    cls = writer.__class__
+
+    def create(old):
+        def ifdef(self, member):
+            if member.has_attr("ifdef"):
+                old(self, member.attributes["ifdef"][0])
+        return types.MethodType(ifdef, None, cls)
+
+    cls.ifdef      = create(cls.ifdef)
+    cls.ifdef_else = create(cls.ifdef_else)
+    cls.endif      = create(cls.endif)
+
+
 def write_protocol_parser(writer, proto):
     global hf_writer
     global ett_writer
 
+    decorate_writer(writer)
+
     write_parser_helpers(writer)
 
     # put fields declaration first

Comments

On Wed, Jul 08, 2015 at 02:53:47PM +0100, Frediano Ziglio wrote:
> I'm generating code for dissector from demarshaller.
> Make simple to hangle ifdef/endif not having to check manually attribute.
> 
> Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
> ---
>  python_modules/dissector.py | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/python_modules/dissector.py b/python_modules/dissector.py
> index 52234fc..f9ad08a 100644
> --- a/python_modules/dissector.py
> +++ b/python_modules/dissector.py
> @@ -2,6 +2,8 @@
>  from . import codegen
>  import re
>  
> +import types
> +
>  
>  # generate a new tree identifier
>  ett_writer = None
> @@ -95,10 +97,26 @@ def write_protocol_definitions(writer):
>      writer.end_block()
>  
>  
> +def decorate_writer(writer):
> +    cls = writer.__class__
> +
> +    def create(old):
> +        def ifdef(self, member):
> +            if member.has_attr("ifdef"):
> +                old(self, member.attributes["ifdef"][0])
> +        return types.MethodType(ifdef, None, cls)

This call fails with python3:

Traceback (most recent call last):
  File "../spice_codegen.py", line 228, in <module>
    dissector.write_protocol_parser(writer, proto)
  File "/home/teuf/redhat/spice-common/python_modules/dissector.py", line 1051, in write_protocol_parser
    decorate_writer(writer)
  File "/home/teuf/redhat/spice-common/python_modules/dissector.py", line 1042, in decorate_writer
    cls.ifdef      = create(cls.ifdef)
  File "/home/teuf/redhat/spice-common/python_modules/dissector.py", line 1040, in create
    return types.MethodType(ifdef, None, cls)
TypeError: method expected 2 arguments, got 3
On Thu, Jul 23, 2015 at 10:41:52AM +0200, Christophe Fergeau wrote:
> On Wed, Jul 08, 2015 at 02:53:47PM +0100, Frediano Ziglio wrote:
> > I'm generating code for dissector from demarshaller.
> > Make simple to hangle ifdef/endif not having to check manually attribute.
> > 
> > Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
> > ---
> >  python_modules/dissector.py | 18 ++++++++++++++++++
> >  1 file changed, 18 insertions(+)
> > 
> > diff --git a/python_modules/dissector.py b/python_modules/dissector.py
> > index 52234fc..f9ad08a 100644
> > --- a/python_modules/dissector.py
> > +++ b/python_modules/dissector.py
> > @@ -95,10 +97,26 @@ def write_protocol_definitions(writer):
> >      writer.end_block()
> >  
> >  
> > +def decorate_writer(writer):
> > +    cls = writer.__class__
> > +
> > +    def create(old):
> > +        def ifdef(self, member):
> > +            if member.has_attr("ifdef"):
> > +                old(self, member.attributes["ifdef"][0])
> > +        return types.MethodType(ifdef, None, cls)
> 
> This call fails with python3:

[again, answered to the wrong thread, but still relevant in v3]


> _______________________________________________
> Spice-devel mailing list
> Spice-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/spice-devel