basectx: add an empty class that will be used as a parent of all contexts
authorSean Farley <sean.michael.farley@gmail.com>
Sat, 13 Jul 2013 19:59:21 -0500
changeset 19537 6e3e8575276d
parent 19536 ab3cf67740d6
child 19538 049d6b5a4a59
basectx: add an empty class that will be used as a parent of all contexts At the moment, there is no simple way to check if an object is a context because there is no common parent class. If there were, we could use 'isinstance' everywhere. Simply having memctx inherit from workingctx or changectx would allow the use of 'isinstance' but that could lead to some confusing situations of reading the code since we have three distinct concepts of a context: - changectx represents a changeset *already* in the repo, and is therefore immutable - workingctx represents changes on disk in the working directory - memctx represents changes solely in memory which may or may not be on disk Therefore, I propose refactoring context.py to have all three contexts inherit from a parent class 'basectx'.
mercurial/context.py
--- a/mercurial/context.py	Wed Jul 24 19:13:39 2013 -0400
+++ b/mercurial/context.py	Sat Jul 13 19:59:21 2013 -0500
@@ -16,9 +16,20 @@
 
 propertycache = util.propertycache
 
-class changectx(object):
+class basectx(object):
+    """A basectx object represents the common logic for its children:
+    changectx: read-only context that is already present in the repo,
+    workingctx: a context that represents the working directory and can
+                be committed,
+    memctx: a context that represents changes in-memory and can also
+            be committed."""
+    def __new__(cls, repo, changeid='', *args, **kwargs):
+        return super(basectx, cls).__new__(cls)
+
+class changectx(basectx):
     """A changecontext object makes access to data related to a particular
-    changeset convenient."""
+    changeset convenient. It represents a read-only context already presnt in
+    the repo."""
     def __init__(self, repo, changeid=''):
         """changeid is a revision number, node, or tag"""
         if changeid == '':