Context manager to ignore specific POSIX error codes. Takes a list of error codes to ignore: this can be either the name of the code, or the code integer itself:: >>> with ignore_errno('ENOENT'): ... with open('foo', 'r') as fh: ... return fh.read()
(*errnos, **kwargs)
| 758 | |
| 759 | @contextmanager |
| 760 | def ignore_errno(*errnos, **kwargs): |
| 761 | """Context manager to ignore specific POSIX error codes. |
| 762 | |
| 763 | Takes a list of error codes to ignore: this can be either |
| 764 | the name of the code, or the code integer itself:: |
| 765 | |
| 766 | >>> with ignore_errno('ENOENT'): |
| 767 | ... with open('foo', 'r') as fh: |
| 768 | ... return fh.read() |
| 769 | |
| 770 | >>> with ignore_errno(errno.ENOENT, errno.EPERM): |
| 771 | ... pass |
| 772 | |
| 773 | Arguments: |
| 774 | types (Tuple[Exception]): A tuple of exceptions to ignore |
| 775 | (when the errno matches). Defaults to :exc:`Exception`. |
| 776 | """ |
| 777 | types = kwargs.get('types') or (Exception,) |
| 778 | errnos = [get_errno_name(errno) for errno in errnos] |
| 779 | try: |
| 780 | yield |
| 781 | except types as exc: |
| 782 | if not hasattr(exc, 'errno'): |
| 783 | raise |
| 784 | if exc.errno not in errnos: |
| 785 | raise |
| 786 | |
| 787 | |
| 788 | def check_privileges(accept_content): |