In parent, replace child_name's old definition with new_child. The parent could be a module when the child is a function at module scope. Or the parent could be a class when a class' method is being replaced. The named child is set to new_child, while the prior definition is saved
(self, parent, child_name, new_child)
| 231 | self.stubs = [] |
| 232 | |
| 233 | def Set(self, parent, child_name, new_child): |
| 234 | """In parent, replace child_name's old definition with new_child. |
| 235 | |
| 236 | The parent could be a module when the child is a function at |
| 237 | module scope. Or the parent could be a class when a class' method |
| 238 | is being replaced. The named child is set to new_child, while the |
| 239 | prior definition is saved away for later, when UnsetAll() is |
| 240 | called. |
| 241 | |
| 242 | This method supports the case where child_name is a staticmethod or a |
| 243 | classmethod of parent. |
| 244 | |
| 245 | Args: |
| 246 | parent: The context in which the attribute child_name is to be changed. |
| 247 | child_name: The name of the attribute to change. |
| 248 | new_child: The new value of the attribute. |
| 249 | """ |
| 250 | old_child = getattr(parent, child_name) |
| 251 | |
| 252 | old_attribute = parent.__dict__.get(child_name) |
| 253 | if old_attribute is not None and isinstance(old_attribute, staticmethod): |
| 254 | old_child = staticmethod(old_child) |
| 255 | |
| 256 | self.cache.append((parent, old_child, child_name)) |
| 257 | setattr(parent, child_name, new_child) |
| 258 | |
| 259 | def UnsetAll(self): |
| 260 | """Reverses Set() calls, restoring things to their original definitions. |