Skip to content
Myr

Improved error handling for files.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 2c2f432 · Browse files · 1 file+19 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+19 added−3 removed

  • other:+19 added−3 removed

1 file+19 added−3 removed

modified:click.py+19 added-3 removed
94 unmodified lines
95 os.environ.get('COLUMNS', 80))96 return int(cr[1]), int(cr[0])979899def safecall(func):100 """Wraps a function so that it swallows exceptions."""101 def wrapper(*args, **kwargs):102 try:103 return func(*args, **kwargs)104 except Exception:105 pass106 return wrapper107108109class TextWrapper(textwrap.TextWrapper):110111 def _cutdown(self, ucstr, space_left):112 l = 0
711 unmodified lines
824 # properly close the file. This for instance happens when the825 # type is used with prompts.826 if ctx is not None:827 if was_opened:818 ctx.call_on_close(f.close)828 ctx.call_on_close(safecall(f.close))829 else:820 ctx.call_on_close(f.flush)830 ctx.call_on_close(safecall(f.flush))831 return f832 except (IOError, OSError) as e:833 if isinstance(value, bytes):834 value = value.decode(sys.getfilesystemencoding(), 'replace')835 if hasattr(e, 'strerror'):836 msg = e.strerror837 else:838 msg = str(e)839 if isinstance(msg, bytes):840 msg = msg.decode('utf-8', 'replace')825 self.fail('Could not open file %s: %s' % (value, e),841 self.fail('Could not open file %s: %s' % (value, msg),842 param, ctx)843844845def convert_type(ty, default=None):
708 unmodified lines