Skip to content

Commit c76a180

Browse files
committed
Fix incomplete patch of argparse.ArgumentParser
Previous patch is applied on an instance of `argparse.ArgumentParser` which is returned by `_setup_root_parser`. However due to existence of these subparsers the patch can not reach the instance returned by `subparsers.add_parser`. So we must do some surgery on `argparse.ArgumentParser`:hammer::hammer::hammer:.
1 parent 3b425b7 commit c76a180

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

bugzilla/_mi.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# This work is licensed under the GNU GPLv2 or later.
1414
# See the COPYING file in the top-level directory.
1515

16+
import argparse
1617
import datetime
1718
import getpass
1819
import json
@@ -91,18 +92,18 @@
9192

9293
class InterruptLoop(Exception): pass
9394

95+
9496
################
9597
# Patch output #
9698
################
9799

98100
def _print_message_patched(self, message, file=None):
99-
""" A patch on an instance of `ArgumentParser`
101+
""" A patch on `argparse.ArgumentParser`
100102
101-
Prepare for Monkey Patch on an instance of
103+
Prepare for Monkey Patch on built-in
102104
`argparse.ArgumentParser` to fit MI.
103105
Later we would use this like
104-
`parser._print_message = types.MethodType(_print_message_patched, parser)` or
105-
`parser._print_message = _print_message_patched.__get__(parser, argparse.ArgumentParser)`
106+
`argparse.ArgumentParser._print_message = _print_message_patched`
106107
"""
107108
if message:
108109
swrite(FLAG_HEAD_ARGINF)
@@ -111,9 +112,9 @@ def _print_message_patched(self, message, file=None):
111112
sflush()
112113

113114
def exit_patched(self, status=0, message=None):
114-
""" A patch on an instance of `ArgumentParser`
115+
""" A patch on `argparse.ArgumentParser`
115116
116-
Prepare for Monkey Patch on an instance of
117+
Prepare for Monkey Patch on built-in
117118
`argparse.ArgumentParser` to fit MI.
118119
Later we would use just like `_print_message_patched`
119120
"""
@@ -231,9 +232,9 @@ def setup_parser():
231232
232233
Redirect argparse output to stdout with our syntax
233234
"""
235+
argparse.ArgumentParser._print_message = _print_message_patched #Monkey Patch
236+
argparse.ArgumentParser.exit = exit_patched #Monkey Patch
234237
rootparser = _setup_root_parser()
235-
rootparser._print_message = types.MethodType(_print_message_patched, rootparser)#Monkey Patch
236-
rootparser.exit = types.MethodType(exit_patched , rootparser)#Monkey Patch
237238
subparsers = rootparser.add_subparsers(dest="command")
238239
subparsers.required = True
239240
_setup_action_new_parser(subparsers)

0 commit comments

Comments
 (0)