Exemple #1
0
    def _subchainAndResolutionOrNone(self, pyObject, chain):
        if PyforaInspect.isfunction(pyObject):
            return self._lookupChainInFunction(pyObject, chain)

        if PyforaInspect.isclass(pyObject):
            return self._lookupChainInClass(pyObject, chain)
        
        return None
Exemple #2
0
    def _subchainAndResolutionOrNone(self, pyObject, pyAst, chainWithPosition):
        if PyforaInspect.isfunction(pyObject):
            return self._lookupChainInFunction(pyObject, chainWithPosition)

        if PyforaInspect.isclass(pyObject):
            return self._lookupChainInClass(pyObject, pyAst, chainWithPosition)

        return None
Exemple #3
0
    def _lookupChainInFunctionOrClass(self, pyObject, pyAst,
                                      chainWithPosition):
        if PyforaInspect.isfunction(pyObject):
            return self._lookupChainInFunction(pyObject, chainWithPosition)

        if PyforaInspect.isclass(pyObject):
            return self._lookupChainInClass(pyObject, pyAst, chainWithPosition)

        assert False, "should only have functions or classes here"
Exemple #4
0
    def _subchainAndResolutionOrNone(self, pyObject, chainWithPosition):
        if PyforaInspect.isfunction(pyObject):
            return self._lookupChainInFunction(pyObject, chainWithPosition)

        if PyforaInspect.isclass(pyObject):
            return self._lookupChainInClass(pyObject, chainWithPosition)

        raise Exceptions.PythonToForaConversionError(
            "don't know how to resolve %s in %s (line:%s)" %
            (chainWithPosition.var, pyObject, chainWithPosition.pos.lineno))
Exemple #5
0
    def _subchainAndResolutionOrNone(self, pyObject, chainWithPosition):
        if PyforaInspect.isfunction(pyObject):
            return self._lookupChainInFunction(pyObject, chainWithPosition)

        if PyforaInspect.isclass(pyObject):
            return self._lookupChainInClass(pyObject, chainWithPosition)

        raise Exceptions.PythonToForaConversionError(
            "don't know how to resolve %s in %s (line:%s)"
            % (chainWithPosition.var, pyObject, chainWithPosition.pos.lineno)
            )
Exemple #6
0
    def _objectToWalkInsteadOrNone(self, pyObject):
        """Takes a python object and returns another python object or None.

        If the given python object belongs to the PyObjectNode hierarchy,
        return None, otherwise build or fetch from a cache the appropriate
        PyObjectNode to walk, and return it.
        """

        tr = None
        if isinstance(pyObject, PyObjectNodes.PyObjectNode):
            return tr  # If this is the common path, we may want to pull it to the call-site(s)

        if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
            tr = PyObjectNodes.RemotePythonObject(pyObject)
        elif isinstance(
                pyObject, Exception
        ) and pyObject.__class__ in NamedSingletons.pythonSingletonToName:
            tr = PyObjectNodes.BuiltinExceptionInstance(
                pyObject,
                NamedSingletons.pythonSingletonToName[pyObject.__class__],
                pyObject.args)
        elif isinstance(
                pyObject,
            (type, type(isinstance)
             )) and pyObject in NamedSingletons.pythonSingletonToName:
            tr = PyObjectNodes.NamedSingleton(
                pyObject, NamedSingletons.pythonSingletonToName[pyObject])
        elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
            tr = self._pyObjectNodeForWithBlock(pyObject)
        elif id(pyObject) in self._convertedObjectCache:
            tr = self._convertedObjectCache[id(pyObject)][1]
        elif self.purePythonClassMapping.canMap(pyObject):
            pureInstance = self.purePythonClassMapping.mappableInstanceToPure(
                pyObject)
            self._convertedObjectCache[id(pyObject)] = (pyObject, pureInstance)
            tr = pureInstance
        elif isinstance(pyObject, tuple):
            tr = PyObjectNodes.Tuple(pyObject)
        elif isinstance(pyObject, list):
            tr = PyObjectNodes.List(pyObject)
        elif isinstance(pyObject, dict):
            tr = PyObjectNodes.Dict(pyObject)
        elif _isPrimitive(pyObject):
            tr = PyObjectNodes.Primitive(pyObject)
        elif PyforaInspect.isfunction(pyObject):
            tr = self._pyObjectNodeForFunction(pyObject)
        elif PyforaInspect.isclass(pyObject):
            tr = self._pyObjectNodeForClass(pyObject)
        elif isClassInstance(pyObject):
            tr = self._classInstanceDescriptionFromClassInstance(pyObject)

        return tr
    def populateModuleMembers(self, path):
        res = {}
        module = self.moduleForFile(path)

        for leafItemName in module.__dict__:
            leafItemValue = module.__dict__[leafItemName]

            if PyforaInspect.isclass(leafItemValue) or PyforaInspect.isfunction(leafItemValue):
                try:
                    _, lineNumber = PyforaInspect.findsource(leafItemValue)
                    res[lineNumber+1] = leafItemValue
                except:
                    pass

        self.moduleClassesAndFunctionsByPath[path] = res
    def populateModuleMembers(self, path):
        if path in self.moduleClassesAndFunctionsByPath:
            return

        res = self.moduleClassesAndFunctionsByPath[path] = {}
        module = self.moduleForFile(path)

        if module is not None and self.canPopulateForPath(path):
            for leafItemName in module.__dict__:
                leafItemValue = module.__dict__[leafItemName]

                if PyforaInspect.isclass(
                        leafItemValue) or PyforaInspect.isfunction(
                            leafItemValue):
                    try:
                        sourcePath = PyforaInspect.getsourcefile(leafItemValue)

                        if sourcePath is not None:
                            if os.path.samefile(path, sourcePath):
                                _, lineNumber = PyforaInspect.findsource(
                                    leafItemValue)

                                lineNumberToUse = lineNumber + 1

                                if lineNumberToUse in res and res[
                                        lineNumberToUse] is not leafItemValue:
                                    raise Exceptions.ForaToPythonConversionError(
                                        ("PythonObjectRehydratorHelpers got a line number collision at lineNumber %s"
                                         ", between %s and %s"),
                                        lineNumberToUse, leafItemValue,
                                        res[lineNumber + 1])

                                res[lineNumberToUse] = leafItemValue
                            else:
                                self.populateModuleMembers(sourcePath)

                    except Exceptions.ForaToPythonConversionError:
                        raise
                    except PyforaInspect.PyforaInspectError:
                        pass
                    except IOError:
                        #this gets raised when PyforaInspect can't find a file it needs
                        pass
                    except Exception as e:
                        logging.critical(
                            "PyforaInspect threw an exception: %s. tb = %s", e,
                            traceback.format_exc())
Exemple #9
0
    def _objectToWalkInsteadOrNone(self, pyObject):
        """Takes a python object and returns another python object or None.

        If the given python object belongs to the PyObjectNode hierarchy,
        return None, otherwise build or fetch from a cache the appropriate
        PyObjectNode to walk, and return it.
        """

        tr = None
        if isinstance(pyObject, PyObjectNodes.PyObjectNode):
            return tr  # If this is the common path, we may want to pull it to the call-site(s)

        if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
            tr = PyObjectNodes.RemotePythonObject(pyObject)
        elif isinstance(pyObject, Exception) and pyObject.__class__ in NamedSingletons.pythonSingletonToName:
            tr = PyObjectNodes.BuiltinExceptionInstance(
                pyObject, 
                NamedSingletons.pythonSingletonToName[pyObject.__class__], 
                pyObject.args
                )
        elif isinstance(pyObject, (type, type(isinstance))) and pyObject in NamedSingletons.pythonSingletonToName:
            tr = PyObjectNodes.NamedSingleton(pyObject, NamedSingletons.pythonSingletonToName[pyObject])
        elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
            tr = self._pyObjectNodeForWithBlock(pyObject)
        elif id(pyObject) in self._convertedObjectCache:
            tr = self._convertedObjectCache[id(pyObject)][1]
        elif self.purePythonClassMapping.canMap(pyObject):
            pureInstance = self.purePythonClassMapping.mappableInstanceToPure(pyObject)
            self._convertedObjectCache[id(pyObject)] = (pyObject, pureInstance)
            tr = pureInstance
        elif isinstance(pyObject, tuple):
            tr = PyObjectNodes.Tuple(pyObject)
        elif isinstance(pyObject, list):
            tr = PyObjectNodes.List(pyObject)
        elif isinstance(pyObject, dict):
            tr = PyObjectNodes.Dict(pyObject)
        elif _isPrimitive(pyObject):
            tr = PyObjectNodes.Primitive(pyObject)
        elif PyforaInspect.isfunction(pyObject):
            tr = self._pyObjectNodeForFunction(pyObject)
        elif PyforaInspect.isclass(pyObject):
            tr = self._pyObjectNodeForClass(pyObject)
        elif isClassInstance(pyObject):
            tr = self._classInstanceDescriptionFromClassInstance(pyObject)

        return tr
Exemple #10
0
 def _walkPyObject(self, pyObject, objectId):
     if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
         self._registerRemotePythonObject(objectId, pyObject)
     elif isinstance(pyObject, Future.Future):
         #it would be better to register the future and do a second pass of walking
         self._walkPyObject(pyObject.result(), objectId)
     elif isinstance(pyObject, _FileDescription):
         self._registerFileDescription(objectId, pyObject)
     elif isinstance(pyObject, Exception) and pyObject.__class__ in \
        NamedSingletons.pythonSingletonToName:
         self._registerBuiltinExceptionInstance(objectId, pyObject)
     elif isinstance(pyObject, (type, type(isinstance))) and \
        pyObject in NamedSingletons.pythonSingletonToName:
         self._registerNamedSingleton(
             objectId,
             NamedSingletons.pythonSingletonToName[pyObject]
             )
     elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
         self._registerWithBlock(objectId, pyObject)
     elif isinstance(pyObject, _Unconvertible):
         self._registerUnconvertible(objectId)
     elif isinstance(pyObject, tuple):
         self._registerTuple(objectId, pyObject)
     elif isinstance(pyObject, list):
         self._registerList(objectId, pyObject)
     elif isinstance(pyObject, dict):
         self._registerDict(objectId, pyObject)
     elif isPrimitive(pyObject):
         self._registerPrimitive(objectId, pyObject)
     elif PyforaInspect.isfunction(pyObject):
         self._registerFunction(objectId, pyObject)
     elif PyforaInspect.isclass(pyObject):
         self._registerClass(objectId, pyObject)
     elif isinstance(pyObject, instancemethod):
         self._registerInstanceMethod(objectId, pyObject)
     elif isClassInstance(pyObject):
         self._registerClassInstance(objectId, pyObject)
     else:
         assert False, "don't know what to do with %s" % pyObject
Exemple #11
0
    def populateModuleMembers(self, path):
        res = {}
        module = self.moduleForFile(path)

        if module is not None:
            for leafItemName in module.__dict__:
                leafItemValue = module.__dict__[leafItemName]

                if PyforaInspect.isclass(leafItemValue) or PyforaInspect.isfunction(leafItemValue):
                    try:
                        sourcePath = PyforaInspect.getsourcefile(leafItemValue)

                        if os.path.samefile(path, sourcePath):
                            _, lineNumber = PyforaInspect.findsource(leafItemValue)

                            lineNumberToUse = lineNumber + 1

                            if lineNumberToUse in res:
                                raise Exceptions.ForaToPythonConversionError(
                                    ("PythonObjectRehydrator got a line number collision at lineNumber %s"
                                     ", between %s and %s"),
                                    lineNumberToUse,
                                    leafItemValue,
                                    res[lineNumber + 1]
                                    )


                            res[lineNumberToUse] = leafItemValue
                        else:
                            self.populateModuleMembers(sourcePath)
                        
                    except Exceptions.ForaToPythonConversionError:
                        raise
                    except Exception as e:
                        logging.critical("PyforaInspect threw an exception: %s. tb = %s",
                                         e,
                                         traceback.format_exc())

        self.moduleClassesAndFunctionsByPath[path] = res
Exemple #12
0
 def _walkPyObject(self, pyObject, objectId):
     if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
         self._registerRemotePythonObject(objectId, pyObject)
     elif isinstance(pyObject, Future.Future):
         #it would be better to register the future and do a second pass of walking
         self._walkPyObject(pyObject.result(), objectId)
     elif isinstance(pyObject, _FileDescription):
         self._registerFileDescription(objectId, pyObject)
     elif isinstance(pyObject, Exception) and pyObject.__class__ in \
        NamedSingletons.pythonSingletonToName:
         self._registerBuiltinExceptionInstance(objectId, pyObject)
     elif isinstance(pyObject, (type, type(isinstance))) and \
        pyObject in NamedSingletons.pythonSingletonToName:
         self._registerNamedSingleton(
             objectId, NamedSingletons.pythonSingletonToName[pyObject])
     elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
         self._registerWithBlock(objectId, pyObject)
     elif isinstance(pyObject, _Unconvertible):
         self._registerUnconvertible(objectId)
     elif isinstance(pyObject, tuple):
         self._registerTuple(objectId, pyObject)
     elif isinstance(pyObject, list):
         self._registerList(objectId, pyObject)
     elif isinstance(pyObject, dict):
         self._registerDict(objectId, pyObject)
     elif isPrimitive(pyObject):
         self._registerPrimitive(objectId, pyObject)
     elif PyforaInspect.isfunction(pyObject):
         self._registerFunction(objectId, pyObject)
     elif PyforaInspect.isclass(pyObject):
         self._registerClass(objectId, pyObject)
     elif isinstance(pyObject, instancemethod):
         self._registerInstanceMethod(objectId, pyObject)
     elif isClassInstance(pyObject):
         self._registerClassInstance(objectId, pyObject)
     else:
         assert False, "don't know what to do with %s" % pyObject
Exemple #13
0
 def _classMemberFunctions(self, pyObject):
     return PyforaInspect.getmembers(
         pyObject,
         lambda elt: PyforaInspect.ismethod(elt) or PyforaInspect.isfunction(elt)
         )
Exemple #14
0
    def walkPyObject(self, pyObject):
        """
        `walkPyObject`: recursively traverse a live python object,
        registering its "pieces" with an `ObjectRegistry`
        (`self.objectRegistry`).

        Note that we use python `id`s for caching in this class,
        which means it cannot be used in cases where `id`s might get
        reused (recall they are just memory addresses).

        `objectId`s are assigned to all pieces of the python object.

        Returns:
            An `int`, the `objectId` of the root python object.
        """
        if id(pyObject) in self._pyObjectIdToObjectId:
            return self._pyObjectIdToObjectId[id(pyObject)]

        if id(pyObject) in self._convertedObjectCache:
            pyObject = self._convertedObjectCache[id(pyObject)]
        elif self._purePythonClassMapping.canMap(pyObject):
            pureInstance = self._purePythonClassMapping.mappableInstanceToPure(
                pyObject)
            self._convertedObjectCache[id(pyObject)] = pureInstance
            pyObject = pureInstance

        objectId = self._allocateId(pyObject)

        if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
            self._registerRemotePythonObject(objectId, pyObject)
        elif isinstance(pyObject, _FileDescription):
            self._registerFileDescription(objectId, pyObject)
        elif isinstance(pyObject, Exception) and pyObject.__class__ in \
           NamedSingletons.pythonSingletonToName:
            self._registerBuiltinExceptionInstance(objectId, pyObject)
        elif isinstance(pyObject, (type, type(isinstance))) and \
           pyObject in NamedSingletons.pythonSingletonToName:
            self._registerNamedSingleton(
                objectId, NamedSingletons.pythonSingletonToName[pyObject])
        elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
            self._registerWithBlock(objectId, pyObject)
        elif isinstance(pyObject, tuple):
            self._registerTuple(objectId, pyObject)
        elif isinstance(pyObject, list):
            self._registerList(objectId, pyObject)
        elif isinstance(pyObject, dict):
            self._registerDict(objectId, pyObject)
        elif isPrimitive(pyObject):
            self._registerPrimitive(objectId, pyObject)
        elif PyforaInspect.isfunction(pyObject):
            self._registerFunction(objectId, pyObject)
        elif PyforaInspect.isclass(pyObject):
            self._registerClass(objectId, pyObject)
        elif isinstance(pyObject, instancemethod):
            self._registerInstanceMethod(objectId, pyObject)
        elif isClassInstance(pyObject):
            self._registerClassInstance(objectId, pyObject)
        else:
            assert False, "don't know what to do with %s" % pyObject

        return objectId
Exemple #15
0
    def walkPyObject(self, pyObject):
        """
        `walkPyObject`: recursively traverse a live python object,
        registering its "pieces" with an `ObjectRegistry`
        (`self.objectRegistry`).

        Note that we use python `id`s for caching in this class,
        which means it cannot be used in cases where `id`s might get
        reused (recall they are just memory addresses).

        `objectId`s are assigned to all pieces of the python object.

        Returns:
            An `int`, the `objectId` of the root python object.
        """
        if id(pyObject) in self._pyObjectIdToObjectId:
            return self._pyObjectIdToObjectId[id(pyObject)]

        if id(pyObject) in self._convertedObjectCache:
            pyObject = self._convertedObjectCache[id(pyObject)]
        elif self._purePythonClassMapping.canMap(pyObject):
            pureInstance = self._purePythonClassMapping.mappableInstanceToPure(
                pyObject
                )
            self._convertedObjectCache[id(pyObject)] = pureInstance
            pyObject = pureInstance

        objectId = self._allocateId(pyObject)

        if isinstance(pyObject, RemotePythonObject.RemotePythonObject):
            self._registerRemotePythonObject(objectId, pyObject)
        elif isinstance(pyObject, _FileDescription):
            self._registerFileDescription(objectId, pyObject)
        elif isinstance(pyObject, Exception) and pyObject.__class__ in \
           NamedSingletons.pythonSingletonToName:
            self._registerBuiltinExceptionInstance(objectId, pyObject)
        elif isinstance(pyObject, (type, type(isinstance))) and \
           pyObject in NamedSingletons.pythonSingletonToName:
            self._registerNamedSingleton(
                objectId,
                NamedSingletons.pythonSingletonToName[pyObject]
                )
        elif isinstance(pyObject, PyforaWithBlock.PyforaWithBlock):
            self._registerWithBlock(objectId, pyObject)
        elif isinstance(pyObject, tuple):
            self._registerTuple(objectId, pyObject)
        elif isinstance(pyObject, list):
            self._registerList(objectId, pyObject)
        elif isinstance(pyObject, dict):
            self._registerDict(objectId, pyObject)
        elif isPrimitive(pyObject):
            self._registerPrimitive(objectId, pyObject)
        elif PyforaInspect.isfunction(pyObject):
            self._registerFunction(objectId, pyObject)
        elif PyforaInspect.isclass(pyObject):
            self._registerClass(objectId, pyObject)
        elif isinstance(pyObject, instancemethod):
            self._registerInstanceMethod(objectId, pyObject)
        elif isClassInstance(pyObject):
            self._registerClassInstance(objectId, pyObject)
        else:
            assert False, "don't know what to do with %s" % pyObject

        return objectId