[Spice-commits] 10 commits - python_modules/codegen.py python_modules/marshal.py python_modules/ptypes.py python_modules/spice_parser.py

Christophe Fergau teuf at kemper.freedesktop.org
Thu Jul 23 02:25:27 PDT 2015


 python_modules/codegen.py      |   10 ++-
 python_modules/marshal.py      |   29 ++++-------
 python_modules/ptypes.py       |  104 +++++++++++++++++++++++++++++++++--------
 python_modules/spice_parser.py |    4 -
 4 files changed, 105 insertions(+), 42 deletions(-)

New commits:
commit a1d1d396cdd5f5c16765d3769b3f821e4f38cdda
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:40 2015 +0100

    codegen: Allow to specify C type for index variable
    
    This is to prepare to generate the wireshark dissector which uses
    glib types instead of the newer C ones (for compatibility with some
    compilers).
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/codegen.py b/python_modules/codegen.py
index c470988..f7a2048 100644
--- a/python_modules/codegen.py
+++ b/python_modules/codegen.py
@@ -81,6 +81,7 @@ class CodeWriter:
         self.has_error_check = False
         self.options = {}
         self.function_helper_writer = None
+        self.index_type = 'uint32_t'
 
     def set_option(self, opt, value = True):
         self.options[opt] = value
@@ -113,6 +114,7 @@ class CodeWriter:
         self.contents.append(self.out)
         writer.indentation = self.indentation
         writer.at_line_start = self.at_line_start
+        writer.index_type = self.index_type
         writer.generated = self.generated
         writer.options = self.options
         writer.public_prefix = self.public_prefix
@@ -353,7 +355,7 @@ class CodeWriter:
     def pop_index(self):
         index = self.indexes[self.current_index]
         self.current_index = self.current_index + 1
-        self.add_function_variable("uint32_t", index)
+        self.add_function_variable(self.index_type, index)
         return index
 
     def push_index(self):
commit f71727300287eef4cffdcee9ffb3d7b4b45ff732
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:39 2015 +0100

    codegen: Check we don't pop too many indexes

diff --git a/python_modules/codegen.py b/python_modules/codegen.py
index 02ffdb9..c470988 100644
--- a/python_modules/codegen.py
+++ b/python_modules/codegen.py
@@ -357,6 +357,7 @@ class CodeWriter:
         return index
 
     def push_index(self):
+        assert self.current_index > 0
         self.current_index = self.current_index - 1
 
     class Index:
commit f0f578abeea644a8a2ccc9522b0952eab076e2ed
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:38 2015 +0100

    codegen: Remove old ptr32 attribute
    
    This attribute is not used in code.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 5cd7759..efbe9b6 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -108,8 +108,6 @@ valid_attributes={
     # for a switch this indicates that on network
     # it will occupy always the same size (maximum size required for all members)
     'fixedsize',
-    # use 32 bit pointer
-    'ptr32',
 }
 
 attributes_with_arguments={
@@ -616,8 +614,6 @@ class Member(Containee):
         self.container = container
         self.member_type = self.member_type.resolve()
         self.member_type.register()
-        if self.has_attr("ptr32") and self.member_type.is_pointer():
-            self.member_type.set_ptr_size(4)
         for i in propagated_attributes:
             if self.has_attr(i):
                 self.member_type.attributes[i] = self.attributes[i]
commit eff8b1a0e46bc93df2e2aeec3e616eb4d83502f1
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:37 2015 +0100

    codegen: Do some checks on attributes
    
    Verify that the attribute is known. This could help for instance to
    avoid some future typo mistakes.
    We also now have a list of attributes that we can comment for
    documentation purpose.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 845fa73..5cd7759 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -62,11 +62,79 @@ class FixedSize:
 # other members
 propagated_attributes=["ptr_array", "nonnull", "chunk"]
 
+valid_attributes={
+    # embedded/appended at the end of the structure
+    'end',
+    # the C structure contains a pointer to data
+    # for instance we want to write an array to an allocated array
+    'to_ptr',
+    # write output to this C structure
+    'ctype',
+    # prefix for flags/values enumerations
+    'prefix',
+    # used in demarshaller to use directly data from message without a copy
+    'nocopy',
+    # store member array in a pointer
+    # similar to to_ptr but has an additional argument which is the name of a C
+    # field which will store the array length
+    'as_ptr',
+    # do not generate marshall code
+    # used for last members to be able to marshall them manually
+    'nomarshal',
+    # ??? not used by python code
+    'zero_terminated',
+    'marshall',
+    # this pointer member cannot be null
+    'nonnull',
+    # this flag member contains only a single flag
+    'unique_flag',
+    'ptr_array',
+    'outvar',
+    # C structure has an anonymous member (used in switch)
+    'anon',
+    'chunk',
+    # this channel is contained in an #ifdef section
+    # the argument specifies the preprocessor define to check
+    'ifdef',
+    # write this member as zero on network
+    'zero',
+    # specify minor version required for these members
+    'minor',
+    # this member contains the byte count for an array.
+    # the argument is the member name for item count (not bytes)
+    'bytes_count',
+    # this attribute does not exist on the network, fill just structure with the value
+    'virtual',
+    # for a switch this indicates that on network
+    # it will occupy always the same size (maximum size required for all members)
+    'fixedsize',
+    # use 32 bit pointer
+    'ptr32',
+}
+
+attributes_with_arguments={
+    'ctype',
+    'prefix',
+    'as_ptr',
+    'outvar',
+    'ifdef',
+    'minor',
+    'bytes_count',
+    'virtual',
+}
+
 def fix_attributes(attribute_list):
     attrs = {}
     for attr in attribute_list:
         name = attr[0][1:]
         lst = attr[1:]
+        if not name in valid_attributes:
+            raise Exception("Attribute %s not recognized" % name)
+        if not name in attributes_with_arguments:
+            if len(lst) > 0:
+                raise Exception("Attribute %s specified with options" % name)
+        elif len(lst) > 1:
+            raise Exception("Attribute %s has more than 1 argument" % name)
         attrs[name] = lst
     return attrs
 
@@ -139,6 +207,8 @@ class Type:
         _types_by_name[self.name] = self
 
     def has_attr(self, name):
+        if not name in valid_attributes:
+            raise Exception('attribute %s not expected' % name)
         return name in self.attributes
 
 class TypeRef(Type):
@@ -522,6 +592,8 @@ class Containee:
         return not self.is_switch() and self.member_type.is_primitive()
 
     def has_attr(self, name):
+        if not name in valid_attributes:
+            raise Exception('attribute %s not expected' % name)
         return name in self.attributes
 
     def has_minor_attr(self):
commit 179928fceb1fd0cbd0ba331645a1b210dcb4d7d0
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:36 2015 +0100

    codegen: Reuse code to fix attribute from prototype file
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index d031d09..845fa73 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -62,6 +62,14 @@ class FixedSize:
 # other members
 propagated_attributes=["ptr_array", "nonnull", "chunk"]
 
+def fix_attributes(attribute_list):
+    attrs = {}
+    for attr in attribute_list:
+        name = attr[0][1:]
+        lst = attr[1:]
+        attrs[name] = lst
+    return attrs
+
 class Type:
     def __init__(self):
         self.attributes = {}
@@ -178,8 +186,7 @@ class TypeAlias(Type):
         Type.__init__(self)
         self.name = name
         self.the_type = the_type
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def get_type(self, recursive=False):
         if recursive:
@@ -288,8 +295,7 @@ class EnumType(EnumBaseType):
         self.names = names
         self.values = values
 
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def __str__(self):
         return "enum %s" % self.name
@@ -342,8 +348,7 @@ class FlagsType(EnumBaseType):
         self.names = names
         self.values = values
 
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def __str__(self):
         return "flags %s" % self.name
@@ -533,8 +538,7 @@ class Member(Containee):
         Containee.__init__(self)
         self.name = name
         self.member_type = member_type
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def resolve(self, container):
         self.container = container
@@ -636,8 +640,7 @@ class Switch(Containee):
         self.variable = variable
         self.name = name
         self.cases = cases
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def is_switch(self):
         return True
@@ -846,8 +849,7 @@ class StructType(ContainerType):
         self.members_by_name = {}
         for m in members:
             self.members_by_name[m.name] = m
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def __str__(self):
         if self.name == None:
@@ -869,8 +871,7 @@ class MessageType(ContainerType):
         for m in members:
             self.members_by_name[m.name] = m
         self.reverse_members = {} # ChannelMembers referencing this message
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def __str__(self):
         if self.name == None:
@@ -938,8 +939,7 @@ class ChannelType(Type):
         self.base = base
         self.member_name = None
         self.members = members
-        for attr in attribute_list:
-            self.attributes[attr[0][1:]] = attr[1:]
+        self.attributes = fix_attributes(attribute_list)
 
     def __str__(self):
         if self.name == None:
commit 365866c4c9e3fe0acc45f157ff2df5a17c828959
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:35 2015 +0100

    codegen: Remove duplicate variable initialization
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/spice_parser.py b/python_modules/spice_parser.py
index 80b559b..97af8b2 100644
--- a/python_modules/spice_parser.py
+++ b/python_modules/spice_parser.py
@@ -58,7 +58,6 @@ def SPICE_BNF():
         uint64_    = Keyword("uint64").setParseAction(replaceWith(ptypes.uint64))
 
         # keywords
-        channel_   = Keyword("channel")
         enum32_    = Keyword("enum32").setParseAction(replaceWith(32))
         enum16_    = Keyword("enum16").setParseAction(replaceWith(16))
         enum8_     = Keyword("enum8").setParseAction(replaceWith(8))
commit 0130e8dc396fc267027915fc8b3167b0a4497911
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:34 2015 +0100

    codegen: Optimize code indentation and avoid a loop
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/codegen.py b/python_modules/codegen.py
index 55500b7..02ffdb9 100644
--- a/python_modules/codegen.py
+++ b/python_modules/codegen.py
@@ -130,8 +130,7 @@ class CodeWriter:
             return
 
         if self.at_line_start:
-            for i in range(self.indentation):
-                self.out.write(u" ")
+            self.out.write(u" " * self.indentation)
             self.at_line_start = False
         self.out.write(s)
         return self
commit 184feb541a86a697d263e1065e12a01c53bab289
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:33 2015 +0100

    codegen: Fix typo in variable name
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/codegen.py b/python_modules/codegen.py
index f324498..55500b7 100644
--- a/python_modules/codegen.py
+++ b/python_modules/codegen.py
@@ -193,7 +193,7 @@ class CodeWriter:
     def unindent(self):
         self.indentation -= 4
         if self.indentation < 0:
-            self.indenttation = 0
+            self.indentation = 0
 
     def begin_block(self, prefix= "", comment = ""):
         if len(prefix) > 0:
commit aa43c0d61e0b5d309fca5fb2016c9d0a7fb871b1
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:32 2015 +0100

    codegen: Simplify if/else blocks
    
    Blocks were mainly the same, this reduces the amount of code.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/marshal.py b/python_modules/marshal.py
index b77b910..1d38d3d 100644
--- a/python_modules/marshal.py
+++ b/python_modules/marshal.py
@@ -380,25 +380,18 @@ def write_protocol_marshaller(writer, proto, is_server, private_marshallers):
             writer.ifdef(channel.attributes["ifdef"][0])
             writer.header.ifdef(channel.attributes["ifdef"][0])
         if is_server:
-            for m in channel.client_messages:
-                message = m.message_type
-                f = write_message_marshaller(writer, message, is_server, private_marshallers)
-                if channel.has_attr("ifdef") and f not in functions:
-                    functions[f] = channel.attributes["ifdef"][0]
-                elif message.has_attr("ifdef") and f not in functions:
-                    functions[f] = message.attributes["ifdef"][0]
-                else:
-                    functions[f] = True
+            messages = channel.client_messages
         else:
-            for m in channel.server_messages:
-                message = m.message_type
-                f = write_message_marshaller(writer, message, is_server, private_marshallers)
-                if channel.has_attr("ifdef") and f not in functions:
-                    functions[f] = channel.attributes["ifdef"][0]
-                elif message.has_attr("ifdef") and f not in functions:
-                    functions[f] = message.attributes["ifdef"][0]
-                else:
-                    functions[f] = True
+            messages = channel.server_messages
+        for m in messages:
+            message = m.message_type
+            f = write_message_marshaller(writer, message, is_server, private_marshallers)
+            if channel.has_attr("ifdef") and f not in functions:
+                functions[f] = channel.attributes["ifdef"][0]
+            elif message.has_attr("ifdef") and f not in functions:
+                functions[f] = message.attributes["ifdef"][0]
+            else:
+                functions[f] = True
         if channel.has_attr("ifdef"):
             writer.endif(channel.attributes["ifdef"][0])
             writer.header.endif(channel.attributes["ifdef"][0])
commit 86b0568055443b7d729fb629f07e94409013401e
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Tue Jul 21 17:45:31 2015 +0100

    codegen: Import six module before first use
    
    The module is used in the initial try/except so make sure it is
    already imported.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/python_modules/spice_parser.py b/python_modules/spice_parser.py
index d60bb10..80b559b 100644
--- a/python_modules/spice_parser.py
+++ b/python_modules/spice_parser.py
@@ -1,3 +1,5 @@
+import six
+
 try:
     from pyparsing import Literal, CaselessLiteral, Word, OneOrMore, ZeroOrMore, \
             Forward, delimitedList, Group, Optional, Combine, alphas, nums, restOfLine, cStyleComment, \
@@ -8,7 +10,6 @@ except ImportError:
 
 
 from . import ptypes
-import six
 import sys
 
 cvtInt = lambda toks: int(toks[0])


More information about the Spice-commits mailing list