Permalink
Browse files

Factor out a common portion of CodeGenerator.Start()

  • Loading branch information...
Andy Chu
Andy Chu committed Mar 20, 2018
1 parent d8b6912 commit cd1e3c4bcb6750f96e18c491d97f226fc7dd1eac
Showing with 16 additions and 13 deletions.
  1. +16 −13 opy/compiler2/pycodegen.py
View
@@ -181,9 +181,21 @@ def __init__(self, graph, futures):
elif feature == "print_function":
self.graph.setFlag(CO_FUTURE_PRINT_FUNCTION)
#
# Two methods that subclasses must implement.
#
def _Start(self):
raise NotImplementedError
def Finish(self):
raise NotImplementedError
def Start(self):
self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars())
self._Start()
def mangle(self, name):
return misc.mangle(name, self.class_name)
@@ -1306,9 +1318,7 @@ def FindLocals(self):
class FunctionCodeGenerator(_FunctionCodeGenerator):
def Start(self):
self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars())
def _Start(self):
if self.scope.generator is not None: # does it have yield in it?
self.graph.setFlag(CO_GENERATOR)
if self.func.doc:
@@ -1319,12 +1329,9 @@ def Finish(self):
self.emit('LOAD_CONST', None)
self.emit('RETURN_VALUE')
class LambdaCodeGenerator(_FunctionCodeGenerator):
def Start(self):
self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars())
def _Start(self):
if self.scope.generator is not None: # does it have yield in it?
self.graph.setFlag(CO_GENERATOR)
@@ -1335,9 +1342,7 @@ def Finish(self):
class GenExprCodeGenerator(_FunctionCodeGenerator):
def Start(self):
self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars())
def _Start(self):
self.graph.setFlag(CO_GENERATOR) # It's always a generator
def Finish(self):
@@ -1356,9 +1361,7 @@ def __init__(self, graph, futures, klass, scopes):
self.class_name = klass.name
self.scope = scopes[klass]
def Start(self):
self.graph.setFreeVars(self.scope.get_free_vars())
self.graph.setCellVars(self.scope.get_cell_vars())
def _Start(self):
self.set_lineno(self.klass)
self.emit("LOAD_GLOBAL", "__name__")
self.storeName("__module__")

0 comments on commit cd1e3c4

Please sign in to comment.