Skip to content

Does not work with methods. FIX ADDED #1

@eqvis

Description

@eqvis

Hi,
I already fixed this, but I didn't really understand all the code and I only did some trial/error until it was running. Please take a look and decide...
I was not able to push my branch (git is giving me error 403).
Here is the diff:


diff --git a/src/patch.py b/src/patch.py
index e8eb83e..48175e3 100644
--- a/src/patch.py
+++ b/src/patch.py
@@ -17,9 +17,27 @@ def reg_name(globs, obj):
     return _id

 NOPATCH_CACHE = set()
-NOMORE_CACHE = {}
+NOMORE_CACHE = {}
+
+def getFuncAttr(obj, attr, default=None):
+    if hasattr(obj, '__func__'):
+        return getattr(obj.__func__, attr, default)
+    else:
+        return getattr(obj, attr, default)
+
+def setFuncAttr(obj, attr, value):
+    if hasattr(obj, '__func__'):
+        return setattr(obj.__func__, attr, value)
+    else:
+        return setattr(obj, attr, value)

 def can_patch(obj):
+    if hasattr(obj, "__func__"):
+        obj = obj.__func__
+
+    if not hasattr(obj, "__globals__"):
+        return False
+
     k = obj.__class__

     if k in NOPATCH_CACHE:
@@ -43,7 +61,7 @@ def _decorate(fun, callback):

     _id = callback._pyspy_id

-    if hasattr(fun, _id): #already patched
+    if getFuncAttr(fun, _id): #already patched
         return fun

     nomore = NOMORE_CACHE.get(_id)
@@ -60,7 +78,7 @@ def _decorate(fun, callback):
     if flags:
         if flags & PATCH_NOMORE:
             if can_patch(fun):
-                setattr(fun, callback._pyspy_id, True)
+                setFuncAttr(fun, callback._pyspy_id, True)
             else:
                 nomore.add(k)

@@ -72,7 +90,7 @@ def patch_pre(fun, callback):
     if not can_patch(fun):
         return False

-    callback_name = reg_name(fun.func_globals, callback)
+    callback_name = reg_name(getFuncAttr(fun, "func_globals"), callback)

     patch = [(LOAD_CONST, -1),
              (LOAD_CONST, None),
@@ -86,10 +104,11 @@ def patch_pre(fun, callback):
              (CALL_FUNCTION, 1),
              (POP_TOP, None)]

-    code = Code.from_code(fun.func_code)
+    code = Code.from_code(getFuncAttr(fun, "func_code"))
     code.code[0:0] = patch
-
-    fun.func_code = code.to_code()
+
+    setFuncAttr(fun, "func_code", code.to_code())
+
     return True

 def patch_return(fun, callback):
@@ -99,11 +118,11 @@ def patch_return(fun, callback):
 def patch_calls(fun, callback, **kw):
     if not can_patch(fun):
         return False
-
-    module = fun.func_globals
-
-    callback_name = reg_name(module, callback)
-    wrapper_name  = reg_name(module, _decorate)
+
+    globs = getFuncAttr(fun, "func_globals")
+
+    callback_name = reg_name(globs, callback)
+    wrapper_name  = reg_name(globs, _decorate)

     def gen_patch(call_arg):
         argnum = call_arg & 0xff
@@ -129,7 +148,7 @@ def patch_calls(fun, callback, **kw):

         return replace_call

-    cur_code = Code.from_code(fun.func_code)
+    cur_code = Code.from_code(getFuncAttr(fun, 'func_code'))

     i = 0

@@ -145,4 +164,5 @@ def patch_calls(fun, callback, **kw):
     for i, ins in inserts:
         cur_code.code[i:i] = ins

-    fun.func_code = cur_code.to_code()
+    setFuncAttr(fun, "func_code", cur_code.to_code())
+
\ No newline at end of file

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions