/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to doc/developers/HACKING.txt

  • Committer: Aaron Bentley
  • Date: 2008-06-06 16:40:46 UTC
  • mfrom: (3482 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3483.
  • Revision ID: aaron@aaronbentley.com-20080606164046-ghbxplxuhtpcb9iz
Merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
650
650
the form of URL components.
651
651
 
652
652
 
653
 
Core Topics
654
 
###########
655
 
 
656
 
Evolving Interfaces
657
 
===================
658
 
 
659
 
We have a commitment to 6 months API stability - any supported symbol in a
660
 
release of bzr MUST NOT be altered in any way that would result in
661
 
breaking existing code that uses it. That means that method names,
662
 
parameter ordering, parameter names, variable and attribute names etc must
663
 
not be changed without leaving a 'deprecated forwarder' behind. This even
664
 
applies to modules and classes.
665
 
 
666
 
If you wish to change the behaviour of a supported API in an incompatible
667
 
way, you need to change its name as well. For instance, if I add an optional keyword
668
 
parameter to branch.commit - that's fine. On the other hand, if I add a
669
 
keyword parameter to branch.commit which is a *required* transaction
670
 
object, I should rename the API - i.e. to 'branch.commit_transaction'. 
671
 
 
672
 
When renaming such supported API's, be sure to leave a deprecated_method (or
673
 
_function or ...) behind which forwards to the new API. See the
674
 
bzrlib.symbol_versioning module for decorators that take care of the
675
 
details for you - such as updating the docstring, and issuing a warning
676
 
when the old api is used.
677
 
 
678
 
For unsupported API's, it does not hurt to follow this discipline, but it's
679
 
not required. Minimally though, please try to rename things so that
680
 
callers will at least get an AttributeError rather than weird results.
681
 
 
682
 
 
683
 
Deprecation decorators
684
 
----------------------
685
 
 
686
 
``bzrlib.symbol_versioning`` provides decorators that can be attached to
687
 
methods, functions, and other interfaces to indicate that they should no
688
 
longer be used.
689
 
 
690
 
To deprecate a static method you must call ``deprecated_function``
691
 
(**not** method), after the staticmethod call::
692
 
 
693
 
    @staticmethod
694
 
    @deprecated_function(zero_ninetyone)
695
 
    def create_repository(base, shared=False, format=None):
696
 
 
697
 
When you deprecate an API, you should not just delete its tests, because
698
 
then we might introduce bugs in them.  If the API is still present at all,
699
 
it should still work.  The basic approach is to use
700
 
``TestCase.applyDeprecated`` which in one step checks that the API gives
701
 
the expected deprecation message, and also returns the real result from
702
 
the method, so that tests can keep running.
703
 
 
704
653
Coding Style Guidelines
705
 
=======================
 
654
#######################
706
655
 
707
656
hasattr and getattr
708
 
-------------------
 
657
===================
709
658
 
710
659
``hasattr`` should not be used because it swallows exceptions including
711
660
``KeyboardInterrupt``.  Instead, say something like ::
714
663
 
715
664
 
716
665
Code layout
717
 
-----------
 
666
===========
718
667
 
719
668
Please write PEP-8__ compliant code.  
720
669
 
803
752
 
804
753
 
805
754
Module Imports
806
 
--------------
 
755
==============
807
756
 
808
757
* Imports should be done at the top-level of the file, unless there is
809
758
  a strong reason to have them lazily loaded when a particular
815
764
 
816
765
 
817
766
Naming
818
 
------
 
767
======
819
768
 
820
769
Functions, methods or members that are "private" to bzrlib are given
821
770
a leading underscore prefix.  Names without a leading underscore are
838
787
 
839
788
 
840
789
Standard Names
841
 
--------------
 
790
==============
842
791
 
843
792
``revision_id`` not ``rev_id`` or ``revid``
844
793
 
847
796
 
848
797
 
849
798
Destructors
850
 
-----------
 
799
===========
851
800
 
852
801
Python destructors (``__del__``) work differently to those of other
853
802
languages.  In particular, bear in mind that destructors may be called
855
804
later time, or possibly never at all.  Therefore we have restrictions on
856
805
what can be done inside them.
857
806
 
858
 
 0. Never use a __del__ method without asking Martin/Robert first.
 
807
 0. If you think you need to use a ``__del__`` method ask another
 
808
    developer for alternatives.  If you do need to use one, explain
 
809
    why in a comment.
859
810
 
860
811
 1. Never rely on a ``__del__`` method running.  If there is code that
861
812
    must run, do it from a ``finally`` block instead.
869
820
 
870
821
 
871
822
Factories
872
 
---------
 
823
=========
873
824
 
874
825
In some places we have variables which point to callables that construct
875
826
new instances.  That is to say, they can be used a lot like class objects,
884
835
 
885
836
 
886
837
Registries
887
 
----------
 
838
==========
888
839
 
889
840
Several places in Bazaar use (or will use) a registry, which is a 
890
841
mapping from names to objects or classes.  The registry allows for 
893
844
 
894
845
 
895
846
Lazy Imports
896
 
------------
 
847
============
897
848
 
898
849
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
899
850
delay importing modules until they are actually used. ``lazy_import`` uses
923
874
the variable is a module, and these object should be hidden anyway, since
924
875
they shouldn't be imported into other namespaces.
925
876
 
926
 
 
927
 
Modules versus Members
928
 
~~~~~~~~~~~~~~~~~~~~~~
929
 
 
930
877
While it is possible for ``lazy_import()`` to import members of a module
931
878
when using the ``from module import member`` syntax, it is recommended to
932
879
only use that syntax to load sub modules ``from module import submodule``.
943
890
This will incorrectly fail, because ``MyClass`` is a ``ImportReplacer``
944
891
object, rather than the real class.
945
892
 
946
 
 
947
 
Passing to Other Variables
948
 
~~~~~~~~~~~~~~~~~~~~~~~~~~
949
 
 
950
893
It also is incorrect to assign ``ImportReplacer`` objects to other variables.
951
894
Because the replacer only knows about the original name, it is unable to
952
895
replace other variables. The ``ImportReplacer`` class will raise an
956
899
 
957
900
 
958
901
The Null revision
959
 
-----------------
 
902
=================
960
903
 
961
904
The null revision is the ancestor of all revisions.  Its revno is 0, its
962
905
revision-id is ``null:``, and its tree is the empty tree.  When referring
965
908
being phased out.
966
909
 
967
910
 
 
911
Object string representations
 
912
=============================
 
913
 
 
914
Python prints objects using their ``__repr__`` method when they are
 
915
written to logs, exception tracebacks, or the debugger.  We want
 
916
objects to have useful representations to help in determining what went
 
917
wrong.
 
918
 
 
919
If you add a new class you should generally add a ``__repr__`` method
 
920
unless there is an adequate method in a parent class.  There should be a
 
921
test for the repr.  
 
922
 
 
923
Representations should typically look like Python constructor syntax, but
 
924
they don't need to include every value in the object and they don't need
 
925
to be able to actually execute.  They're to be read by humans, not
 
926
machines.  Don't hardcode the classname in the format, so that we get the
 
927
correct value if the method is inherited by a subclass.  If you're
 
928
printing attributes of the object, including strings, you should normally
 
929
use ``%r`` syntax (to call their repr in turn).
 
930
 
 
931
Try to avoid the representation becoming more than one or two lines long.
 
932
(But balance this against including useful information, and simplicity of
 
933
implementation.)
 
934
 
 
935
Because repr methods are often called when something has already gone
 
936
wrong, they should be written more defensively than most code.  The object
 
937
may be half-initialized or in some other way in an illegal state.  The
 
938
repr method shouldn't raise an exception, or it may hide the (probably
 
939
more useful) underlying exception.
 
940
 
 
941
Example::
 
942
 
 
943
    def __repr__(self):
 
944
        try:
 
945
            return '%s(%r)' % (self.__class__.__name__,
 
946
                               self._transport)
 
947
        except:
 
948
            return 'FooObject(**unprintable**)'
 
949
 
 
950
 
 
951
Core Topics
 
952
###########
 
953
 
 
954
Evolving Interfaces
 
955
===================
 
956
 
 
957
We have a commitment to 6 months API stability - any supported symbol in a
 
958
release of bzr MUST NOT be altered in any way that would result in
 
959
breaking existing code that uses it. That means that method names,
 
960
parameter ordering, parameter names, variable and attribute names etc must
 
961
not be changed without leaving a 'deprecated forwarder' behind. This even
 
962
applies to modules and classes.
 
963
 
 
964
If you wish to change the behaviour of a supported API in an incompatible
 
965
way, you need to change its name as well. For instance, if I add an optional keyword
 
966
parameter to branch.commit - that's fine. On the other hand, if I add a
 
967
keyword parameter to branch.commit which is a *required* transaction
 
968
object, I should rename the API - i.e. to 'branch.commit_transaction'. 
 
969
 
 
970
When renaming such supported API's, be sure to leave a deprecated_method (or
 
971
_function or ...) behind which forwards to the new API. See the
 
972
bzrlib.symbol_versioning module for decorators that take care of the
 
973
details for you - such as updating the docstring, and issuing a warning
 
974
when the old api is used.
 
975
 
 
976
For unsupported API's, it does not hurt to follow this discipline, but it's
 
977
not required. Minimally though, please try to rename things so that
 
978
callers will at least get an AttributeError rather than weird results.
 
979
 
 
980
 
 
981
Deprecation decorators
 
982
----------------------
 
983
 
 
984
``bzrlib.symbol_versioning`` provides decorators that can be attached to
 
985
methods, functions, and other interfaces to indicate that they should no
 
986
longer be used.  For example::
 
987
 
 
988
   @deprecated_method(deprecated_in((0, 1, 4)))
 
989
   def foo(self):
 
990
        return self._new_foo()
 
991
 
 
992
To deprecate a static method you must call ``deprecated_function``
 
993
(**not** method), after the staticmethod call::
 
994
 
 
995
    @staticmethod
 
996
    @deprecated_function(deprecated_in((0, 1, 4)))
 
997
    def create_repository(base, shared=False, format=None):
 
998
 
 
999
When you deprecate an API, you should not just delete its tests, because
 
1000
then we might introduce bugs in them.  If the API is still present at all,
 
1001
it should still work.  The basic approach is to use
 
1002
``TestCase.applyDeprecated`` which in one step checks that the API gives
 
1003
the expected deprecation message, and also returns the real result from
 
1004
the method, so that tests can keep running.
 
1005
 
 
1006
Deprecation warnings will be suppressed for final releases, but not for
 
1007
development versions or release candidates, or when running ``bzr
 
1008
selftest``. This gives developers information about whether their code is
 
1009
using deprecated functions, but avoids confusing users about things they
 
1010
can't fix.
 
1011
 
 
1012
 
968
1013
Getting Input
969
1014
=============
970
1015
 
1208
1253
 
1209
1254
 
1210
1255
Assertions
1211
 
----------
 
1256
==========
1212
1257
 
1213
1258
Do not use the Python ``assert`` statement, either in tests or elsewhere.
1214
1259
A source test checks that it is not used.  It is ok to explicitly raise