Temporary swap out an item with a new object. Usage: with swap_item(obj, "item", 5): ... This will set obj["item"] to 5 for the duration of the with: block, restoring the old value at the end of the block. If `item` doesn't exist on `obj`, it will be
(obj, item, new_val)
| 1581 | |
| 1582 | @contextlib.contextmanager |
| 1583 | def swap_item(obj, item, new_val): |
| 1584 | """Temporary swap out an item with a new object. |
| 1585 | |
| 1586 | Usage: |
| 1587 | with swap_item(obj, "item", 5): |
| 1588 | ... |
| 1589 | |
| 1590 | This will set obj["item"] to 5 for the duration of the with: block, |
| 1591 | restoring the old value at the end of the block. If `item` doesn't |
| 1592 | exist on `obj`, it will be created and then deleted at the end of the |
| 1593 | block. |
| 1594 | |
| 1595 | The old value (or None if it doesn't exist) will be assigned to the |
| 1596 | target of the "as" clause, if there is one. |
| 1597 | """ |
| 1598 | if item in obj: |
| 1599 | real_val = obj[item] |
| 1600 | obj[item] = new_val |
| 1601 | try: |
| 1602 | yield real_val |
| 1603 | finally: |
| 1604 | obj[item] = real_val |
| 1605 | else: |
| 1606 | obj[item] = new_val |
| 1607 | try: |
| 1608 | yield |
| 1609 | finally: |
| 1610 | if item in obj: |
| 1611 | del obj[item] |
| 1612 | |
| 1613 | def args_from_interpreter_flags(): |
| 1614 | """Return a list of command-line arguments reproducing the current |
no outgoing calls
searching dependent graphs…