Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
lib2to3.fixes.fix_idioms.FixIdioms Class Reference
Inheritance diagram for lib2to3.fixes.fix_idioms.FixIdioms:
Inheritance graph
[legend]
Collaboration diagram for lib2to3.fixes.fix_idioms.FixIdioms:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def match
def transform
def transform_isinstance
def transform_while
def transform_sort
def compile_pattern
def set_filename
def new_name
def log_message
def cannot_convert
def warning
def start_tree
def finish_tree

Public Attributes

 numbers
 used_names
 log
 first_log
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 explicit = True
string PATTERN = r""
 pattern = None
 pattern_tree = None
 options = None
 filename = None
 logger = None
tuple numbers = itertools.count(1)
tuple used_names = set()
string order = "post"
int run_order = 5
 keep_line_order = False
 BM_compatible = False
 syms = pygram.python_symbols

Detailed Description

Definition at line 37 of file fix_idioms.py.


Member Function Documentation

def lib2to3.fixer_base.BaseFix.cannot_convert (   self,
  node,
  reason = None 
) [inherited]
Warn the user that a given chunk of code is not valid Python 3,
but that it cannot be converted automatically.

First argument is the top-level node for the code in question.
Optional second argument is why it can't be converted.

Definition at line 125 of file fixer_base.py.

00125 
00126     def cannot_convert(self, node, reason=None):
00127         """Warn the user that a given chunk of code is not valid Python 3,
00128         but that it cannot be converted automatically.
00129 
00130         First argument is the top-level node for the code in question.
00131         Optional second argument is why it can't be converted.
00132         """
00133         lineno = node.get_lineno()
00134         for_output = node.clone()
00135         for_output.prefix = ""
00136         msg = "Line %d: could not convert: %s"
00137         self.log_message(msg % (lineno, for_output))
00138         if reason:
00139             self.log_message(reason)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.compile_pattern (   self) [inherited]
Compiles self.PATTERN into self.pattern.

Subclass may override if it doesn't want to use
self.{pattern,PATTERN} in .match().

Reimplemented in lib2to3.fixes.fix_imports.FixImports.

Definition at line 61 of file fixer_base.py.

00061 
00062     def compile_pattern(self):
00063         """Compiles self.PATTERN into self.pattern.
00064 
00065         Subclass may override if it doesn't want to use
00066         self.{pattern,PATTERN} in .match().
00067         """
00068         if self.PATTERN is not None:
00069             PC = PatternCompiler()
00070             self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN,
00071                                                                  with_tree=True)

def lib2to3.fixer_base.BaseFix.finish_tree (   self,
  tree,
  filename 
) [inherited]
Some fixers need to maintain tree-wide state.
This method is called once, at the conclusion of tree fix-up.

tree - the root node of the tree to be processed.
filename - the name of the file the tree came from.

Reimplemented in lib2to3.fixes.fix_xrange.FixXrange.

Definition at line 162 of file fixer_base.py.

00162 
00163     def finish_tree(self, tree, filename):
00164         """Some fixers need to maintain tree-wide state.
00165         This method is called once, at the conclusion of tree fix-up.
00166 
00167         tree - the root node of the tree to be processed.
00168         filename - the name of the file the tree came from.
00169         """
00170         pass
00171 

def lib2to3.fixer_base.BaseFix.log_message (   self,
  message 
) [inherited]

Definition at line 119 of file fixer_base.py.

00119 
00120     def log_message(self, message):
00121         if self.first_log:
00122             self.first_log = False
00123             self.log.append("### In file %s ###" % self.filename)
00124         self.log.append(message)

Here is the caller graph for this function:

def lib2to3.fixes.fix_idioms.FixIdioms.match (   self,
  node 
)
Returns match for a given parse tree node.

Should return a true or false object (not necessarily a bool).
It may return a non-empty dict of matching sub-nodes as
returned by a matching pattern.

Subclass may override.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 79 of file fix_idioms.py.

00079 
00080     def match(self, node):
00081         r = super(FixIdioms, self).match(node)
00082         # If we've matched one of the sort/sorted subpatterns above, we
00083         # want to reject matches where the initial assignment and the
00084         # subsequent .sort() call involve different identifiers.
00085         if r and "sorted" in r:
00086             if r["id1"] == r["id2"]:
00087                 return r
00088             return None
00089         return r

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.new_name (   self,
  template = "xxx_todo_changeme" 
) [inherited]
Return a string suitable for use as an identifier

The new name is guaranteed not to conflict with other identifiers.

Definition at line 108 of file fixer_base.py.

00108 
00109     def new_name(self, template="xxx_todo_changeme"):
00110         """Return a string suitable for use as an identifier
00111 
00112         The new name is guaranteed not to conflict with other identifiers.
00113         """
00114         name = template
00115         while name in self.used_names:
00116             name = template + str(next(self.numbers))
00117         self.used_names.add(name)
00118         return name

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.set_filename (   self,
  filename 
) [inherited]
Set the filename, and a logger derived from it.

The main refactoring tool should call this.

Definition at line 72 of file fixer_base.py.

00072 
00073     def set_filename(self, filename):
00074         """Set the filename, and a logger derived from it.
00075 
00076         The main refactoring tool should call this.
00077         """
00078         self.filename = filename
00079         self.logger = logging.getLogger(filename)

def lib2to3.fixer_base.BaseFix.start_tree (   self,
  tree,
  filename 
) [inherited]
Some fixers need to maintain tree-wide state.
This method is called once, at the start of tree fix-up.

tree - the root node of the tree to be processed.
filename - the name of the file the tree came from.

Reimplemented in lib2to3.fixes.fix_imports.FixImports, lib2to3.fixes.fix_import.FixImport, lib2to3.fixes.fix_next.FixNext, lib2to3.fixes.fix_exitfunc.FixExitfunc, and lib2to3.fixes.fix_xrange.FixXrange.

Definition at line 150 of file fixer_base.py.

00150 
00151     def start_tree(self, tree, filename):
00152         """Some fixers need to maintain tree-wide state.
00153         This method is called once, at the start of tree fix-up.
00154 
00155         tree - the root node of the tree to be processed.
00156         filename - the name of the file the tree came from.
00157         """
00158         self.used_names = tree.used_names
00159         self.set_filename(filename)
00160         self.numbers = itertools.count(1)
00161         self.first_log = True

def lib2to3.fixes.fix_idioms.FixIdioms.transform (   self,
  node,
  results 
)
Returns the transformation for a given parse tree node.

Args:
  node: the root of the parse tree that matched the fixer.
  results: a dict mapping symbolic names to part of the match.

Returns:
  None, or a node that is a modified copy of the
  argument node.  The node argument may also be modified in-place to
  effect the same change.

Subclass *must* override.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 90 of file fix_idioms.py.

00090 
00091     def transform(self, node, results):
00092         if "isinstance" in results:
00093             return self.transform_isinstance(node, results)
00094         elif "while" in results:
00095             return self.transform_while(node, results)
00096         elif "sorted" in results:
00097             return self.transform_sort(node, results)
00098         else:
00099             raise RuntimeError("Invalid match")

Here is the call graph for this function:

def lib2to3.fixes.fix_idioms.FixIdioms.transform_isinstance (   self,
  node,
  results 
)

Definition at line 100 of file fix_idioms.py.

00100 
00101     def transform_isinstance(self, node, results):
00102         x = results["x"].clone() # The thing inside of type()
00103         T = results["T"].clone() # The type being compared against
00104         x.prefix = ""
00105         T.prefix = " "
00106         test = Call(Name("isinstance"), [x, Comma(), T])
00107         if "n" in results:
00108             test.prefix = " "
00109             test = Node(syms.not_test, [Name("not"), test])
00110         test.prefix = node.prefix
00111         return test

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixes.fix_idioms.FixIdioms.transform_sort (   self,
  node,
  results 
)

Definition at line 116 of file fix_idioms.py.

00116 
00117     def transform_sort(self, node, results):
00118         sort_stmt = results["sort"]
00119         next_stmt = results["next"]
00120         list_call = results.get("list")
00121         simple_expr = results.get("expr")
00122 
00123         if list_call:
00124             list_call.replace(Name("sorted", prefix=list_call.prefix))
00125         elif simple_expr:
00126             new = simple_expr.clone()
00127             new.prefix = ""
00128             simple_expr.replace(Call(Name("sorted"), [new],
00129                                      prefix=simple_expr.prefix))
00130         else:
00131             raise RuntimeError("should not have reached here")
00132         sort_stmt.remove()
00133 
00134         btwn = sort_stmt.prefix
00135         # Keep any prefix lines between the sort_stmt and the list_call and
00136         # shove them right after the sorted() call.
00137         if "\n" in btwn:
00138             if next_stmt:
00139                 # The new prefix should be everything from the sort_stmt's
00140                 # prefix up to the last newline, then the old prefix after a new
00141                 # line.
00142                 prefix_lines = (btwn.rpartition("\n")[0], next_stmt[0].prefix)
00143                 next_stmt[0].prefix = "\n".join(prefix_lines)
00144             else:
00145                 assert list_call.parent
00146                 assert list_call.next_sibling is None
00147                 # Put a blank line after list_call and set its prefix.
00148                 end_line = BlankLine()
00149                 list_call.parent.append_child(end_line)
00150                 assert list_call.next_sibling is end_line
00151                 # The new prefix should be everything up to the first new line
00152                 # of sort_stmt's prefix.
00153                 end_line.prefix = btwn.rpartition("\n")[0]

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixes.fix_idioms.FixIdioms.transform_while (   self,
  node,
  results 
)

Definition at line 112 of file fix_idioms.py.

00112 
00113     def transform_while(self, node, results):
00114         one = results["while"]
00115         one.replace(Name("True", prefix=one.prefix))

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.warning (   self,
  node,
  reason 
) [inherited]
Used for warning the user about possible uncertainty in the
translation.

First argument is the top-level node for the code in question.
Optional second argument is why it can't be converted.

Definition at line 140 of file fixer_base.py.

00140 
00141     def warning(self, node, reason):
00142         """Used for warning the user about possible uncertainty in the
00143         translation.
00144 
00145         First argument is the top-level node for the code in question.
00146         Optional second argument is why it can't be converted.
00147         """
00148         lineno = node.get_lineno()
00149         self.log_message("Line %d: %s" % (lineno, reason))

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

lib2to3.fixer_base.BaseFix.BM_compatible = False [static, inherited]

Reimplemented in lib2to3.fixes.fix_metaclass.FixMetaclass, lib2to3.fixes.fix_imports.FixImports, lib2to3.fixes.fix_types.FixTypes, lib2to3.fixes.fix_dict.FixDict, lib2to3.fixes.fix_renames.FixRenames, lib2to3.fixes.fix_has_key.FixHasKey, lib2to3.fixes.fix_import.FixImport, lib2to3.fixes.fix_except.FixExcept, lib2to3.fixes.fix_tuple_params.FixTupleParams, lib2to3.fixes.fix_raise.FixRaise, lib2to3.fixes.fix_print.FixPrint, lib2to3.fixes.fix_map.FixMap, lib2to3.fixes.fix_operator.FixOperator, lib2to3.fixes.fix_filter.FixFilter, lib2to3.fixes.fix_exec.FixExec, lib2to3.fixes.fix_next.FixNext, lib2to3.fixes.fix_isinstance.FixIsinstance, lib2to3.fixes.fix_reduce.FixReduce, lib2to3.fixes.fix_sys_exc.FixSysExc, lib2to3.fixes.fix_throw.FixThrow, lib2to3.fixes.fix_execfile.FixExecfile, lib2to3.fixes.fix_methodattrs.FixMethodattrs, lib2to3.fixes.fix_zip.FixZip, lib2to3.fixes.fix_apply.FixApply, lib2to3.fixes.fix_intern.FixIntern, lib2to3.fixes.fix_itertools.FixItertools, lib2to3.fixes.fix_callable.FixCallable, lib2to3.fixes.fix_input.FixInput, lib2to3.fixes.fix_set_literal.FixSetLiteral, lib2to3.fixes.fix_exitfunc.FixExitfunc, lib2to3.fixes.fix_long.FixLong, lib2to3.fixes.fix_paren.FixParen, lib2to3.fixes.fix_repr.FixRepr, lib2to3.fixes.fix_unicode.FixUnicode, lib2to3.fixes.fix_xrange.FixXrange, lib2to3.fixes.fix_buffer.FixBuffer, lib2to3.fixes.fix_future.FixFuture, lib2to3.fixes.fix_standarderror.FixStandarderror, lib2to3.fixes.fix_xreadlines.FixXreadlines, lib2to3.fixes.fix_getcwdu.FixGetcwdu, lib2to3.fixes.fix_funcattrs.FixFuncattrs, lib2to3.fixes.fix_raw_input.FixRawInput, lib2to3.fixes.fix_basestring.FixBasestring, lib2to3.fixes.fix_itertools_imports.FixItertoolsImports, and lib2to3.fixes.fix_nonzero.FixNonzero.

Definition at line 42 of file fixer_base.py.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 38 of file fix_idioms.py.

lib2to3.fixer_base.BaseFix.filename = None [static, inherited]

Definition at line 29 of file fixer_base.py.

Definition at line 121 of file fixer_base.py.

lib2to3.fixer_base.BaseFix.keep_line_order = False [static, inherited]

Definition at line 58 of file fixer_base.py.

lib2to3.fixer_base.BaseFix.logger = None [static, inherited]

Definition at line 30 of file fixer_base.py.

tuple lib2to3.fixer_base.BaseFix.numbers = itertools.count(1) [static, inherited]

Definition at line 31 of file fixer_base.py.

Definition at line 159 of file fixer_base.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

lib2to3.fixer_base.BaseFix.options = None [static, inherited]

Definition at line 28 of file fixer_base.py.

string lib2to3.fixer_base.BaseFix.order = "post" [static, inherited]
lib2to3.fixer_base.BaseFix.pattern = None [static, inherited]

Definition at line 26 of file fixer_base.py.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 40 of file fix_idioms.py.

lib2to3.fixer_base.BaseFix.pattern_tree = None [static, inherited]

Definition at line 27 of file fixer_base.py.

int lib2to3.fixer_base.BaseFix.run_order = 5 [static, inherited]

Definition at line 47 of file fixer_base.py.

tuple lib2to3.fixer_base.BaseFix.used_names = set() [static, inherited]

Definition at line 32 of file fixer_base.py.

Definition at line 157 of file fixer_base.py.


The documentation for this class was generated from the following file: