(self, context)
| 206 | context.replace(new_node) |
| 207 | |
| 208 | def rewrite_function_call(self, context): |
| 209 | if not isinstance(context.node, Call): |
| 210 | return |
| 211 | |
| 212 | if ( |
| 213 | context.node.full_name is None |
| 214 | and isinstance(context.node.func, Import) |
| 215 | and type(context.node._original) == str |
| 216 | ): |
| 217 | context.node._full_name = context.node.func.names[context.node._original] |
| 218 | return True |
| 219 | |
| 220 | # Replace call to functions by their targets from defined variables, e.g. |
| 221 | # x = open |
| 222 | # x("test.txt") will be replaced to open("test.txt") |
| 223 | try: |
| 224 | if isinstance(context.node.func, Var): |
| 225 | source = context.node._full_name |
| 226 | else: |
| 227 | source = context.node.func |
| 228 | |
| 229 | target = context.stack[source] |
| 230 | if isinstance(target, Import): |
| 231 | name = target.names[source] |
| 232 | else: |
| 233 | name = target.full_name |
| 234 | if ( |
| 235 | type(name) == str |
| 236 | and context.node._full_name != name |
| 237 | and target.line_no != context.node.line_no |
| 238 | ): |
| 239 | context.node._full_name = name |
| 240 | context.visitor.modified = True |
| 241 | return True |
| 242 | except (TypeError, KeyError, AttributeError): |
| 243 | pass |
| 244 | |
| 245 | # Rewrite the `ord('x')` function call |
| 246 | if context.node.full_name == "ord" and type(context.node.args[0]) in (String, str) and len(context.node.args[0]) == 1: |
| 247 | ord_val = ord(str(context.node.args[0])) |
| 248 | new_node = Number(value=ord_val) |
| 249 | new_node.enrich_from_previous(context.node) |
| 250 | context.replace(new_node) |
| 251 | return True |
| 252 | |
| 253 | # Rewrite the `chr(x)` function call |
| 254 | if context.node.full_name == "chr" and type(context.node.args[0]) in (Number, int): |
| 255 | ord_val = int(context.node.args[0]) |
| 256 | try: |
| 257 | chr_val = chr(ord_val) |
| 258 | new_node = String(value=chr_val) |
| 259 | new_node.enrich_from_previous(context.node) |
| 260 | context.replace(new_node) |
| 261 | return True |
| 262 | except ValueError: |
| 263 | pass |
| 264 | |
| 265 | # Rewrite call var arguments |
nothing calls this directly
no test coverage detected