Patchwork [04,of,10] chgserver: add utilities to wrap and unwrap functions

login
register
mail settings
Submitter Jun Wu
Date June 30, 2016, 4:58 p.m.
Message ID <c02da7bd56e98cf4e905.1467305939@x1c>
Download mbox | patch
Permalink /patch/15683/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - June 30, 2016, 4:58 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1467279489 -3600
#      Thu Jun 30 10:38:09 2016 +0100
# Node ID c02da7bd56e98cf4e905550859031079ed9beb18
# Parent  169a0f4db80746bcd7ae1e04ae1daa2e4e0d9872
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r c02da7bd56e9
chgserver: add utilities to wrap and unwrap functions

We want to wrap and unwrap functions. So we record what is wrapped and
restore them later.

Patch

diff --git a/hgext/chgserver.py b/hgext/chgserver.py
--- a/hgext/chgserver.py
+++ b/hgext/chgserver.py
@@ -713,3 +713,18 @@  def uisetup(ui):
     # start another chg. drop it to avoid possible side effects.
     if 'CHGINTERNALMARK' in os.environ:
         del os.environ['CHGINTERNALMARK']
+
+def _wrapfunc(obj, name, newfunc, wraplist):
+    # like extensions.wrapfunction, but log what is wrapped to wraplist
+    oldfunc = getattr(obj, name)
+    extensions.wrapfunction(obj, name, newfunc)
+    newfunc = getattr(obj, name)
+    wraplist.append((obj, name, oldfunc, newfunc))
+
+def _unwrapfuncs(wraplist):
+    for obj, name, oldfunc, newfunc in wraplist:
+        if getattr(obj, name) == newfunc:
+            setattr(obj, name, oldfunc)
+        else:
+            raise RuntimeError('%s cannot be restored' % name)
+    wraplist[:] = []