Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Replies: 0 (Who?), Viewed: 68 times.
Test Subject
Original Poster
#1 Old 17th Jan 2023 at 2:51 AM Last edited by JohnHubner : 17th Jan 2023 at 9:01 PM.
Default TS4 Scripting - need help with malfunctioning injector
Hi everyone.

I wonder if someone might be able to help me. The injector in my script doesn't seem to be able to wrap the function I want it to. I can get it to wrap other functions, just not the one I want to use. Can anyone see anything wrong with my injector code?

I'm able to get @inject_to to wrap (Sim, 'on_add'), and the script works fine when I do that. But, I can't get it to wrap (traits.trait_tracker.TraitTracker, '_add_default_gender_option_traits'). When I do that, the script just doesn't run.
Is there something different about the type of the second function and I'm accessing it wrong?
Thanks in advance for any help.

Code:
import traits.trait_tracker
from functools import wraps


# some bunch of code that does the stuff I want


def inject(target_function, new_function):
    @wraps(target_function)
    def _inject(*args, **kwargs):
        return new_function(target_function, *args, **kwargs)
    return _inject


def inject_to(target_object, target_function_name):
    def _inject_to(new_function):
        target_function = getattr(target_object, target_function_name)
        setattr(target_object, target_function_name, inject(target_function, new_function))
        return new_function
    return _inject_to


@inject_to(traits.trait_tracker.TraitTracker, '_add_default_gender_option_traits')
def john_h_add_custom_gender_traits(original, self, *args, **kwargs):
    result = original(self, *args, **kwargs)
    try:
        john_h_custom_gender_traits(self.sim_info)
    except Exception as e:
        # use a custom error logger here if you have one, if not, just use the below line
        raise Exception(f"Error with Tifyer3 by John H: {str(e)}")
    return result

Back to top