paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Common functions. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
paul@0 | 7 | 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | |
paul@0 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | version. |
paul@0 | 13 | |
paul@0 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | details. |
paul@0 | 18 | |
paul@0 | 19 | You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | from errors import * |
paul@0 | 24 | from os import listdir, makedirs, remove |
paul@0 | 25 | from os.path import exists, isdir, join, split |
paul@0 | 26 | import compiler |
paul@0 | 27 | |
paul@0 | 28 | class CommonOutput: |
paul@0 | 29 | |
paul@0 | 30 | "Common output functionality." |
paul@0 | 31 | |
paul@0 | 32 | def check_output(self): |
paul@0 | 33 | |
paul@0 | 34 | "Check the existing output and remove it if irrelevant." |
paul@0 | 35 | |
paul@0 | 36 | if not exists(self.output): |
paul@0 | 37 | makedirs(self.output) |
paul@0 | 38 | |
paul@0 | 39 | details = self.importer.get_cache_details() |
paul@0 | 40 | recorded_details = self.get_output_details() |
paul@0 | 41 | |
paul@0 | 42 | if recorded_details != details: |
paul@0 | 43 | self.remove_output() |
paul@0 | 44 | |
paul@0 | 45 | writefile(self.get_output_details_filename(), details) |
paul@0 | 46 | |
paul@0 | 47 | def get_output_details_filename(self): |
paul@0 | 48 | |
paul@0 | 49 | "Return the output details filename." |
paul@0 | 50 | |
paul@0 | 51 | return join(self.output, "$details") |
paul@0 | 52 | |
paul@0 | 53 | def get_output_details(self): |
paul@0 | 54 | |
paul@0 | 55 | "Return details of the existing output." |
paul@0 | 56 | |
paul@0 | 57 | details_filename = self.get_output_details_filename() |
paul@0 | 58 | |
paul@0 | 59 | if not exists(details_filename): |
paul@0 | 60 | return None |
paul@0 | 61 | else: |
paul@0 | 62 | return readfile(details_filename) |
paul@0 | 63 | |
paul@0 | 64 | def remove_output(self, dirname=None): |
paul@0 | 65 | |
paul@0 | 66 | "Remove the output." |
paul@0 | 67 | |
paul@0 | 68 | dirname = dirname or self.output |
paul@0 | 69 | |
paul@0 | 70 | for filename in listdir(dirname): |
paul@0 | 71 | path = join(dirname, filename) |
paul@0 | 72 | if isdir(path): |
paul@0 | 73 | self.remove_output(path) |
paul@0 | 74 | else: |
paul@0 | 75 | remove(path) |
paul@0 | 76 | |
paul@0 | 77 | class CommonModule: |
paul@0 | 78 | |
paul@0 | 79 | "A common module representation." |
paul@0 | 80 | |
paul@0 | 81 | def __init__(self, name, importer): |
paul@0 | 82 | |
paul@0 | 83 | """ |
paul@0 | 84 | Initialise this module with the given 'name' and an 'importer' which is |
paul@0 | 85 | used to provide access to other modules when required. |
paul@0 | 86 | """ |
paul@0 | 87 | |
paul@0 | 88 | self.name = name |
paul@0 | 89 | self.importer = importer |
paul@0 | 90 | self.filename = None |
paul@0 | 91 | |
paul@0 | 92 | # Inspection-related attributes. |
paul@0 | 93 | |
paul@0 | 94 | self.astnode = None |
paul@0 | 95 | self.iterators = {} |
paul@0 | 96 | self.temp = {} |
paul@0 | 97 | self.lambdas = {} |
paul@0 | 98 | |
paul@0 | 99 | # Constants, literals and values. |
paul@0 | 100 | |
paul@0 | 101 | self.constants = {} |
paul@0 | 102 | self.constant_values = {} |
paul@0 | 103 | self.literals = {} |
paul@0 | 104 | self.literal_types = {} |
paul@0 | 105 | |
paul@0 | 106 | # Nested namespaces. |
paul@0 | 107 | |
paul@0 | 108 | self.namespace_path = [] |
paul@0 | 109 | self.in_function = False |
paul@0 | 110 | |
paul@0 | 111 | # Attribute chains. |
paul@0 | 112 | |
paul@0 | 113 | self.attrs = [] |
paul@0 | 114 | |
paul@0 | 115 | def __repr__(self): |
paul@0 | 116 | return "CommonModule(%r, %r)" % (self.name, self.importer) |
paul@0 | 117 | |
paul@0 | 118 | def parse_file(self, filename): |
paul@0 | 119 | |
paul@0 | 120 | "Parse the file with the given 'filename', initialising attributes." |
paul@0 | 121 | |
paul@0 | 122 | self.filename = filename |
paul@0 | 123 | self.astnode = compiler.parseFile(filename) |
paul@0 | 124 | |
paul@0 | 125 | # Module-relative naming. |
paul@0 | 126 | |
paul@0 | 127 | def get_global_path(self, name): |
paul@0 | 128 | return "%s.%s" % (self.name, name) |
paul@0 | 129 | |
paul@0 | 130 | def get_namespace_path(self): |
paul@0 | 131 | return ".".join([self.name] + self.namespace_path) |
paul@0 | 132 | |
paul@0 | 133 | def get_object_path(self, name): |
paul@0 | 134 | return ".".join([self.name] + self.namespace_path + [name]) |
paul@0 | 135 | |
paul@0 | 136 | def get_parent_path(self): |
paul@0 | 137 | return ".".join([self.name] + self.namespace_path[:-1]) |
paul@0 | 138 | |
paul@0 | 139 | # Namespace management. |
paul@0 | 140 | |
paul@0 | 141 | def enter_namespace(self, name): |
paul@0 | 142 | |
paul@0 | 143 | "Enter the namespace having the given 'name'." |
paul@0 | 144 | |
paul@0 | 145 | self.namespace_path.append(name) |
paul@0 | 146 | |
paul@0 | 147 | def exit_namespace(self): |
paul@0 | 148 | |
paul@0 | 149 | "Exit the current namespace." |
paul@0 | 150 | |
paul@0 | 151 | self.namespace_path.pop() |
paul@0 | 152 | |
paul@0 | 153 | # Constant reference naming. |
paul@0 | 154 | |
paul@0 | 155 | def get_constant_name(self, value): |
paul@0 | 156 | |
paul@0 | 157 | "Add a new constant to the current namespace for 'value'." |
paul@0 | 158 | |
paul@0 | 159 | path = self.get_namespace_path() |
paul@0 | 160 | init_item(self.constants, path, dict) |
paul@0 | 161 | return "$c%d" % add_counter_item(self.constants[path], value) |
paul@0 | 162 | |
paul@0 | 163 | # Literal reference naming. |
paul@0 | 164 | |
paul@0 | 165 | def get_literal_name(self): |
paul@0 | 166 | |
paul@0 | 167 | "Add a new literal to the current namespace." |
paul@0 | 168 | |
paul@0 | 169 | path = self.get_namespace_path() |
paul@0 | 170 | init_item(self.literals, path, lambda: 0) |
paul@0 | 171 | return "$C%d" % self.literals[path] |
paul@0 | 172 | |
paul@0 | 173 | def next_literal(self): |
paul@0 | 174 | self.literals[self.get_namespace_path()] += 1 |
paul@0 | 175 | |
paul@0 | 176 | # Temporary iterator naming. |
paul@0 | 177 | |
paul@0 | 178 | def get_iterator_path(self): |
paul@0 | 179 | return self.in_function and self.get_namespace_path() or self.name |
paul@0 | 180 | |
paul@0 | 181 | def get_iterator_name(self): |
paul@0 | 182 | path = self.get_iterator_path() |
paul@0 | 183 | init_item(self.iterators, path, lambda: 0) |
paul@0 | 184 | return "$i%d" % self.iterators[path] |
paul@0 | 185 | |
paul@0 | 186 | def next_iterator(self): |
paul@0 | 187 | self.iterators[self.get_iterator_path()] += 1 |
paul@0 | 188 | |
paul@0 | 189 | # Temporary variable naming. |
paul@0 | 190 | |
paul@0 | 191 | def get_temporary_name(self): |
paul@0 | 192 | path = self.get_namespace_path() |
paul@0 | 193 | init_item(self.temp, path, lambda: 0) |
paul@0 | 194 | return "$t%d" % self.temp[path] |
paul@0 | 195 | |
paul@0 | 196 | def next_temporary(self): |
paul@0 | 197 | self.temp[self.get_namespace_path()] += 1 |
paul@0 | 198 | |
paul@0 | 199 | # Arbitrary function naming. |
paul@0 | 200 | |
paul@0 | 201 | def get_lambda_name(self): |
paul@0 | 202 | path = self.get_namespace_path() |
paul@0 | 203 | init_item(self.lambdas, path, lambda: 0) |
paul@0 | 204 | name = "$l%d" % self.lambdas[path] |
paul@0 | 205 | self.lambdas[path] += 1 |
paul@0 | 206 | return name |
paul@0 | 207 | |
paul@0 | 208 | def reset_lambdas(self): |
paul@0 | 209 | self.lambdas = {} |
paul@0 | 210 | |
paul@0 | 211 | # Constant and literal recording. |
paul@0 | 212 | |
paul@0 | 213 | def get_constant_reference(self, ref, value): |
paul@0 | 214 | |
paul@0 | 215 | "Return a constant reference for the given 'ref' type and 'value'." |
paul@0 | 216 | |
paul@0 | 217 | constant_name = self.get_constant_name(value) |
paul@0 | 218 | |
paul@0 | 219 | # Return a reference for the constant. |
paul@0 | 220 | |
paul@0 | 221 | objpath = self.get_object_path(constant_name) |
paul@0 | 222 | name_ref = ConstantValueRef(constant_name, ref.instance_of(), value) |
paul@0 | 223 | |
paul@0 | 224 | # Record the value and type for the constant. |
paul@0 | 225 | |
paul@0 | 226 | self.constant_values[objpath] = name_ref.value, name_ref.get_origin() |
paul@0 | 227 | return name_ref |
paul@0 | 228 | |
paul@0 | 229 | def get_literal_reference(self, name, ref, items, cls): |
paul@0 | 230 | |
paul@0 | 231 | # Construct an invocation using the items as arguments. |
paul@0 | 232 | |
paul@0 | 233 | typename = "$L%s" % name |
paul@0 | 234 | |
paul@0 | 235 | invocation = compiler.ast.CallFunc( |
paul@0 | 236 | compiler.ast.Name(typename), |
paul@0 | 237 | items |
paul@0 | 238 | ) |
paul@0 | 239 | |
paul@0 | 240 | # Get a name for the actual literal. |
paul@0 | 241 | |
paul@0 | 242 | instname = self.get_literal_name() |
paul@0 | 243 | self.next_literal() |
paul@0 | 244 | |
paul@0 | 245 | # Record the type for the literal. |
paul@0 | 246 | |
paul@0 | 247 | objpath = self.get_object_path(instname) |
paul@0 | 248 | self.literal_types[objpath] = ref.get_origin() |
paul@0 | 249 | |
paul@0 | 250 | # Return a wrapper for the invocation exposing the items. |
paul@0 | 251 | |
paul@0 | 252 | return cls( |
paul@0 | 253 | instname, |
paul@0 | 254 | ref.instance_of(), |
paul@0 | 255 | self.process_structure_node(invocation), |
paul@0 | 256 | invocation.args |
paul@0 | 257 | ) |
paul@0 | 258 | |
paul@0 | 259 | # Node handling. |
paul@0 | 260 | |
paul@0 | 261 | def process_structure(self, node): |
paul@0 | 262 | |
paul@0 | 263 | """ |
paul@0 | 264 | Within the given 'node', process the program structure. |
paul@0 | 265 | |
paul@0 | 266 | During inspection, this will process global declarations, adjusting the |
paul@0 | 267 | module namespace, and import statements, building a module dependency |
paul@0 | 268 | hierarchy. |
paul@0 | 269 | |
paul@0 | 270 | During translation, this will consult deduced program information and |
paul@0 | 271 | output translated code. |
paul@0 | 272 | """ |
paul@0 | 273 | |
paul@0 | 274 | l = [] |
paul@0 | 275 | for n in node.getChildNodes(): |
paul@0 | 276 | l.append(self.process_structure_node(n)) |
paul@0 | 277 | return l |
paul@0 | 278 | |
paul@0 | 279 | def process_augassign_node(self, n): |
paul@0 | 280 | |
paul@0 | 281 | "Process the given augmented assignment node 'n'." |
paul@0 | 282 | |
paul@0 | 283 | op = operator_functions[n.op] |
paul@0 | 284 | |
paul@0 | 285 | if isinstance(n.node, compiler.ast.Getattr): |
paul@0 | 286 | target = compiler.ast.AssAttr(n.node.expr, n.node.attrname, "OP_ASSIGN") |
paul@0 | 287 | elif isinstance(n.node, compiler.ast.Name): |
paul@0 | 288 | target = compiler.ast.AssName(n.node.name, "OP_ASSIGN") |
paul@0 | 289 | else: |
paul@0 | 290 | target = n.node |
paul@0 | 291 | |
paul@0 | 292 | assignment = compiler.ast.Assign( |
paul@0 | 293 | [target], |
paul@0 | 294 | compiler.ast.CallFunc( |
paul@0 | 295 | compiler.ast.Name("$op%s" % op), |
paul@0 | 296 | [n.node, n.expr])) |
paul@0 | 297 | |
paul@0 | 298 | return self.process_structure_node(assignment) |
paul@0 | 299 | |
paul@0 | 300 | def process_assignment_for_function(self, original_name, name): |
paul@0 | 301 | |
paul@0 | 302 | """ |
paul@0 | 303 | Return an assignment operation making 'original_name' refer to the given |
paul@0 | 304 | 'name'. |
paul@0 | 305 | """ |
paul@0 | 306 | |
paul@0 | 307 | assignment = compiler.ast.Assign( |
paul@0 | 308 | [compiler.ast.AssName(original_name, "OP_ASSIGN")], |
paul@0 | 309 | compiler.ast.Name(name) |
paul@0 | 310 | ) |
paul@0 | 311 | |
paul@0 | 312 | return self.process_structure_node(assignment) |
paul@0 | 313 | |
paul@0 | 314 | def process_assignment_node_items(self, n, expr): |
paul@0 | 315 | |
paul@0 | 316 | """ |
paul@0 | 317 | Process the given assignment node 'n' whose children are to be assigned |
paul@0 | 318 | items of 'expr'. |
paul@0 | 319 | """ |
paul@0 | 320 | |
paul@0 | 321 | name_ref = self.process_structure_node(expr) |
paul@0 | 322 | |
paul@0 | 323 | # Either unpack the items and present them directly to each assignment |
paul@0 | 324 | # node. |
paul@0 | 325 | |
paul@0 | 326 | if isinstance(name_ref, LiteralSequenceRef): |
paul@0 | 327 | self.process_literal_sequence_items(n, name_ref) |
paul@0 | 328 | |
paul@0 | 329 | # Or have the assignment nodes access each item via the sequence API. |
paul@0 | 330 | |
paul@0 | 331 | else: |
paul@0 | 332 | self.process_assignment_node_items_by_position(n, expr, name_ref) |
paul@0 | 333 | |
paul@0 | 334 | def process_assignment_node_items_by_position(self, n, expr, name_ref): |
paul@0 | 335 | |
paul@0 | 336 | """ |
paul@0 | 337 | Process the given sequence assignment node 'n', converting the node to |
paul@0 | 338 | the separate assignment of each target using positional access on a |
paul@0 | 339 | temporary variable representing the sequence. Use 'expr' as the assigned |
paul@0 | 340 | value and 'name_ref' as the reference providing any existing temporary |
paul@0 | 341 | variable. |
paul@0 | 342 | """ |
paul@0 | 343 | |
paul@0 | 344 | assignments = [] |
paul@0 | 345 | |
paul@0 | 346 | if isinstance(name_ref, NameRef): |
paul@0 | 347 | temp = name_ref.name |
paul@0 | 348 | else: |
paul@0 | 349 | temp = self.get_temporary_name() |
paul@0 | 350 | self.next_temporary() |
paul@0 | 351 | |
paul@0 | 352 | assignments.append( |
paul@0 | 353 | compiler.ast.Assign([compiler.ast.AssName(temp, "OP_ASSIGN")], expr) |
paul@0 | 354 | ) |
paul@0 | 355 | |
paul@0 | 356 | for i, node in enumerate(n.nodes): |
paul@0 | 357 | assignments.append( |
paul@0 | 358 | compiler.ast.Assign([node], compiler.ast.Subscript( |
paul@0 | 359 | compiler.ast.Name(temp), "OP_APPLY", [compiler.ast.Const(i)])) |
paul@0 | 360 | ) |
paul@0 | 361 | |
paul@0 | 362 | return self.process_structure_node(compiler.ast.Stmt(assignments)) |
paul@0 | 363 | |
paul@0 | 364 | def process_literal_sequence_items(self, n, name_ref): |
paul@0 | 365 | |
paul@0 | 366 | """ |
paul@0 | 367 | Process the given assignment node 'n', obtaining from the given |
paul@0 | 368 | 'name_ref' the items to be assigned to the assignment targets. |
paul@0 | 369 | """ |
paul@0 | 370 | |
paul@0 | 371 | if len(n.nodes) == len(name_ref.items): |
paul@0 | 372 | for node, item in zip(n.nodes, name_ref.items): |
paul@0 | 373 | self.process_assignment_node(node, item) |
paul@0 | 374 | else: |
paul@0 | 375 | raise InspectError("In %s, item assignment needing %d items is given %d items." % ( |
paul@0 | 376 | self.get_namespace_path(), len(n.nodes), len(name_ref.items))) |
paul@0 | 377 | |
paul@0 | 378 | def process_compare_node(self, n): |
paul@0 | 379 | |
paul@0 | 380 | """ |
paul@0 | 381 | Process the given comparison node 'n', converting an operator sequence |
paul@0 | 382 | from... |
paul@0 | 383 | |
paul@0 | 384 | <expr1> <op1> <expr2> <op2> <expr3> |
paul@0 | 385 | |
paul@0 | 386 | ...to... |
paul@0 | 387 | |
paul@0 | 388 | <op1>(<expr1>, <expr2>) and <op2>(<expr2>, <expr3>) |
paul@0 | 389 | """ |
paul@0 | 390 | |
paul@0 | 391 | invocations = [] |
paul@0 | 392 | last = n.expr |
paul@0 | 393 | |
paul@0 | 394 | for op, op_node in n.ops: |
paul@0 | 395 | op = operator_functions.get(op) |
paul@0 | 396 | |
paul@0 | 397 | invocations.append(compiler.ast.CallFunc( |
paul@0 | 398 | compiler.ast.Name("$op%s" % op), |
paul@0 | 399 | [last, op_node])) |
paul@0 | 400 | |
paul@0 | 401 | last = op_node |
paul@0 | 402 | |
paul@0 | 403 | if len(invocations) > 1: |
paul@0 | 404 | result = compiler.ast.And(invocations) |
paul@0 | 405 | else: |
paul@0 | 406 | result = invocations[0] |
paul@0 | 407 | |
paul@0 | 408 | return self.process_structure_node(result) |
paul@0 | 409 | |
paul@0 | 410 | def process_dict_node(self, node): |
paul@0 | 411 | |
paul@0 | 412 | """ |
paul@0 | 413 | Process the given dictionary 'node', returning a list of (key, value) |
paul@0 | 414 | tuples. |
paul@0 | 415 | """ |
paul@0 | 416 | |
paul@0 | 417 | l = [] |
paul@0 | 418 | for key, value in node.items: |
paul@0 | 419 | l.append(( |
paul@0 | 420 | self.process_structure_node(key), |
paul@0 | 421 | self.process_structure_node(value))) |
paul@0 | 422 | return l |
paul@0 | 423 | |
paul@0 | 424 | def process_for_node(self, n): |
paul@0 | 425 | |
paul@0 | 426 | """ |
paul@0 | 427 | Generate attribute accesses for {n.list}.__iter__ and the next method on |
paul@0 | 428 | the iterator, producing a replacement node for the original. |
paul@0 | 429 | """ |
paul@0 | 430 | |
paul@0 | 431 | node = compiler.ast.Stmt([ |
paul@0 | 432 | |
paul@0 | 433 | # <iterator> = {n.list}.__iter__ |
paul@0 | 434 | |
paul@0 | 435 | compiler.ast.Assign( |
paul@0 | 436 | [compiler.ast.AssName(self.get_iterator_name(), "OP_ASSIGN")], |
paul@0 | 437 | compiler.ast.CallFunc( |
paul@0 | 438 | compiler.ast.Getattr(n.list, "__iter__"), |
paul@0 | 439 | [] |
paul@0 | 440 | )), |
paul@0 | 441 | |
paul@0 | 442 | # try: |
paul@0 | 443 | # while True: |
paul@0 | 444 | # <var>... = <iterator>.next() |
paul@0 | 445 | # ... |
paul@0 | 446 | # except StopIteration: |
paul@0 | 447 | # pass |
paul@0 | 448 | |
paul@0 | 449 | compiler.ast.TryExcept( |
paul@0 | 450 | compiler.ast.While( |
paul@0 | 451 | compiler.ast.Name("True"), |
paul@0 | 452 | compiler.ast.Stmt([ |
paul@0 | 453 | compiler.ast.Assign( |
paul@0 | 454 | [n.assign], |
paul@0 | 455 | compiler.ast.CallFunc( |
paul@0 | 456 | compiler.ast.Getattr(compiler.ast.Name(self.get_iterator_name()), "next"), |
paul@0 | 457 | [] |
paul@0 | 458 | )), |
paul@0 | 459 | n.body]), |
paul@0 | 460 | None), |
paul@0 | 461 | [(compiler.ast.Name("StopIteration"), None, compiler.ast.Stmt([compiler.ast.Pass()]))], |
paul@0 | 462 | None) |
paul@0 | 463 | ]) |
paul@0 | 464 | |
paul@0 | 465 | self.next_iterator() |
paul@0 | 466 | self.process_structure_node(node) |
paul@0 | 467 | |
paul@0 | 468 | def convert_ifexp_node(self, n): |
paul@0 | 469 | |
paul@0 | 470 | """ |
paul@0 | 471 | Convert the given if expression node 'n'. An if expression is considered |
paul@0 | 472 | as mapping to a function body containing an if statement as follows: |
paul@0 | 473 | |
paul@0 | 474 | <expr> if <test> else <altexpr> |
paul@0 | 475 | |
paul@0 | 476 | lambda <argnames>: |
paul@0 | 477 | if <test>: |
paul@0 | 478 | return <expr> |
paul@0 | 479 | else: |
paul@0 | 480 | return <altexpr> |
paul@0 | 481 | |
paul@0 | 482 | The <argnames> are populated with defaults after the node has been |
paul@0 | 483 | processed. |
paul@0 | 484 | """ |
paul@0 | 485 | |
paul@0 | 486 | node = compiler.ast.Lambda( |
paul@0 | 487 | [], [], 0, |
paul@0 | 488 | compiler.ast.If([ |
paul@0 | 489 | (n.test, compiler.ast.Return(n.then)) |
paul@0 | 490 | ], |
paul@0 | 491 | compiler.ast.Return(n.else_) |
paul@0 | 492 | )) |
paul@0 | 493 | |
paul@0 | 494 | return node |
paul@0 | 495 | |
paul@0 | 496 | def convert_listcomp_node(self, n): |
paul@0 | 497 | |
paul@0 | 498 | """ |
paul@0 | 499 | Convert the given list comprehension node 'n'. A list comprehension is |
paul@0 | 500 | considered as mapping to a function body containing a for loop as |
paul@0 | 501 | follows: |
paul@0 | 502 | |
paul@0 | 503 | [<expr> for <varexpr1> in <list1> if <ifexpr1> for <varexpr2> in <list2> if <ifexpr2> if <ifexpr3>] |
paul@0 | 504 | |
paul@0 | 505 | lambda <argnames>: |
paul@0 | 506 | <result> = [] |
paul@0 | 507 | for <varexpr1> in <list1>: |
paul@0 | 508 | if <ifexpr1>: |
paul@0 | 509 | for <varexpr2> in <list2>: |
paul@0 | 510 | if <ifexpr2>: |
paul@0 | 511 | if <ifexpr3>: |
paul@0 | 512 | <result>.append(<expr>) |
paul@0 | 513 | return <result> |
paul@0 | 514 | |
paul@0 | 515 | The <argnames> are populated with defaults after the node has been |
paul@0 | 516 | processed. |
paul@0 | 517 | """ |
paul@0 | 518 | |
paul@0 | 519 | temp = "$tr" |
paul@0 | 520 | |
paul@0 | 521 | node = compiler.ast.Lambda( |
paul@0 | 522 | [], [], 0, |
paul@0 | 523 | compiler.ast.Stmt([ |
paul@0 | 524 | |
paul@0 | 525 | # <result> = [] |
paul@0 | 526 | |
paul@0 | 527 | compiler.ast.Assign([compiler.ast.AssName(temp, "OP_ASSIGN")], |
paul@0 | 528 | compiler.ast.List([]) |
paul@0 | 529 | ), |
paul@0 | 530 | |
paul@0 | 531 | # for ... |
paul@0 | 532 | |
paul@0 | 533 | self.convert_listcomp_for_node(n.quals[0], n.quals[1:], n.expr, temp), |
paul@0 | 534 | |
paul@0 | 535 | # return <result> |
paul@0 | 536 | |
paul@0 | 537 | compiler.ast.Return(compiler.ast.Name(temp)) |
paul@0 | 538 | ])) |
paul@0 | 539 | |
paul@0 | 540 | return node |
paul@0 | 541 | |
paul@0 | 542 | def convert_listcomp_for_node(self, loop, following_loops, expr, temp): |
paul@0 | 543 | |
paul@0 | 544 | """ |
paul@0 | 545 | Return a node representing 'loop', encapsulating 'following_loops' and |
paul@0 | 546 | employing 'expr' in the innermost loop body appending to 'temp'. |
paul@0 | 547 | """ |
paul@0 | 548 | |
paul@0 | 549 | if loop.ifs: |
paul@0 | 550 | body = self.convert_listcomp_if_node(loop.ifs[0], loop.ifs[1:], following_loops, expr, temp) |
paul@0 | 551 | elif following_loops: |
paul@0 | 552 | body = self.convert_listcomp_for_node(following_loops[0], following_loops[1:], expr, temp) |
paul@0 | 553 | else: |
paul@0 | 554 | body = self.convert_listcomp_body_node(expr, temp) |
paul@0 | 555 | |
paul@0 | 556 | return compiler.ast.For(loop.assign, loop.list, compiler.ast.Stmt([body]), None) |
paul@0 | 557 | |
paul@0 | 558 | def convert_listcomp_if_node(self, if_, following_ifs, following_loops, expr, temp): |
paul@0 | 559 | |
paul@0 | 560 | """ |
paul@0 | 561 | Return a node representing 'if_', encapsulating the 'following_ifs' and |
paul@0 | 562 | 'following_loops' and employing 'expr' in the innermost loop body |
paul@0 | 563 | appending to 'temp'. |
paul@0 | 564 | """ |
paul@0 | 565 | |
paul@0 | 566 | if following_ifs: |
paul@0 | 567 | body = self.convert_listcomp_if_node(following_ifs[0], following_ifs[1:], following_loops, expr, temp) |
paul@0 | 568 | elif following_loops: |
paul@0 | 569 | body = self.convert_listcomp_for_node(following_loops[0], following_loops[1:], expr, temp) |
paul@0 | 570 | else: |
paul@0 | 571 | body = self.convert_listcomp_body_node(expr, temp) |
paul@0 | 572 | |
paul@0 | 573 | return compiler.ast.If([(if_.test, compiler.ast.Stmt([body]))], None) |
paul@0 | 574 | |
paul@0 | 575 | def convert_listcomp_body_node(self, expr, temp): |
paul@0 | 576 | |
paul@0 | 577 | "Return a node appending 'expr' to 'temp'." |
paul@0 | 578 | |
paul@0 | 579 | return compiler.ast.Discard( |
paul@0 | 580 | compiler.ast.CallFunc( |
paul@0 | 581 | compiler.ast.Getattr(compiler.ast.Name(temp), "append"), [expr])) |
paul@0 | 582 | |
paul@0 | 583 | def process_literal_sequence_node(self, n, name, ref, cls): |
paul@0 | 584 | |
paul@0 | 585 | """ |
paul@0 | 586 | Process the given literal sequence node 'n' as a function invocation, |
paul@0 | 587 | with 'name' indicating the type of the sequence, and 'ref' being a |
paul@0 | 588 | reference to the type. The 'cls' is used to instantiate a suitable name |
paul@0 | 589 | reference. |
paul@0 | 590 | """ |
paul@0 | 591 | |
paul@0 | 592 | if name == "dict": |
paul@0 | 593 | items = [] |
paul@0 | 594 | for key, value in n.items: |
paul@0 | 595 | items.append(compiler.ast.Tuple([key, value])) |
paul@0 | 596 | else: # name in ("list", "tuple"): |
paul@0 | 597 | items = n.nodes |
paul@0 | 598 | |
paul@0 | 599 | return self.get_literal_reference(name, ref, items, cls) |
paul@0 | 600 | |
paul@0 | 601 | def process_operator_node(self, n): |
paul@0 | 602 | |
paul@0 | 603 | """ |
paul@0 | 604 | Process the given operator node 'n' as an operator function invocation. |
paul@0 | 605 | """ |
paul@0 | 606 | |
paul@0 | 607 | op = operator_functions[n.__class__.__name__] |
paul@0 | 608 | invocation = compiler.ast.CallFunc( |
paul@0 | 609 | compiler.ast.Name("$op%s" % op), |
paul@0 | 610 | list(n.getChildNodes()) |
paul@0 | 611 | ) |
paul@0 | 612 | return self.process_structure_node(invocation) |
paul@0 | 613 | |
paul@0 | 614 | def process_slice_node(self, n, expr=None): |
paul@0 | 615 | |
paul@0 | 616 | """ |
paul@0 | 617 | Process the given slice node 'n' as an operator function invocation. |
paul@0 | 618 | """ |
paul@0 | 619 | |
paul@0 | 620 | op = n.flags == "OP_ASSIGN" and "setslice" or "getslice" |
paul@0 | 621 | invocation = compiler.ast.CallFunc( |
paul@0 | 622 | compiler.ast.Name("$op%s" % op), |
paul@0 | 623 | [n.expr, n.lower or compiler.ast.Name("None"), n.upper or compiler.ast.Name("None")] + |
paul@0 | 624 | (expr and [expr] or []) |
paul@0 | 625 | ) |
paul@0 | 626 | return self.process_structure_node(invocation) |
paul@0 | 627 | |
paul@0 | 628 | def process_sliceobj_node(self, n): |
paul@0 | 629 | |
paul@0 | 630 | """ |
paul@0 | 631 | Process the given slice object node 'n' as a slice constructor. |
paul@0 | 632 | """ |
paul@0 | 633 | |
paul@0 | 634 | op = "slice" |
paul@0 | 635 | invocation = compiler.ast.CallFunc( |
paul@0 | 636 | compiler.ast.Name("$op%s" % op), |
paul@0 | 637 | n.nodes |
paul@0 | 638 | ) |
paul@0 | 639 | return self.process_structure_node(invocation) |
paul@0 | 640 | |
paul@0 | 641 | def process_subscript_node(self, n, expr=None): |
paul@0 | 642 | |
paul@0 | 643 | """ |
paul@0 | 644 | Process the given subscript node 'n' as an operator function invocation. |
paul@0 | 645 | """ |
paul@0 | 646 | |
paul@0 | 647 | op = n.flags == "OP_ASSIGN" and "setitem" or "getitem" |
paul@0 | 648 | invocation = compiler.ast.CallFunc( |
paul@0 | 649 | compiler.ast.Name("$op%s" % op), |
paul@0 | 650 | [n.expr] + list(n.subs) + (expr and [expr] or []) |
paul@0 | 651 | ) |
paul@0 | 652 | return self.process_structure_node(invocation) |
paul@0 | 653 | |
paul@0 | 654 | def process_attribute_chain(self, n): |
paul@0 | 655 | |
paul@0 | 656 | """ |
paul@0 | 657 | Process the given attribute access node 'n'. Return a reference |
paul@0 | 658 | describing the expression. |
paul@0 | 659 | """ |
paul@0 | 660 | |
paul@0 | 661 | # AssAttr/Getattr are nested with the outermost access being the last |
paul@0 | 662 | # access in any chain. |
paul@0 | 663 | |
paul@0 | 664 | self.attrs.insert(0, n.attrname) |
paul@0 | 665 | attrs = self.attrs |
paul@0 | 666 | |
paul@0 | 667 | # Break attribute chains where non-access nodes are found. |
paul@0 | 668 | |
paul@0 | 669 | if not self.have_access_expression(n): |
paul@0 | 670 | self.attrs = [] |
paul@0 | 671 | |
paul@0 | 672 | # Descend into the expression, extending backwards any existing chain, |
paul@0 | 673 | # or building another for the expression. |
paul@0 | 674 | |
paul@0 | 675 | name_ref = self.process_structure_node(n.expr) |
paul@0 | 676 | |
paul@0 | 677 | # Restore chain information applying to this node. |
paul@0 | 678 | |
paul@0 | 679 | self.attrs = attrs |
paul@0 | 680 | |
paul@0 | 681 | # Return immediately if the expression was another access and thus a |
paul@0 | 682 | # continuation backwards along the chain. The above processing will |
paul@0 | 683 | # have followed the chain all the way to its conclusion. |
paul@0 | 684 | |
paul@0 | 685 | if self.have_access_expression(n): |
paul@0 | 686 | del self.attrs[0] |
paul@0 | 687 | |
paul@0 | 688 | return name_ref |
paul@0 | 689 | |
paul@0 | 690 | def have_access_expression(self, node): |
paul@0 | 691 | |
paul@0 | 692 | "Return whether the expression associated with 'node' is Getattr." |
paul@0 | 693 | |
paul@0 | 694 | return isinstance(node.expr, compiler.ast.Getattr) |
paul@0 | 695 | |
paul@0 | 696 | def get_name_for_tracking(self, name, path=None): |
paul@0 | 697 | |
paul@0 | 698 | """ |
paul@0 | 699 | Return the name to be used for attribute usage observations involving |
paul@0 | 700 | the given 'name' in the current namespace. If 'path' is indicated and |
paul@0 | 701 | the name is being used outside a function, return the path value; |
paul@0 | 702 | otherwise, return a path computed using the current namespace and the |
paul@0 | 703 | given name. |
paul@0 | 704 | |
paul@0 | 705 | The intention of this method is to provide a suitably-qualified name |
paul@0 | 706 | that can be tracked across namespaces. Where globals are being |
paul@0 | 707 | referenced in class namespaces, they should be referenced using their |
paul@0 | 708 | path within the module, not using a path within each class. |
paul@0 | 709 | |
paul@0 | 710 | It may not be possible to identify a global within a function at the |
paul@0 | 711 | time of inspection (since a global may appear later in a file). |
paul@0 | 712 | Consequently, globals are identified by their local name rather than |
paul@0 | 713 | their module-qualified path. |
paul@0 | 714 | """ |
paul@0 | 715 | |
paul@0 | 716 | # For functions, use the appropriate local names. |
paul@0 | 717 | |
paul@0 | 718 | if self.in_function: |
paul@0 | 719 | return name |
paul@0 | 720 | |
paul@0 | 721 | # For static namespaces, use the given qualified name. |
paul@0 | 722 | |
paul@0 | 723 | elif path: |
paul@0 | 724 | return path |
paul@0 | 725 | |
paul@0 | 726 | # Otherwise, establish a name in the current (module) namespace. |
paul@0 | 727 | |
paul@0 | 728 | else: |
paul@0 | 729 | return self.get_object_path(name) |
paul@0 | 730 | |
paul@0 | 731 | def get_path_for_access(self): |
paul@0 | 732 | |
paul@0 | 733 | "Outside functions, register accesses at the module level." |
paul@0 | 734 | |
paul@0 | 735 | if not self.in_function: |
paul@0 | 736 | return self.name |
paul@0 | 737 | else: |
paul@0 | 738 | return self.get_namespace_path() |
paul@0 | 739 | |
paul@0 | 740 | def get_module_name(self, node): |
paul@0 | 741 | |
paul@0 | 742 | """ |
paul@0 | 743 | Using the given From 'node' in this module, calculate any relative import |
paul@0 | 744 | information, returning a tuple containing a module to import along with any |
paul@0 | 745 | names to import based on the node's name information. |
paul@0 | 746 | |
paul@0 | 747 | Where the returned module is given as None, whole module imports should |
paul@0 | 748 | be performed for the returned modules using the returned names. |
paul@0 | 749 | """ |
paul@0 | 750 | |
paul@0 | 751 | # Absolute import. |
paul@0 | 752 | |
paul@0 | 753 | if node.level == 0: |
paul@0 | 754 | return node.modname, node.names |
paul@0 | 755 | |
paul@0 | 756 | # Relative to an ancestor of this module. |
paul@0 | 757 | |
paul@0 | 758 | else: |
paul@0 | 759 | path = self.name.split(".") |
paul@0 | 760 | level = node.level |
paul@0 | 761 | |
paul@0 | 762 | # Relative imports treat package roots as submodules. |
paul@0 | 763 | |
paul@0 | 764 | if split(self.filename)[-1] == "__init__.py": |
paul@0 | 765 | level -= 1 |
paul@0 | 766 | |
paul@0 | 767 | if level > len(path): |
paul@0 | 768 | raise InspectError("Relative import %r involves too many levels up from module %r" % ( |
paul@0 | 769 | ("%s%s" % ("." * node.level, node.modname or "")), self.name)) |
paul@0 | 770 | |
paul@0 | 771 | basename = ".".join(path[:len(path)-level]) |
paul@0 | 772 | |
paul@0 | 773 | # Name imports from a module. |
paul@0 | 774 | |
paul@0 | 775 | if node.modname: |
paul@0 | 776 | return "%s.%s" % (basename, node.modname), node.names |
paul@0 | 777 | |
paul@0 | 778 | # Relative whole module imports. |
paul@0 | 779 | |
paul@0 | 780 | else: |
paul@0 | 781 | return basename, node.names |
paul@0 | 782 | |
paul@0 | 783 | def get_argnames(args): |
paul@0 | 784 | |
paul@0 | 785 | """ |
paul@0 | 786 | Return a list of all names provided by 'args'. Since tuples may be |
paul@0 | 787 | employed, the arguments are traversed depth-first. |
paul@0 | 788 | """ |
paul@0 | 789 | |
paul@0 | 790 | l = [] |
paul@0 | 791 | for arg in args: |
paul@0 | 792 | if isinstance(arg, tuple): |
paul@0 | 793 | l += get_argnames(arg) |
paul@0 | 794 | else: |
paul@0 | 795 | l.append(arg) |
paul@0 | 796 | return l |
paul@0 | 797 | |
paul@0 | 798 | # Classes representing inspection and translation observations. |
paul@0 | 799 | |
paul@0 | 800 | class Result: |
paul@0 | 801 | |
paul@0 | 802 | "An abstract expression result." |
paul@0 | 803 | |
paul@0 | 804 | def is_name(self): |
paul@0 | 805 | return False |
paul@0 | 806 | def get_origin(self): |
paul@0 | 807 | return None |
paul@0 | 808 | |
paul@0 | 809 | class NameRef(Result): |
paul@0 | 810 | |
paul@0 | 811 | "A reference to a name." |
paul@0 | 812 | |
paul@0 | 813 | def __init__(self, name, expr=None): |
paul@0 | 814 | self.name = name |
paul@0 | 815 | self.expr = expr |
paul@0 | 816 | |
paul@0 | 817 | def is_name(self): |
paul@0 | 818 | return True |
paul@0 | 819 | |
paul@0 | 820 | def reference(self): |
paul@0 | 821 | return None |
paul@0 | 822 | |
paul@0 | 823 | def final(self): |
paul@0 | 824 | return None |
paul@0 | 825 | |
paul@0 | 826 | def __repr__(self): |
paul@0 | 827 | return "NameRef(%r, %r)" % (self.name, self.expr) |
paul@0 | 828 | |
paul@0 | 829 | class LocalNameRef(NameRef): |
paul@0 | 830 | |
paul@0 | 831 | "A reference to a local name." |
paul@0 | 832 | |
paul@0 | 833 | def __init__(self, name, number): |
paul@0 | 834 | NameRef.__init__(self, name) |
paul@0 | 835 | self.number = number |
paul@0 | 836 | |
paul@0 | 837 | def __repr__(self): |
paul@0 | 838 | return "LocalNameRef(%r, %r)" % (self.name, self.number) |
paul@0 | 839 | |
paul@0 | 840 | class ResolvedNameRef(NameRef): |
paul@0 | 841 | |
paul@0 | 842 | "A resolved name-based reference." |
paul@0 | 843 | |
paul@0 | 844 | def __init__(self, name, ref, expr=None): |
paul@0 | 845 | NameRef.__init__(self, name, expr) |
paul@0 | 846 | self.ref = ref |
paul@0 | 847 | |
paul@0 | 848 | def reference(self): |
paul@0 | 849 | return self.ref |
paul@0 | 850 | |
paul@0 | 851 | def get_name(self): |
paul@0 | 852 | return self.ref and self.ref.get_name() or None |
paul@0 | 853 | |
paul@0 | 854 | def get_origin(self): |
paul@0 | 855 | return self.ref and self.ref.get_origin() or None |
paul@0 | 856 | |
paul@0 | 857 | def static(self): |
paul@0 | 858 | return self.ref and self.ref.static() or None |
paul@0 | 859 | |
paul@0 | 860 | def final(self): |
paul@0 | 861 | return self.ref and self.ref.final() or None |
paul@0 | 862 | |
paul@0 | 863 | def has_kind(self, kinds): |
paul@0 | 864 | return self.ref and self.ref.has_kind(kinds) |
paul@0 | 865 | |
paul@0 | 866 | def __repr__(self): |
paul@0 | 867 | return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr) |
paul@0 | 868 | |
paul@0 | 869 | class ConstantValueRef(ResolvedNameRef): |
paul@0 | 870 | |
paul@0 | 871 | "A constant reference representing a single literal value." |
paul@0 | 872 | |
paul@0 | 873 | def __init__(self, name, ref, value, number=None): |
paul@0 | 874 | ResolvedNameRef.__init__(self, name, ref) |
paul@0 | 875 | self.value = value |
paul@0 | 876 | self.number = number |
paul@0 | 877 | |
paul@0 | 878 | def __repr__(self): |
paul@0 | 879 | return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number) |
paul@0 | 880 | |
paul@0 | 881 | class InstanceRef(Result): |
paul@0 | 882 | |
paul@0 | 883 | "An instance reference." |
paul@0 | 884 | |
paul@0 | 885 | def __init__(self, ref): |
paul@0 | 886 | self.ref = ref |
paul@0 | 887 | |
paul@0 | 888 | def reference(self): |
paul@0 | 889 | return self.ref |
paul@0 | 890 | |
paul@0 | 891 | def __repr__(self): |
paul@0 | 892 | return "InstanceRef(%r)" % self.ref |
paul@0 | 893 | |
paul@0 | 894 | class LiteralSequenceRef(ResolvedNameRef): |
paul@0 | 895 | |
paul@0 | 896 | "A reference representing a sequence of values." |
paul@0 | 897 | |
paul@0 | 898 | def __init__(self, name, ref, node, items=None): |
paul@0 | 899 | ResolvedNameRef.__init__(self, name, ref) |
paul@0 | 900 | self.node = node |
paul@0 | 901 | self.items = items |
paul@0 | 902 | |
paul@0 | 903 | def __repr__(self): |
paul@0 | 904 | return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items) |
paul@0 | 905 | |
paul@0 | 906 | # Dictionary utilities. |
paul@0 | 907 | |
paul@0 | 908 | def init_item(d, key, fn): |
paul@0 | 909 | |
paul@0 | 910 | """ |
paul@0 | 911 | Add to 'd' an entry for 'key' using the callable 'fn' to make an initial |
paul@0 | 912 | value where no entry already exists. |
paul@0 | 913 | """ |
paul@0 | 914 | |
paul@0 | 915 | if not d.has_key(key): |
paul@0 | 916 | d[key] = fn() |
paul@0 | 917 | return d[key] |
paul@0 | 918 | |
paul@0 | 919 | def dict_for_keys(d, keys): |
paul@0 | 920 | |
paul@0 | 921 | "Return a new dictionary containing entries from 'd' for the given 'keys'." |
paul@0 | 922 | |
paul@0 | 923 | nd = {} |
paul@0 | 924 | for key in keys: |
paul@0 | 925 | if d.has_key(key): |
paul@0 | 926 | nd[key] = d[key] |
paul@0 | 927 | return nd |
paul@0 | 928 | |
paul@0 | 929 | def make_key(s): |
paul@0 | 930 | |
paul@0 | 931 | "Make sequence 's' into a tuple-based key, first sorting its contents." |
paul@0 | 932 | |
paul@0 | 933 | l = list(s) |
paul@0 | 934 | l.sort() |
paul@0 | 935 | return tuple(l) |
paul@0 | 936 | |
paul@0 | 937 | def add_counter_item(d, key): |
paul@0 | 938 | |
paul@0 | 939 | """ |
paul@0 | 940 | Make a mapping in 'd' for 'key' to the number of keys added before it, thus |
paul@0 | 941 | maintaining a mapping of keys to their order of insertion. |
paul@0 | 942 | """ |
paul@0 | 943 | |
paul@0 | 944 | if not d.has_key(key): |
paul@0 | 945 | d[key] = len(d.keys()) |
paul@0 | 946 | return d[key] |
paul@0 | 947 | |
paul@0 | 948 | def remove_items(d1, d2): |
paul@0 | 949 | |
paul@0 | 950 | "Remove from 'd1' all items from 'd2'." |
paul@0 | 951 | |
paul@0 | 952 | for key in d2.keys(): |
paul@0 | 953 | if d1.has_key(key): |
paul@0 | 954 | del d1[key] |
paul@0 | 955 | |
paul@0 | 956 | # Set utilities. |
paul@0 | 957 | |
paul@0 | 958 | def first(s): |
paul@0 | 959 | return list(s)[0] |
paul@0 | 960 | |
paul@0 | 961 | def same(s1, s2): |
paul@0 | 962 | return set(s1) == set(s2) |
paul@0 | 963 | |
paul@0 | 964 | # General input/output. |
paul@0 | 965 | |
paul@0 | 966 | def readfile(filename): |
paul@0 | 967 | |
paul@0 | 968 | "Return the contents of 'filename'." |
paul@0 | 969 | |
paul@0 | 970 | f = open(filename) |
paul@0 | 971 | try: |
paul@0 | 972 | return f.read() |
paul@0 | 973 | finally: |
paul@0 | 974 | f.close() |
paul@0 | 975 | |
paul@0 | 976 | def writefile(filename, s): |
paul@0 | 977 | |
paul@0 | 978 | "Write to 'filename' the string 's'." |
paul@0 | 979 | |
paul@0 | 980 | f = open(filename, "w") |
paul@0 | 981 | try: |
paul@0 | 982 | f.write(s) |
paul@0 | 983 | finally: |
paul@0 | 984 | f.close() |
paul@0 | 985 | |
paul@0 | 986 | # General encoding. |
paul@0 | 987 | |
paul@0 | 988 | def sorted_output(x): |
paul@0 | 989 | |
paul@0 | 990 | "Sort sequence 'x' and return a string with commas separating the values." |
paul@0 | 991 | |
paul@0 | 992 | x = map(str, x) |
paul@0 | 993 | x.sort() |
paul@0 | 994 | return ", ".join(x) |
paul@0 | 995 | |
paul@0 | 996 | # Attribute chain decoding. |
paul@0 | 997 | |
paul@0 | 998 | def get_attrnames(attrnames): |
paul@0 | 999 | if attrnames.startswith("#"): |
paul@0 | 1000 | return [attrnames] |
paul@0 | 1001 | else: |
paul@0 | 1002 | return attrnames.split(".") |
paul@0 | 1003 | |
paul@0 | 1004 | def get_attrname_from_location(location): |
paul@0 | 1005 | path, name, attrnames, access = location |
paul@0 | 1006 | return get_attrnames(attrnames)[0] |
paul@0 | 1007 | |
paul@0 | 1008 | # Useful data. |
paul@0 | 1009 | |
paul@0 | 1010 | predefined_constants = "Ellipsis", "False", "None", "NotImplemented", "True" |
paul@0 | 1011 | |
paul@0 | 1012 | operator_functions = { |
paul@0 | 1013 | |
paul@0 | 1014 | # Fundamental operations. |
paul@0 | 1015 | |
paul@0 | 1016 | "is" : "is_", |
paul@0 | 1017 | "is not" : "is_not", |
paul@0 | 1018 | |
paul@0 | 1019 | # Binary operations. |
paul@0 | 1020 | |
paul@0 | 1021 | "in" : "in_", |
paul@0 | 1022 | "not in" : "not_in", |
paul@0 | 1023 | "Add" : "add", |
paul@0 | 1024 | "Bitand" : "and_", |
paul@0 | 1025 | "Bitor" : "or_", |
paul@0 | 1026 | "Bitxor" : "xor", |
paul@0 | 1027 | "Div" : "div", |
paul@0 | 1028 | "FloorDiv" : "floordiv", |
paul@0 | 1029 | "LeftShift" : "lshift", |
paul@0 | 1030 | "Mod" : "mod", |
paul@0 | 1031 | "Mul" : "mul", |
paul@0 | 1032 | "Power" : "pow", |
paul@0 | 1033 | "RightShift" : "rshift", |
paul@0 | 1034 | "Sub" : "sub", |
paul@0 | 1035 | |
paul@0 | 1036 | # Unary operations. |
paul@0 | 1037 | |
paul@0 | 1038 | "Invert" : "invert", |
paul@0 | 1039 | "UnaryAdd" : "pos", |
paul@0 | 1040 | "UnarySub" : "neg", |
paul@0 | 1041 | |
paul@0 | 1042 | # Augmented assignment. |
paul@0 | 1043 | |
paul@0 | 1044 | "+=" : "iadd", |
paul@0 | 1045 | "-=" : "isub", |
paul@0 | 1046 | "*=" : "imul", |
paul@0 | 1047 | "/=" : "idiv", |
paul@0 | 1048 | "//=" : "ifloordiv", |
paul@0 | 1049 | "%=" : "imod", |
paul@0 | 1050 | "**=" : "ipow", |
paul@0 | 1051 | "<<=" : "ilshift", |
paul@0 | 1052 | ">>=" : "irshift", |
paul@0 | 1053 | "&=" : "iand", |
paul@0 | 1054 | "^=" : "ixor", |
paul@0 | 1055 | "|=" : "ior", |
paul@0 | 1056 | |
paul@0 | 1057 | # Comparisons. |
paul@0 | 1058 | |
paul@0 | 1059 | "==" : "eq", |
paul@0 | 1060 | "!=" : "ne", |
paul@0 | 1061 | "<" : "lt", |
paul@0 | 1062 | "<=" : "le", |
paul@0 | 1063 | ">=" : "ge", |
paul@0 | 1064 | ">" : "gt", |
paul@0 | 1065 | } |
paul@0 | 1066 | |
paul@0 | 1067 | # vim: tabstop=4 expandtab shiftwidth=4 |