Skip to content

Commit 22d2f3a

Browse files
committed
Move the parsed_cmd conversion def's to module level functions.
Python3.9 does not allow you to put a reference to a class staticmethod in a table and call it from there. Python3.10 and following do allow this, but we still support 3.9. staticmethod was slightly cleaner, but this will do.
1 parent 29d1aca commit 22d2f3a

File tree

2 files changed

+60
-66
lines changed

2 files changed

+60
-66
lines changed

lldb/examples/python/templates/parsed_cmd.py

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,65 @@ def __call__(self, debugger, args_list, exe_ctx, result):
5252
import sys
5353
from abc import abstractmethod
5454

55+
# Some methods to translate common value types. Should return a
56+
# tuple of the value and an error value (True => error) if the
57+
# type can't be converted. These are called internally when the
58+
# command line is parsed into the 'dest' properties, you should
59+
# not need to call them directly.
60+
# FIXME: Need a way to push the conversion error string back to lldb.
61+
def to_bool(in_value):
62+
error = True
63+
value = False
64+
if type(in_value) != str or len(in_value) == 0:
65+
return (value, error)
66+
67+
low_in = in_value.lower()
68+
if low_in in ["y", "yes", "t", "true", "1"]:
69+
value = True
70+
error = False
71+
72+
if not value and low_in in ["n", "no", "f", "false", "0"]:
73+
value = False
74+
error = False
75+
76+
return (value, error)
77+
78+
def to_int(in_value):
79+
#FIXME: Not doing errors yet...
80+
return (int(in_value), False)
81+
82+
def to_unsigned(in_value):
83+
# FIXME: find an unsigned converter...
84+
# And handle errors.
85+
return (int(in_value), False)
86+
87+
translators = {
88+
lldb.eArgTypeBoolean : to_bool,
89+
lldb.eArgTypeBreakpointID : to_unsigned,
90+
lldb.eArgTypeByteSize : to_unsigned,
91+
lldb.eArgTypeCount : to_unsigned,
92+
lldb.eArgTypeFrameIndex : to_unsigned,
93+
lldb.eArgTypeIndex : to_unsigned,
94+
lldb.eArgTypeLineNum : to_unsigned,
95+
lldb.eArgTypeNumLines : to_unsigned,
96+
lldb.eArgTypeNumberPerLine : to_unsigned,
97+
lldb.eArgTypeOffset : to_int,
98+
lldb.eArgTypeThreadIndex : to_unsigned,
99+
lldb.eArgTypeUnsignedInteger : to_unsigned,
100+
lldb.eArgTypeWatchpointID : to_unsigned,
101+
lldb.eArgTypeColumnNum : to_unsigned,
102+
lldb.eArgTypeRecognizerID : to_unsigned,
103+
lldb.eArgTypeTargetID : to_unsigned,
104+
lldb.eArgTypeStopHookID : to_unsigned
105+
}
106+
107+
def translate_value(value_type, value):
108+
try:
109+
return translators[value_type](value)
110+
except KeyError:
111+
# If we don't have a translator, return the string value.
112+
return (value, False)
113+
55114
class LLDBOptionValueParser:
56115
"""
57116
This class holds the option definitions for the command, and when
@@ -63,68 +122,6 @@ def __init__(self):
63122
self.options_dict = {}
64123
self.args_array = []
65124

66-
# Some methods to translate common value types. Should return a
67-
# tuple of the value and an error value (True => error) if the
68-
# type can't be converted. These are called internally when the
69-
# command line is parsed into the 'dest' properties, you should
70-
# not need to call them directly.
71-
# FIXME: Need a way to push the conversion error string back to lldb.
72-
@staticmethod
73-
def to_bool(in_value):
74-
error = True
75-
value = False
76-
if type(in_value) != str or len(in_value) == 0:
77-
return (value, error)
78-
79-
low_in = in_value.lower()
80-
if low_in in ["y", "yes", "t", "true", "1"]:
81-
value = True
82-
error = False
83-
84-
if not value and low_in in ["n", "no", "f", "false", "0"]:
85-
value = False
86-
error = False
87-
88-
return (value, error)
89-
90-
@staticmethod
91-
def to_int(in_value):
92-
#FIXME: Not doing errors yet...
93-
return (int(in_value), False)
94-
95-
@staticmethod
96-
def to_unsigned(in_value):
97-
# FIXME: find an unsigned converter...
98-
# And handle errors.
99-
return (int(in_value), False)
100-
101-
translators = {
102-
lldb.eArgTypeBoolean : to_bool,
103-
lldb.eArgTypeBreakpointID : to_unsigned,
104-
lldb.eArgTypeByteSize : to_unsigned,
105-
lldb.eArgTypeCount : to_unsigned,
106-
lldb.eArgTypeFrameIndex : to_unsigned,
107-
lldb.eArgTypeIndex : to_unsigned,
108-
lldb.eArgTypeLineNum : to_unsigned,
109-
lldb.eArgTypeNumLines : to_unsigned,
110-
lldb.eArgTypeNumberPerLine : to_unsigned,
111-
lldb.eArgTypeOffset : to_int,
112-
lldb.eArgTypeThreadIndex : to_unsigned,
113-
lldb.eArgTypeUnsignedInteger : to_unsigned,
114-
lldb.eArgTypeWatchpointID : to_unsigned,
115-
lldb.eArgTypeColumnNum : to_unsigned,
116-
lldb.eArgTypeRecognizerID : to_unsigned,
117-
lldb.eArgTypeTargetID : to_unsigned,
118-
lldb.eArgTypeStopHookID : to_unsigned
119-
}
120-
121-
@classmethod
122-
def translate_value(cls, value_type, value):
123-
try:
124-
return cls.translators[value_type](value)
125-
except KeyError:
126-
# If we don't have a translator, return the string value.
127-
return (value, False)
128125

129126
# FIXME: would this be better done on the C++ side?
130127
# The common completers are missing some useful ones.
@@ -219,7 +216,7 @@ def set_option_value(self, exe_ctx, opt_name, opt_value):
219216
if "enum_values" in elem:
220217
(value, error) = self.set_enum_value(elem["enum_values"], opt_value)
221218
else:
222-
(value, error) = __class__.translate_value(elem["value_type"], opt_value)
219+
(value, error) = translate_value(elem["value_type"], opt_value)
223220

224221
if error:
225222
return False

lldb/test/API/commands/command/script/add/TestAddParsedCommand.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
class ParsedCommandTestCase(TestBase):
1414
NO_DEBUG_INFO_TESTCASE = True
1515

16-
# This crashes on the x86_64 Debian bot, but the failure is not helpful.
17-
# Disable the test while I try to find a way to reproduce.
18-
@skipIf(py_version=("<=", (3, 9)))
1916
def test(self):
2017
self.pycmd_tests()
2118

0 commit comments

Comments
 (0)