Back to index

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

List of all members.

Public Member Functions

def transform
def compile_pattern
def set_filename
def match
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

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

Detailed Description

Definition at line 145 of file fix_metaclass.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.fixer_base.BaseFix.match (   self,
  node 
) [inherited]
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 in lib2to3.fixes.fix_imports.FixImports, lib2to3.fixes.fix_idioms.FixIdioms, lib2to3.fixes.fix_renames.FixRenames, lib2to3.fixes.fix_ne.FixNe, lib2to3.fixes.fix_numliterals.FixNumliterals, myfixes.fix_last.FixLast, myfixes.fix_first.FixFirst, and myfixes.fix_preorder.FixPreorder.

Definition at line 80 of file fixer_base.py.

00080 
00081     def match(self, node):
00082         """Returns match for a given parse tree node.
00083 
00084         Should return a true or false object (not necessarily a bool).
00085         It may return a non-empty dict of matching sub-nodes as
00086         returned by a matching pattern.
00087 
00088         Subclass may override.
00089         """
00090         results = {"node": node}
00091         return self.pattern.match(node, results) and results

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_metaclass.FixMetaclass.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 152 of file fix_metaclass.py.

00152 
00153     def transform(self, node, results):
00154         if not has_metaclass(node):
00155             return
00156 
00157         fixup_parse_tree(node)
00158 
00159         # find metaclasses, keep the last one
00160         last_metaclass = None
00161         for suite, i, stmt in find_metas(node):
00162             last_metaclass = stmt
00163             stmt.remove()
00164 
00165         text_type = node.children[0].type # always Leaf(nnn, 'class')
00166 
00167         # figure out what kind of classdef we have
00168         if len(node.children) == 7:
00169             # Node(classdef, ['class', 'name', '(', arglist, ')', ':', suite])
00170             #                 0        1       2    3        4    5    6
00171             if node.children[3].type == syms.arglist:
00172                 arglist = node.children[3]
00173             # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
00174             else:
00175                 parent = node.children[3].clone()
00176                 arglist = Node(syms.arglist, [parent])
00177                 node.set_child(3, arglist)
00178         elif len(node.children) == 6:
00179             # Node(classdef, ['class', 'name', '(',  ')', ':', suite])
00180             #                 0        1       2     3    4    5
00181             arglist = Node(syms.arglist, [])
00182             node.insert_child(3, arglist)
00183         elif len(node.children) == 4:
00184             # Node(classdef, ['class', 'name', ':', suite])
00185             #                 0        1       2    3
00186             arglist = Node(syms.arglist, [])
00187             node.insert_child(2, Leaf(token.RPAR, ')'))
00188             node.insert_child(2, arglist)
00189             node.insert_child(2, Leaf(token.LPAR, '('))
00190         else:
00191             raise ValueError("Unexpected class definition")
00192 
00193         # now stick the metaclass in the arglist
00194         meta_txt = last_metaclass.children[0].children[0]
00195         meta_txt.value = 'metaclass'
00196         orig_meta_prefix = meta_txt.prefix
00197 
00198         if arglist.children:
00199             arglist.append_child(Leaf(token.COMMA, ','))
00200             meta_txt.prefix = ' '
00201         else:
00202             meta_txt.prefix = ''
00203 
00204         # compact the expression "metaclass = Meta" -> "metaclass=Meta"
00205         expr_stmt = last_metaclass.children[0]
00206         assert expr_stmt.type == syms.expr_stmt
00207         expr_stmt.children[1].prefix = ''
00208         expr_stmt.children[2].prefix = ''
00209 
00210         arglist.append_child(last_metaclass)
00211 
00212         fixup_indent(suite)
00213 
00214         # check for empty suite
00215         if not suite.children:
00216             # one-liner that was just __metaclass_
00217             suite.remove()
00218             pass_leaf = Leaf(text_type, 'pass')
00219             pass_leaf.prefix = orig_meta_prefix
00220             node.append_child(pass_leaf)
00221             node.append_child(Leaf(token.NEWLINE, '\n'))
00222 
00223         elif len(suite.children) > 1 and \
00224                  (suite.children[-2].type == token.INDENT and
00225                   suite.children[-1].type == token.DEDENT):
00226             # there was only one line in the class body and it was __metaclass__
00227             pass_leaf = Leaf(text_type, 'pass')
00228             suite.insert_child(-1, pass_leaf)
00229             suite.insert_child(-1, Leaf(token.NEWLINE, '\n'))

Here is the call 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

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 146 of file fix_metaclass.py.

lib2to3.fixer_base.BaseFix.explicit = False [static, inherited]
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.

Initial value:
"""
classdef<any*>
"""

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 148 of file fix_metaclass.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: