diff --git a/examples/Makefile b/examples/Makefile index 30ececec08c4c907295cf9200c4fe6c593b525fb..cdbef3d2ac044cc003c28a599cd3e1df93fe318a 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -8,8 +8,10 @@ test: echo More to be done... cd simple ; sh compile.sh && sh run.sh $(MAKE) -C twoway test + $(MAKE) -C duck_typing test .PHONY: clean distclean clean distclean: echo To be done... $(MAKE) -C twoway clean + $(MAKE) -C duck_typing clean diff --git a/examples/duck_typing/Makefile b/examples/duck_typing/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..cd5ec76bda5723684e3892efdf82d362d598723e --- /dev/null +++ b/examples/duck_typing/Makefile @@ -0,0 +1,20 @@ +LABCOMM_JAR=../../compiler/labComm.jar +LABCOMM=java -jar $(LABCOMM_JAR) + +all: gen/animal.py + +.PHONY: test +test: gen/animal.py + PYTHONPATH=../../lib/python:gen ./duck_typing.py + + +gen/.dir: + mkdir -p $@ + +gen/%.py: %.lc | gen/.dir + $(LABCOMM) --python=$@ $< + +.PHONY: clean distclean +clean distclean: + rm -rf gen + diff --git a/examples/duck_typing/animal.lc b/examples/duck_typing/animal.lc new file mode 100644 index 0000000000000000000000000000000000000000..3272ae98ecdb765f67651db557704e399222b115 --- /dev/null +++ b/examples/duck_typing/animal.lc @@ -0,0 +1,9 @@ +typedef struct { + string says; +} animal; + +sample animal cow; +sample animal dog; +sample struct { + string says; +} duck; \ No newline at end of file diff --git a/examples/duck_typing/duck_typing.py b/examples/duck_typing/duck_typing.py new file mode 100755 index 0000000000000000000000000000000000000000..3138e47fc40467d23007f5a3273f64fca0ed6a11 --- /dev/null +++ b/examples/duck_typing/duck_typing.py @@ -0,0 +1,36 @@ +#!/usr/bin/python + +import labcomm +import animal +import StringIO + +class Animal: + def __init__(self): + self.says = None + +if __name__ == '__main__': + buf = StringIO.StringIO() + encoder = labcomm.Encoder(labcomm.StreamWriter(buf)) + encoder.add_decl(animal.cow.signature) + encoder.add_decl(animal.dog.signature) + encoder.add_decl(animal.duck.signature) + theAnimal = Animal() + theAnimal.says = 'Moo' + encoder.encode(theAnimal, animal.cow.signature) + theAnimal.says = 'Bow-Wow' + encoder.encode(theAnimal, animal.dog.signature) + theAnimal.says = 'Quack' + encoder.encode(theAnimal, animal.duck.signature) + buf.seek(0) + decoder = labcomm.Decoder(labcomm.StreamReader(buf)) + try: + while True: + value,decl = decoder.decode() + if value: + print decl.name, 'says', value.says + pass + pass + pass + except EOFError: + pass + pass diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py index 604b90139fc443f4a057af93cbe48543f0fb4ff4..2a66397bd46e82ace847832ee6c8870ebf8167d6 100644 --- a/lib/python/labcomm/LabComm.py +++ b/lib/python/labcomm/LabComm.py @@ -431,7 +431,7 @@ class struct: decl.encode(encoder, obj[name]) else: for (name, decl) in self.field: - decl.encode(encoder, obj.__getattribute__(name)) + decl.encode(encoder, getattr(obj, name)) def decode_decl(self, decoder): n_field = decoder.decode_packed32()