0

I have this code where "clr" and "Assembly" are parts of C library in Linux:

import clr
from System.Reflection import Assembly
import functools

def prefix_function(function, prefunction):
    @functools.wraps(function)
    def run(*args, **kwargs):
        prefunction(*args, **kwargs)
        return function(*args, **kwargs)
    return run

def this_is_a_function():
    print("Hook function called instead of the original!")

# This gives me error "attribute is read-only"
Assembly.GetEntryAssembly = prefix_function(
    Assembly.GetEntryAssembly, this_is_a_function)

# Should print:
# "Hook function called instead of the original!"
Assembly.GetEntryAssembly()

Is it possible to somehow hook calling to static function "GetEntryAssembly()" as well as calls to any functions in C libraries ? I need to modify logic and return result of the original "GetEntryAssembly()". So, in the example above I wish to print "Hook function called" when call to GetEntryAssembly() is made.

I can't subclass, but could accept overwrite kind of "pointers" like in C code.

Also I tried this answer, but read-only attribute error occurs.

DBenson
  • 377
  • 3
  • 12
  • 1
    "Also I tried this answer, but read-only attribute error occurs." We can only tell you what is wrong with [code that you actually show to us](https://stackoverflow.com/help/minimal-reproducible-example), and we can only possibly understand an error message [that we can actually see](https://meta.stackoverflow.com/questions/359146). (Also, please don't ask "is X possible?" if you've already found answers about doing X. Ask the question you *actually have*, which is "what is the problem with this attempt to do X?" - after trying to figure it out yourself first, and showing the attempt.) – Karl Knechtel Jan 19 '22 at 10:49
  • @KarlKnechtel changed code according to your comment. Thanks. – DBenson Jan 19 '22 at 11:00
  • It's not really clear what you mean by "hook in". If you want to modify the result, then operate on the result – it's your code afterall. If, as ``Your own code here that will be run before`` suggests, this is some exercise, please clearly state the problem requirements – see [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – MisterMiyagi Jan 19 '22 at 11:04
  • @MisterMiyagi thanks for you comment. I've improved code. I need to call "this_is_a_function()" instead of the original "GetEntryAssembly()" when call to "GetEntryAssembly()" made . – DBenson Jan 19 '22 at 11:10
  • Note that in the code, ``prefix_function`` will *already* "hook" a call to ``this_is_a_function`` before every call of ``Assembly.GetEntryAssembly``. It won't happen "in C code", though. – MisterMiyagi Jan 19 '22 at 11:11
  • The code above doesn't work, cuz I've got an error ```attribute is read-only``` at the ```Assembly.GetEntryAssembly = prefix_function(Assembly.GetEntryAssembly, this_is_a_function)``` So, I assume it is not hooked. – DBenson Jan 19 '22 at 11:16
  • Okay, your recent edit made that clearer indeed. Can you say anything about ``clr`` (is it even relevant, seeing how it is not used) and ``System``/``System.Reflection``/``System.Reflection.Assembly``? I cannot find them in the usual packaging indices. Are you by any chance trying to hook into *the* [CLR](https://en.wikipedia.org/wiki/Common_Language_Runtime)? – MisterMiyagi Jan 19 '22 at 11:30
  • "Clr" is a part of "mono-devel" package and a libmono. To use it also need to install Pythonnet. Function GetEntryAssembly() is a part of "mscorlib.dll.so" which is loaded into Python after importing clr. No, I'm not trying to hook IL code, cuz it is a different story. I'm just need to make any call to "GetEntryAssembly()" pointing my to my function, but not any function from "*.so" or other libraries. – DBenson Jan 19 '22 at 12:25

0 Answers0