def test_pyforaInspect_twoClassesOneName(self): class C2: def fC1(self): return 0 def f2(): class C2: def fC2(self): return 1 return C2 with self.assertRaises(PyforaInspect.PyforaInspectError) : PyforaInspect.getsourcelines(f2()().__class__)
def test_pyforaInspect_twoClassesOneName(self): class C2: def fC1(self): return 0 def f2(): class C2: def fC2(self): return 1 return C2 with self.assertRaises(PyforaInspect.PyforaInspectError): PyforaInspect.getsourcelines(f2()().__class__)
def test_pyforaInspect_twoNestedClasses(self): def f1(): class C1: def fC1(self): return 0 return C1 def f2(): class C1: def fC2(self): return 1 return C1 with self.assertRaises(PyforaInspect.PyforaInspectError) : PyforaInspect.getsourcelines(f1()().__class__)
def getSourceLines(pyObject): try: tr = PyforaInspect.getsourcelines(pyObject) except (TypeError, IOError) as e: raise Exceptions.CantGetSourceTextError( e.message), None, sys.exc_info()[2] return tr
def test_pyforaInspect_twoClassestwoNames(self): def f1(): class C3a: def fC1(self): return 0 return C3a def f2(): class C3b: def fC2(self): return 1 return C3b self.assertNotEqual( PyforaInspect.getsourcelines(f1()().__class__), PyforaInspect.getsourcelines(f2()().__class__) )
def test_pyforaInspect_twoClassestwoNames(self): def f1(): class C3a: def fC1(self): return 0 return C3a def f2(): class C3b: def fC2(self): return 1 return C3b self.assertNotEqual(PyforaInspect.getsourcelines(f1()().__class__), PyforaInspect.getsourcelines(f2()().__class__))
def test_pyforaInspect_twoNestedClasses(self): def f1(): class C1: def fC1(self): return 0 return C1 def f2(): class C1: def fC2(self): return 1 return C1 with self.assertRaises(PyforaInspect.PyforaInspectError): PyforaInspect.getsourcelines(f1()().__class__)
def test_class_line_number_collision(self): from ufora.FORA.python.PurePython.testModules.same_line_number.A import A from ufora.FORA.python.PurePython.testModules.same_line_number.A import B line_number_of_class_A_definition = PyforaInspect.getsourcelines(A)[1] line_number_of_class_B_definition = PyforaInspect.getsourcelines(B)[1] self.assertEqual(line_number_of_class_A_definition, line_number_of_class_B_definition) with self.create_executor() as executor: x = 99999 with executor.remotely.downloadAll(): a = A(99999) self.assertIsInstance(a, A) self.assertEqual(a.__dict__, A(x).__dict__)
def getSourceText(pyObject): try: source, lineno = PyforaInspect.getsourcelines(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) # Create a prefix of (lineno-1) blank lines to keep track of line numbers for error reporting blankLines = os.linesep * (lineno - 1) # We don't know how to avoid the use of `textwrap.dedent to get the code # though `ast.parse, which means that the computed column_numbers may be # off and we shouldn't report them. return textwrap.dedent(blankLines + "".join(source))
def _pyObjectNodeForClassOrFunction(self, pyObject, classOrFunction): try: sourceFileText, sourceFileName = PyAstUtil.getSourceFilenameAndText(pyObject) except Exceptions.CantGetSourceTextError as e: raise Exceptions.PythonToForaConversionError(e.message) _, sourceLine = PyforaInspect.getsourcelines(pyObject) sourceAst = PyAstUtil.getSourceFileAst(pyObject) if classOrFunction is PyObjectNodes.FunctionDefinition: pyAst = PyAstUtil.functionDefOrLambdaAtLineNumber(sourceAst, sourceLine) else: assert classOrFunction is PyObjectNodes.ClassDefinition pyAst = PyAstUtil.classDefAtLineNumber(sourceAst, sourceLine) freeVariableMemberAccessChainResolutions = \ self._resolveFreeVariableMemberAccessChains( pyObject, pyAst ) processedFreeVariableMemberAccessChainResolutions = { \ '.'.join(chain): resolution for chain, resolution in \ freeVariableMemberAccessChainResolutions.iteritems() } if sourceFileName in self._fileTextCache: fileObject = self._fileTextCache[sourceFileName] else: fileObject = PyObjectNodes.File(sourceFileName, sourceFileText) self._fileTextCache[sourceFileName] = fileObject return classOrFunction( pyObject, fileObject, sourceLine, processedFreeVariableMemberAccessChainResolutions )
def _pyObjectNodeForClassOrFunction(self, pyObject, classOrFunction): try: sourceFileText, sourceFileName = PyAstUtil.getSourceFilenameAndText( pyObject) except Exceptions.CantGetSourceTextError as e: raise Exceptions.PythonToForaConversionError(e.message) _, sourceLine = PyforaInspect.getsourcelines(pyObject) sourceAst = PyAstUtil.getSourceFileAst(pyObject) if classOrFunction is PyObjectNodes.FunctionDefinition: pyAst = PyAstUtil.functionDefOrLambdaAtLineNumber( sourceAst, sourceLine) else: assert classOrFunction is PyObjectNodes.ClassDefinition pyAst = PyAstUtil.classDefAtLineNumber(sourceAst, sourceLine) freeVariableMemberAccessChainResolutions = \ self._resolveFreeVariableMemberAccessChains( pyObject, pyAst ) processedFreeVariableMemberAccessChainResolutions = { \ '.'.join(chain): resolution for chain, resolution in \ freeVariableMemberAccessChainResolutions.iteritems() } if sourceFileName in self._fileTextCache: fileObject = self._fileTextCache[sourceFileName] else: fileObject = PyObjectNodes.File(sourceFileName, sourceFileText) self._fileTextCache[sourceFileName] = fileObject return classOrFunction( pyObject, fileObject, sourceLine, processedFreeVariableMemberAccessChainResolutions)
def getSourceLines(pyObject): try: tr = PyforaInspect.getsourcelines(pyObject) except (TypeError, IOError) as e: raise Exceptions.CantGetSourceTextError(e.message), None, sys.exc_info()[2] return tr
def _classOrFunctionDefinition(self, pyObject, classOrFunction): """ `_classOrFunctionDefinition: create a `_FunctionDefinition` or `_ClassDefinition` out of a python class or function, recursively visiting the resolvable free variable member access chains in `pyObject` as well as the source file object. Args: `pyObject`: a python class or function. `classOrFunction`: should either be `_FunctionDefinition` or `_ClassDefinition`. Returns: a `_FunctionDefinition` or `_ClassDefinition`. """ if pyObject.__name__ == '__inline_fora': raise Exceptions.PythonToForaConversionError( "in pyfora, '__inline_fora' is a reserved word") try: sourceFileText, sourceFileName = PyAstUtil.getSourceFilenameAndText( pyObject) except Exceptions.CantGetSourceTextError: self._raiseConversionErrorForSourceTextError(pyObject) except: logging.error('Failed on %s (of type %s)', pyObject, type(pyObject)) raise _, sourceLine = PyforaInspect.getsourcelines(pyObject) sourceAst = PyAstUtil.pyAstFromText(sourceFileText) if classOrFunction is _FunctionDefinition: pyAst = PyAstUtil.functionDefOrLambdaAtLineNumber( sourceAst, sourceLine) else: assert classOrFunction is _ClassDefinition pyAst = PyAstUtil.classDefAtLineNumber(sourceAst, sourceLine) try: freeVariableMemberAccessChainResolutions = \ self._computeAndResolveFreeVariableMemberAccessChainsInAst( pyObject, pyAst ) except UnresolvedFreeVariableException as e: _convertUnresolvedFreeVariableExceptionAndRaise(e, sourceFileName) try: processedFreeVariableMemberAccessChainResolutions = {} for chain, (resolution, location) in \ freeVariableMemberAccessChainResolutions.iteritems(): processedFreeVariableMemberAccessChainResolutions['.'.join(chain)] = \ self.walkPyObject(resolution) except UnresolvedFreeVariableExceptionWithTrace as e: e.addToTrace( Exceptions.makeTraceElement(path=sourceFileName, lineNumber=location[0])) raise sourceFileId = self.walkPyObject( _FileDescription.cachedFromArgs(fileName=sourceFileName, fileText=sourceFileText)) return classOrFunction( sourceFileId=sourceFileId, lineNumber=sourceLine, freeVariableMemberAccessChainsToId=\ processedFreeVariableMemberAccessChainResolutions )
def _classOrFunctionDefinition(self, pyObject, classOrFunction): """ `_classOrFunctionDefinition: create a `_FunctionDefinition` or `_ClassDefinition` out of a python class or function, recursively visiting the resolvable free variable member access chains in `pyObject` as well as the source file object. Args: `pyObject`: a python class or function. `classOrFunction`: should either be `_FunctionDefinition` or `_ClassDefinition`. Returns: a `_FunctionDefinition` or `_ClassDefinition`. """ if pyObject.__name__ == '__inline_fora': raise Exceptions.PythonToForaConversionError( "in pyfora, '__inline_fora' is a reserved word" ) try: sourceFileText, sourceFileName = PyAstUtil.getSourceFilenameAndText(pyObject) except Exceptions.CantGetSourceTextError: self._raiseConversionErrorForSourceTextError(pyObject) except: logging.error('Failed on %s (of type %s)', pyObject, type(pyObject)) raise _, sourceLine = PyforaInspect.getsourcelines(pyObject) sourceAst = PyAstUtil.pyAstFromText(sourceFileText) if classOrFunction is _FunctionDefinition: pyAst = PyAstUtil.functionDefOrLambdaAtLineNumber(sourceAst, sourceLine) else: assert classOrFunction is _ClassDefinition pyAst = PyAstUtil.classDefAtLineNumber(sourceAst, sourceLine) try: freeVariableMemberAccessChainResolutions = \ self._computeAndResolveFreeVariableMemberAccessChainsInAst( pyObject, pyAst ) except UnresolvedFreeVariableException as e: _convertUnresolvedFreeVariableExceptionAndRaise(e, sourceFileName) try: processedFreeVariableMemberAccessChainResolutions = {} for chain, (resolution, location) in \ freeVariableMemberAccessChainResolutions.iteritems(): processedFreeVariableMemberAccessChainResolutions['.'.join(chain)] = \ self.walkPyObject(resolution) except UnresolvedFreeVariableExceptionWithTrace as e: e.addToTrace( Exceptions.makeTraceElement( path=sourceFileName, lineNumber=location[0] ) ) raise sourceFileId = self.walkPyObject( _FileDescription.cachedFromArgs( fileName=sourceFileName, fileText=sourceFileText ) ) return classOrFunction( sourceFileId=sourceFileId, lineNumber=sourceLine, freeVariableMemberAccessChainsToId=\ processedFreeVariableMemberAccessChainResolutions )