2

Is it possible to override an internal module function without modifications on the given module?

The following example is the best simplification I can imagine. For the original problem look at the end of the question.

I have a package with the following structure:

a
├── b.py
├── c.py
└── __init__.py

Where b.py is

from c import c_func

def b_func():
    print('b.b_func')
    return c_func()

and c.py is

def c_func():
    print('c.c_func')
    return 'return_c'

I want to modify the internal call to c_func() from an external main.py

import a
from a.b import b_func

print('Calling b_func without modification')
solution = b_func()
print(solution)

# Trying to modify the internal function
print('Calling b_func with modification')
old_c_func = a.c.c_func

def new_c_func(*args, **kwargs):
    print('do something in new_c_func')
    return('return_new_c')

a.c.c_func = new_c_func

solution = b_func()
print(solution)

The previous code outputs the following

Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
c.c_func
return_c

But I would expect

Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
do something in new_c_func
return_new_c_func

The original problem relates to a private function of Scipy but I assume that the answer to my question generalises to the following problem:

import scipy

# Stack calls
# minimize calls scipy.optimize._minimize_trust_ncg
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_minimize.py#L463
# _minimize_trust_ncg calls scipy.optimize._trustregion._minimize_trust_region
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_trustregion_ncg.py#L39
from scipy.optimize import minimize

old_minimize_trust_region = scipy.optimize._trustregion._minimize_trust_region

def new_minimize_trust_region(*args, **kwargs):
    print('new function')
    return old_minimize_trust_region

scipy.optimize._trustregion._minimize_trust_region = new_minimize_trust_region

x0 = [2]
fun = lambda x: x**2 + 42
jac = lambda x: 2*x
hess = lambda x: 2

method = 'trust-ncg'
solution = minimize(fun, x0, method=method, jac=jac, hess=hess)

print(solution)
  • I found out that the example works if I modify the import in **b.py** by `import c` instead. And then call the function c as `return c.c_func()`. However, I would like to do the same without modifying any part of the package **a**. – Perello-Nieto Jul 12 '18 at 11:32
  • for future reference, please try to provide a more concise example – diman82 Mar 11 '21 at 11:42

1 Answers1

1

I have found a provisional solution by changing the attribute code of the function to be modified. The following code works as desired:

import a.b
import a.c

print('Calling b_func without modification')
solution = a.b.b_func()
print(solution)

# Trying to modify the internal function
print('Calling b_func with modification')

def new_c_func(*args, **kwargs):
    print('do something in new_c_func')
    return('return new_c_func')

a.c.c_func.__code__ = new_c_func.__code__

solution = a.b.b_func()
print(solution)

I am still missing a full explanation and knowing if the code is appropiate, but it is working as intended. I got inspired by the Stackoverflow question Can you patch just a nested function with closure, or must the whole outer function be repeated?