80
Breezy supports Python 2.7 and Python 3.5 or later.
80
Bazaar supports Python from 2.4 through 2.6, and in the future we want to
81
support Python 2.7 and 3.0. Avoid using language features added in 2.5,
82
2.6 or 2.7, or features deprecated in Python 3.0. (You can check v3
83
compatibility using the ``-3`` option of Python2.6.)
87
* Don't use the ``with`` statement.
89
* Don't ``from . import``.
91
* Don't use ``try/except/finally``, which is not supported in Python2.4,
92
use separate nested ``try/except`` and ``try/finally`` blocks.
82
95
hasattr and getattr
83
96
===================
94
107
``**kwargs`` in the prototype of a function should be used sparingly.
95
108
It can be good on higher-order functions that decorate other functions,
96
109
such as ``addCleanup`` or ``assertRaises``, or on functions that take only
97
(or almost only) kwargs, where any kwargs can be passed.
110
(or almost only) kwargs, where any kwargs can be passed.
99
112
Otherwise, be careful: if the parameters to a function are a bit complex
100
113
and might vary over time (e.g. the ``commit`` API) then we prefer to pass an
129
142
they don't run inside hot functions.
131
144
* Module names should always be given fully-qualified,
132
i.e. ``breezy.hashcache`` not just ``hashcache``.
145
i.e. ``bzrlib.hashcache`` not just ``hashcache``.
138
151
Functions, methods or members that are relatively private are given
139
152
a leading underscore prefix. Names without a leading underscore are
140
public not just across modules but to programmers using breezy as an
153
public not just across modules but to programmers using bzrlib as an
143
156
We prefer class names to be concatenated capital words (``TestCase``)
176
189
why in a comment.
178
191
1. Never rely on a ``__del__`` method running. If there is code that
179
must run, instead have a ``finally`` block or an ``addCleanup`` call an
180
explicit ``close`` method.
192
must run, do it from a ``finally`` block instead.
182
194
2. Never ``import`` from inside a ``__del__`` method, or you may crash the
185
3. Prior to bzr 2.4, we sometimes used to raise warnings from del methods
186
that the object was not cleaned up or closed. We no longer do this:
187
failure to close the object doesn't cause a test failure; the warning
188
appears an arbitrary long time after the problem occurred (the object
189
being leaked); merely having a del method inhibits Python gc; the
190
warnings appear to users and upset them; they can also break tests that
191
are checking what appears on stderr.
197
3. In some places we raise a warning from the destructor if the object
198
has not been cleaned up or closed. This is considered OK: the warning
199
may not catch every case but it's still useful sometimes.
193
In short, just don't use ``__del__``.
198
205
Often when something has failed later code will fail too, including
199
206
cleanups invoked from ``finally`` blocks. These secondary failures are
200
generally uninteresting compared to the original exception. ``breezy``
207
generally uninteresting compared to the original exception. ``bzrlib``
201
208
has some facilities you can use to mitigate this.
203
210
* In ``Command`` subclasses, prefer the ``add_cleanup`` method to using
211
218
acquire/release) because there isn't a large block of code separating
214
* Use the ``only_raises`` decorator (from ``breezy.decorators``) when
221
* Use the ``only_raises`` decorator (from ``bzrlib.decorators``) when
215
222
defining methods that are typically called in ``finally`` blocks, such
216
223
as ``unlock`` methods. For example, ``@only_raises(LockNotHeld,
217
224
LockBroken)``. All errors that are unlikely to be a knock-on failure
218
225
from a previous failure should be allowed.
220
227
* Consider using the ``OperationWithCleanups`` helper from
221
``breezy.cleanup`` anywhere else you have a ``finally`` block that
228
``bzrlib.cleanup`` anywhere else you have a ``finally`` block that
238
Several places in Breezy use (or will use) a registry, which is a
245
Several places in Bazaar use (or will use) a registry, which is a
239
246
mapping from names to objects or classes. The registry allows for
240
247
loading in registered code only when it's needed, and keeping
241
248
associated information such as a help string or description.
272
To make startup time faster, we use the ``breezy.lazy_import`` module to
279
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
273
280
delay importing modules until they are actually used. ``lazy_import`` uses
274
281
the same syntax as regular python imports. So to import a few modules in a
275
282
lazy fashion do::
277
from breezy.lazy_import import lazy_import
284
from bzrlib.lazy_import import lazy_import
278
285
lazy_import(globals(), """
280
287
import subprocess
287
294
revision as _mod_revision,
289
import breezy.transport
296
import bzrlib.transport
293
300
At this point, all of these exist as a ``ImportReplacer`` object, ready to
327
334
The null revision is the ancestor of all revisions. Its revno is 0, its
328
335
revision-id is ``null:``, and its tree is the empty tree. When referring
329
to the null revision, please use ``breezy.revision.NULL_REVISION``. Old
336
to the null revision, please use ``bzrlib.revision.NULL_REVISION``. Old
330
337
code sometimes uses ``None`` for the null revision, but this practice is
331
338
being phased out.
394
All code should be exercised by the test suite. See the `Breezy Testing
395
Guide <http://www.breezy-vcs.org/developers/testing.html>`_ for detailed
401
All code should be exercised by the test suite. See the `Bazaar Testing
402
Guide <http://doc.bazaar.canonical.com/developers/testing.html>`_ for detailed
396
403
information about writing tests.
408
* It makes the behaviour vary depending on whether brz is run with -O
415
* It makes the behaviour vary depending on whether bzr is run with -O
409
416
or not, therefore giving a chance for bugs that occur in one case or
410
417
the other, several of which have already occurred: assertions with
411
418
side effects, code which can't continue unless the assertion passes,
459
The ``breezy.osutils`` module has many useful helper functions, including
466
The ``bzrlib.osutils`` module has many useful helper functions, including
460
467
some more portable variants of functions in the standard library.
462
469
In particular, don't use ``shutil.rmtree`` unless it's acceptable for it
463
470
to fail on Windows if some files are readonly or still open elsewhere.
464
Use ``breezy.osutils.rmtree`` instead.
471
Use ``bzrlib.osutils.rmtree`` instead.
466
473
Using the ``open(..).read(..)`` or ``open(..).write(..)`` style chaining
467
474
of methods for reading or writing file content relies on garbage collection
489
Bazaar is a GNU project and uses standard GNU terminology, especially:
491
* Use the word "Linux" to refer to the Linux kernel, not as a synechoche
492
for the entire operating system. (See `bug 528253
493
<https://bugs.launchpad.net/bzr/+bug/528253>`_).
495
* Don't say "open source" when you mean "free software".
485
504
* If importing a module, not an attribute, and the module is a top-level
486
505
module (i.e. has no dots in the name), then it's ok to use the builtin
487
506
``__import__``, e.g. ``__import__(module_name)``.
488
* In all other cases, prefer ``breezy.pyutils.get_named_object`` to the
507
* In all other cases, prefer ``bzrlib.pyutils.get_named_object`` to the
489
508
built-in ``__import__``. ``__import__`` has some subtleties and
490
509
unintuitive behaviours that make it hard to use correctly.