[bzr-loom:MERGE] Import faster (via lazy_import) with bonus bugfix

John Arbash Meinel john at arbash-meinel.com
Mon Jan 19 13:38:36 GMT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Bennetts wrote:
> This patch cuts down the cost of importing the bzr-loom plugin a little bit.
> More could be done, but this is just some particularly low-hanging fruit.
> 
> It also fixes a trivial bug found by pyflakes: BranchFormat was used by not
> imported.
> 
> -Andrew.
> 
> 


- -from StringIO import StringIO

 import bzrlib.branch
 from bzrlib import bzrdir
@@ -38,8 +37,12 @@
 import bzrlib.tree
 import bzrlib.urlutils

+from bzrlib.lazy_import import lazy_import
+lazy_import(globals(), """
+from cStringIO import StringIO
 import loom_io
 import loom_state
+""")


^- I don't think there is any reason to lazy import "StringIO" as it is
going to be used all over the place. I certainly also recommend not
lazily importing classes, it works most of the time, until you try
something like "isinstance(foo, LazyClass)". Lazy importing modules is a
lot safer because you never use them 'as is' (you always use an attribute).

Anyway, if tests are failing, it *might* be because you are using
cStringIO.StringIO rather than StringIO.StringIO.

The former doesn't really let you write Unicode strings, while the
latter will silently upgrade its internal string from an 8-bit str to a
Unicode string (which I think is a bug, but is how it exists).

So *if* tests are failing because of this, I would recommend fixing the
tests/code rather than changing back to StringIO.StringIO.

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkl0glwACgkQJdeBCYSNAAORqgCgpo/DCvEcexFio1dWuBY3y/Me
GTIAnA5JVl+3fzbEPVWIi7f/Gy5vUXGi
=GPOT
-----END PGP SIGNATURE-----



More information about the bazaar mailing list