On Thu, Feb 19, 2009 at 7:37 PM, Petar Petrov <pesho.pet...@gmail.com>wrote:

>
> On Wed, Feb 18, 2009 at 6:48 AM, bart van deenen <bart.vandee...@gmail.com
> > wrote:
>
>>
>> Hi
>>
>> I'm building a generic  editor for creating loading and saving gpb
>> objects and am running into problems with dynamic __import__
>> statements. I've nailed it down to a very simple testcase. This is all
>> with python 2.5.2 and protobuf 2.0.3 on Linux.
>>
>> test.proto:
>> message test {    optional int32 x=1; }
>>
>> C.py:
>> class K:
>>    def __init__(self):
>>        print "new K"
>>
>> >>> import test_pb2, C
>> >>> a=getattr(test_pb2,"test")
>> >>> a
>> <class 'test_pb2.test'>
>> >>> b=getattr(C,"K")
>> >>> b
>> <class C.K at 0xb64ceddc>
>>
>> Why is one of the classnames in quotes, and the other not?
>>
>> When I try a dynamic import I've narrowed my problem down to :
>>
>> #!/usr/bin/python
>> def test():
>>    for module,klass in [('test_pb2','test'), ('C', 'K')]:
>>        m=__import__(module)
>>        k=getattr(m, klass)
>>        print "*", m, k, type(k)
>>        print k()
>> test()
>>
>> Which when run gives me the following
>> * <module 'test_pb2' from '....gpbedit/test_pb2.pyc'> <class
>> 'test_pb2.test'> <class
>> 'google.protobuf.reflection.GeneratedProtocolMessageType'>
>> new k=
>
>
> Are you sure that it's not printing the protocol message in ASCII, which
> would be an empty string ('')?
>

That's exactly what happens, and it's expected. The "print k()" actually
outputs the string k().__str__(), which is the ASCII representation of the
protocol buffer. That in your case is ''.

I'm doing this (and it's perfectly ok):
pe...@xxx:/tmp/ttt$ python
Python 2.4.3 ...
[GCC 4.0.3 ...] on ...
Type "help", "copyright", "credits" or "license" for more information.
>>> m=__import__('test_pb2')
>>> cls = getattr(m, 'test')
>>> inst = cls()
>>> print inst

>>> inst.x = 123
>>> print inst
x: 123

Note that the first "print inst" prints an empty string, because the
protocol message is empty. The second one prints a non-empty string because
there are some fields which were set in the protocol message.


>
> I'll take a look into the details, but this is the first things that comes
> to mind.
>
> Another thing is to try using the "imp" module's find_module/load_module
> functions.
>
>
>> * <module 'C' from '...gpbedit/C.pyc'> C.K <type 'classobj'>
>> new k= new K
>> <C.K instance at 0xb7d2764c>
>>
>> When I try to create a protocol object of the 'test' type, my python
>> script returns None.
>>
>> So where does this go wrong?
>>
>> Bart
>>
>> P.S. the editor is at http://github.com/bvdeenen/gpbedit/tree/master.
>> It's still alpha quality, but it is capable of editing proto objects
>> and generating a gui from proto files
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to