94 @type ast.Constant |
94 @type ast.Constant |
95 @return value of the node |
95 @return value of the node |
96 @rtype any |
96 @rtype any |
97 @exception TypeError raised to indicate an unsupported type |
97 @exception TypeError raised to indicate an unsupported type |
98 """ |
98 """ |
99 if isinstance(node, ast.Constant): |
99 if not isinstance(node, ast.Constant): |
100 return node.value |
|
101 else: |
|
102 raise TypeError("Illegal node type passed.") |
100 raise TypeError("Illegal node type passed.") |
|
101 |
|
102 return node.value |
103 |
103 |
104 else: |
104 else: |
105 # functions for Python < 3.8 |
105 # functions for Python < 3.8 |
106 |
106 |
107 def isNumber(node): |
107 def isNumber(node): |
167 @type one of ast.Num, ast.Str, ast.Bytes or ast.NameConstant |
167 @type one of ast.Num, ast.Str, ast.Bytes or ast.NameConstant |
168 @return value of the node |
168 @return value of the node |
169 @rtype one of str, bytes, int |
169 @rtype one of str, bytes, int |
170 @exception TypeError raised to indicate an unsupported type |
170 @exception TypeError raised to indicate an unsupported type |
171 """ |
171 """ |
|
172 if not isinstance( |
|
173 node, (ast.Num, ast.Str, ast.Bytes, ast.NameConstant) |
|
174 ): |
|
175 raise TypeError("Illegal node type passed.") |
|
176 |
172 if isinstance(node, ast.Num): |
177 if isinstance(node, ast.Num): |
173 return node.n |
178 return node.n |
174 |
179 |
175 elif isinstance(node, ast.Str): |
180 elif isinstance(node, (ast.Str, ast.Bytes)): |
176 return node.s |
|
177 |
|
178 elif isinstance(node, ast.Bytes): |
|
179 return node.s |
181 return node.s |
180 |
182 |
181 elif isinstance(node, ast.NameConstant): |
183 elif isinstance(node, ast.NameConstant): |
182 return node.value |
184 return node.value |
183 |
185 |
184 else: |
186 return None |
185 raise TypeError("Illegal node type passed.") |
|