Create a copy of a function, but with a new name.
(f, new_name)
| 420 | |
| 421 | |
| 422 | def rename_function(f, new_name): |
| 423 | """Create a copy of a function, but with a new name.""" |
| 424 | f = type(f)( |
| 425 | f.__code__.replace( |
| 426 | co_name=new_name, |
| 427 | **( |
| 428 | { |
| 429 | "co_qualname": re.sub( |
| 430 | r"\.[^.+]\Z", "." + new_name, f.__code__.co_qualname |
| 431 | ) |
| 432 | if "." in f.__code__.co_qualname |
| 433 | else new_name |
| 434 | } |
| 435 | if PY3_11 |
| 436 | else {} |
| 437 | ), |
| 438 | ), |
| 439 | f.__globals__, |
| 440 | str(new_name), |
| 441 | f.__defaults__, |
| 442 | f.__closure__, |
| 443 | ) |
| 444 | f.__dict__.update(f.__dict__) |
| 445 | return f |
no test coverage detected