[p4python] P4.fetch_client() and pickle

Sven Erik Knop sknop at perforce.com
Thu Sep 18 08:01:05 PDT 2008


Trivial? No.

Interesting? Yes

OK, I need to do some testing to prove this, but I am pretty sure the 
problem is that p4c.fetch_client() does not return a dict. It returns an 
object of type P4.Spec, and this object has (purposely) no constructor, 
so the pickler will not be happy.

The solution is to not pickle the Spec object, but convert the Spec into 
a string first. You do this with

   spec = p4.fetch_client(clientName)
   # create a String out of the spec:
   specString = p4.format_client(spec)

Now you can pickle the string. When you unpickle the object, convert 
this back into a spec with the following command

    specString = pickle.load(fromSomeWhere)
    spec = p4.parse_client(specString)
    p4.save_client(spec)

The reason pickle worked in the previous release of P4Python was that 
that version did not return a Spec object but a dict instead - which 
pickle can handle.

Please check if this solves your problem.

Cheers

Sven Erik


Tony Whitley (Hotmail) wrote:
>
> (At the risk of having another trivial error pointed out!) I'm seeing 
> problems with fetch_client() and pickle.dump() and put together this 
> test script
>
>     import P4, pickle, marshal
>
>     p4c = P4.P4()
>
>     p4c.connect()
>
>     info = p4c.run("info") # Run "p4 info" (returns a dict)
>     version = info[0]['serverVersion'] # Get server version
>     print version
>
>     p4client_archive = open( r'c:\p4clients\twhitley_qtf_bits.text', 'wb')
>
>     client = "twhitley_qtf_bits"
>     *spec = p4c.fetch_client(client)*
>     print spec
>
>     # work-round using marshal
>     *str = marshal.dumps(spec)
>     spec = marshal.loads(str)
>     *
>     pickle.dump(spec, p4client_archive)
>     print "pickle.dump works after marshalling/unmarshalling"
>
>     print "but try it without and...\n"
>     spec = p4c.fetch_client(client)
>     pickle.dump(spec, p4client_archive)            (line 24)
>
> It results in
>
>     P4D/LINUX26X86_64/2008.1/164042 (2008/09/07)
>     {'Description': 'Created by twhitley.\n', 'Host': 'twhitley2',
>     'Client': 'twhitley_qtf_bits', 'Owner': 'twhitley', 'LineEnd':
>     'local', 'View': ['//thi
>     rdparty/... //twhitley_qtf_bits/thirdparty/...', '//source/...
>     //twhitley_qtf_bits/source/...', '//depot/...
>     //twhitley_qtf_bits/depot/...', '//deploy
>     /... //twhitley_qtf_bits/deploy/...'], 'Root':
>     'c:\\tools\\p4\\P4Python\\MyClients', 'Options': 'noallwrite
>     noclobber nocompress unlocked nomodtime no
>     rmdir', 'SubmitOptions': 'submitunchanged'}
>     *pickle.dump works after marshalling/unmarshalling
>     but try it without and...*
>
>     Traceback (most recent call last):
>       File "pickletest.py", line 24, in <module>
>         pickle.dump(spec, p4client_archive)
>       File "c:\utils\python\lib\pickle.py", line 1362, in dump
>         Pickler(file, protocol).dump(obj)
>       File "c:\utils\python\lib\pickle.py", line 224, in dump
>         self.save(obj)
>       File "c:\utils\python\lib\pickle.py", line 306, in save
>         rv = reduce(self.proto)
>
> >python --version
> Python 2.5.2
>  
> (I upgraded from 2.5.1 in case something had been fixed)
>  
> Running on Windows XP. P4python 2008.1.
>
>  
>
> Completely consistent, whichever client spec I read.
>
>  
>
> It was never a problem with the earlier "unofficial" p4python. I can 
> use the data in 'spec' without any problems.
>
> --
> /*Tony Whitley*/
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> p4python mailing list
> p4python at perforce.com
> http://maillist.perforce.com/mailman/listinfo/p4python
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://maillist.perforce.com/pipermail/p4python/attachments/20080918/d6e709c1/attachment-0001.html>


More information about the p4python mailing list