comparison mercurial/dispatch.py @ 50925:d718eddf01d9

safehasattr: drop usage in favor of hasattr The two functions should now be equivalent at least in their usage in core.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 31 Aug 2023 23:56:15 +0200
parents 9a322ccd2fdc
children 18c8c18993f0
comparison
equal deleted inserted replaced
50924:7a8ea1397816 50925:d718eddf01d9
105 105
106 106
107 def _flushstdio(ui, err): 107 def _flushstdio(ui, err):
108 status = None 108 status = None
109 # In all cases we try to flush stdio streams. 109 # In all cases we try to flush stdio streams.
110 if util.safehasattr(ui, 'fout'): 110 if hasattr(ui, 'fout'):
111 assert ui is not None # help pytype 111 assert ui is not None # help pytype
112 assert ui.fout is not None # help pytype 112 assert ui.fout is not None # help pytype
113 try: 113 try:
114 ui.fout.flush() 114 ui.fout.flush()
115 except IOError as e: 115 except IOError as e:
116 err = e 116 err = e
117 status = -1 117 status = -1
118 118
119 if util.safehasattr(ui, 'ferr'): 119 if hasattr(ui, 'ferr'):
120 assert ui is not None # help pytype 120 assert ui is not None # help pytype
121 assert ui.ferr is not None # help pytype 121 assert ui.ferr is not None # help pytype
122 try: 122 try:
123 if err is not None and err.errno != errno.EPIPE: 123 if err is not None and err.errno != errno.EPIPE:
124 ui.ferr.write( 124 ui.ferr.write(
168 # write_through is new in Python 3.7. 168 # write_through is new in Python 3.7.
169 kwargs = { 169 kwargs = {
170 "newline": "\n", 170 "newline": "\n",
171 "line_buffering": sys.stdout.line_buffering, 171 "line_buffering": sys.stdout.line_buffering,
172 } 172 }
173 if util.safehasattr(sys.stdout, "write_through"): 173 if hasattr(sys.stdout, "write_through"):
174 # pytype: disable=attribute-error 174 # pytype: disable=attribute-error
175 kwargs["write_through"] = sys.stdout.write_through 175 kwargs["write_through"] = sys.stdout.write_through
176 # pytype: enable=attribute-error 176 # pytype: enable=attribute-error
177 sys.stdout = io.TextIOWrapper( 177 sys.stdout = io.TextIOWrapper(
178 sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs 178 sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs
181 if sys.stderr is not None: 181 if sys.stderr is not None:
182 kwargs = { 182 kwargs = {
183 "newline": "\n", 183 "newline": "\n",
184 "line_buffering": sys.stderr.line_buffering, 184 "line_buffering": sys.stderr.line_buffering,
185 } 185 }
186 if util.safehasattr(sys.stderr, "write_through"): 186 if hasattr(sys.stderr, "write_through"):
187 # pytype: disable=attribute-error 187 # pytype: disable=attribute-error
188 kwargs["write_through"] = sys.stderr.write_through 188 kwargs["write_through"] = sys.stderr.write_through
189 # pytype: enable=attribute-error 189 # pytype: enable=attribute-error
190 sys.stderr = io.TextIOWrapper( 190 sys.stderr = io.TextIOWrapper(
191 sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs 191 sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs
518 518
519 519
520 def aliasargs(fn, givenargs): 520 def aliasargs(fn, givenargs):
521 args = [] 521 args = []
522 # only care about alias 'args', ignore 'args' set by extensions.wrapfunction 522 # only care about alias 'args', ignore 'args' set by extensions.wrapfunction
523 if not util.safehasattr(fn, '_origfunc'): 523 if not hasattr(fn, '_origfunc'):
524 args = getattr(fn, 'args', args) 524 args = getattr(fn, 'args', args)
525 if args: 525 if args:
526 cmd = b' '.join(map(procutil.shellquote, args)) 526 cmd = b' '.join(map(procutil.shellquote, args))
527 527
528 nums = [] 528 nums = []
706 'optionalrepo': False, 706 'optionalrepo': False,
707 'inferrepo': False, 707 'inferrepo': False,
708 } 708 }
709 if name not in adefaults: 709 if name not in adefaults:
710 raise AttributeError(name) 710 raise AttributeError(name)
711 if self.badalias or util.safehasattr(self, 'shell'): 711 if self.badalias or hasattr(self, 'shell'):
712 return adefaults[name] 712 return adefaults[name]
713 return getattr(self.fn, name) 713 return getattr(self.fn, name)
714 714
715 def __call__(self, ui, *args, **opts): 715 def __call__(self, ui, *args, **opts):
716 if self.badalias: 716 if self.badalias:
732 b'commandalias', 732 b'commandalias',
733 b"alias '%s' expands to '%s'\n", 733 b"alias '%s' expands to '%s'\n",
734 self.name, 734 self.name,
735 self.definition, 735 self.definition,
736 ) 736 )
737 if util.safehasattr(self, 'shell'): 737 if hasattr(self, 'shell'):
738 return self.fn(ui, *args, **opts) 738 return self.fn(ui, *args, **opts)
739 else: 739 else:
740 try: 740 try:
741 return util.checksignature(self.fn)(ui, *args, **opts) 741 return util.checksignature(self.fn)(ui, *args, **opts)
742 except error.SignatureError: 742 except error.SignatureError:
1022 return 1022 return
1023 1023
1024 cmd = aliases[0] 1024 cmd = aliases[0]
1025 fn = entry[0] 1025 fn = entry[0]
1026 1026
1027 if cmd and util.safehasattr(fn, 'shell'): 1027 if cmd and hasattr(fn, 'shell'):
1028 # shell alias shouldn't receive early options which are consumed by hg 1028 # shell alias shouldn't receive early options which are consumed by hg
1029 _earlyopts, args = _earlysplitopts(args) 1029 _earlyopts, args = _earlysplitopts(args)
1030 d = lambda: fn(ui, *args[1:]) 1030 d = lambda: fn(ui, *args[1:])
1031 return lambda: runcommand( 1031 return lambda: runcommand(
1032 lui, None, cmd, args[:1], ui, options, d, [], {} 1032 lui, None, cmd, args[:1], ui, options, d, [], {}