Esempio n. 1
0
        def onResultCallback(jsonResult):
            try:
                if isinstance(jsonResult, Exception):
                    future.set_exception(jsonResult)
                    return

                if 'foraToPythonConversionError' in jsonResult:
                    future.set_exception(
                        Exceptions.ForaToPythonConversionError(
                            str(jsonResult['foraToPythonConversionError'])))
                    return
                if not jsonResult['isException']:
                    if 'maxBytesExceeded' in jsonResult:
                        future.set_exception(
                            Exceptions.ResultExceededBytecountThreshold())
                    else:
                        result = self.objectRehydrator.convertJsonResultToPythonObject(
                            jsonResult['result'])
                        future.set_result(result)
                else:
                    result = self.objectRehydrator.convertJsonResultToPythonObject(
                        jsonResult['result'])
                    future.set_exception(
                        Exceptions.ComputationError(result,
                                                    jsonResult['trace']))
            except Exception as e:
                # TODO need a better way of wrapping exceptions.
                # Alexandros has some ideas here, but this is
                # better than the experience without the wrapping
                # (which is hanging)
                logging.error(
                    "Rehydration failed: %s\nResult was %s of type %s",
                    traceback.format_exc(), jsonResult, type(jsonResult))

                future.set_exception(Exceptions.ForaToPythonConversionError(e))
    def convertJsonResultToPythonObject(self, jsonResult):
        if 'primitive' in jsonResult:
            res = jsonResult['primitive']
            if isinstance(res, unicode):
                return intern(str(res))
            else:
                return res

        if 'tuple' in jsonResult:
            return tuple([self.convertJsonResultToPythonObject(x) for x in jsonResult['tuple']])
        if 'list' in jsonResult:
            return [self.convertJsonResultToPythonObject(x) for x in jsonResult['list']]
        if 'dict' in jsonResult:
            return {
                self.convertJsonResultToPythonObject(key): self.convertJsonResultToPythonObject(val) \
                for key, val in zip(jsonResult['dict']['keys'], jsonResult['dict']['values'])
                }
        if 'untranslatableException' in jsonResult:
            return Exceptions.ForaToPythonConversionError(
                "untranslatable FORA exception: %s" % jsonResult['untranslatableException']
                )
        if 'singleton' in jsonResult:
            singletonName = jsonResult['singleton']
            return NamedSingletons.singletonNameToObject[singletonName]
        if 'InvalidPyforaOperation' in jsonResult:
            return Exceptions.InvalidPyforaOperation(jsonResult['InvalidPyforaOperation'])
        if 'builtinException' in jsonResult:
            builtinExceptionTypeName = jsonResult['builtinException']
            builtinExceptionType = NamedSingletons.singletonNameToObject[builtinExceptionTypeName]
            args = self.convertJsonResultToPythonObject(jsonResult['args'])
            return builtinExceptionType(*args)
        if 'classInstance' in jsonResult:
            members = {k:self.convertJsonResultToPythonObject(v) for k,v in jsonResult['members'].iteritems()}
            classObject = self.convertJsonResultToPythonObject(jsonResult['classInstance'])
            return self._invertPureClassInstanceIfNecessary(self._instantiateClass(classObject, members))
        if 'functionInstance' in jsonResult:
            members = {k:self.convertJsonResultToPythonObject(v) for k,v in jsonResult['members'].iteritems()}
            return self._instantiateFunction(jsonResult['functionInstance'][0], jsonResult['functionInstance'][1], members)
        if 'classObject' in jsonResult:
            members = {k:self.convertJsonResultToPythonObject(v) for k,v in jsonResult['members'].iteritems()}
            return self._classObjectFromFilenameAndLine(jsonResult['classObject'][0], jsonResult['classObject'][1], members)
        if 'stacktrace' in jsonResult:
            return jsonResult['stacktrace']
        
        raise Exceptions.ForaToPythonConversionError("not implemented: cant convert %s" % jsonResult)
Esempio n. 3
0
            def extractVectorContents(vectorIVC):
                if len(vectorIVC) == 0:
                    return {'listContents': []}
                vec = ComputedValue.ComputedValueVector(
                    vectorImplVal=vectorIVC)
                vecSlice = vec.entireSlice

                res = None
                preventPythonArrayExtraction = False

                #see if it's a string. This is the only way to be holding a Vector of char
                if vectorIVC.isVectorOfChar():
                    res = vecSlice.extractVectorDataAsNumpyArray()
                    if res is not None:
                        res = {'string': res.tostring()}

                #see if it's simple enough to transmit as numpy data
                if res is None and len(vectorIVC.getVectorElementsJOR()
                                       ) == 1 and len(vectorIVC) > 1:
                    res = vecSlice.extractVectorDataAsNumpyArrayInChunks()

                    if res is not None:
                        firstElement = vecSlice.extractVectorItemAsIVC(0)
                        if firstElement is None:
                            #note we can't import this at the top of the file because this file gets imported
                            #during the build process, which doesn't have pyfora installed.
                            import pyfora.Exceptions as Exceptions
                            raise Exceptions.ForaToPythonConversionError(
                                "Shouldn't be possible to download data as numpy, and then not get the first value"
                            )

                        res = {
                            'firstElement': firstElement,
                            'contentsAsNumpyArrays': res
                        }
                    else:
                        if not vecSlice.vdmThinksIsLoaded():
                            #there's a race condition where the data could be loaded between now and
                            #the call to 'extractVectorDataAsPythonArray'. This prevents it.
                            preventPythonArrayExtraction = True

                #see if we can extract the data as a regular pythonlist
                if not preventPythonArrayExtraction and res is None:
                    res = vecSlice.extractVectorDataAsPythonArray()
                    if res is not None:
                        res = {'listContents': res}

                if res is None:
                    vecSlice.increaseRequestCount()
                    return None

                return res
Esempio n. 4
0
    def _translate_download_result(self, jsonResult):
        if 'foraToPythonConversionError' in jsonResult:
            return Exceptions.ForaToPythonConversionError(
                str(jsonResult['foraToPythonConversionError'])
                )

        if not jsonResult['isException']:
            if 'maxBytesExceeded' in jsonResult:
                return Exceptions.ResultExceededBytecountThreshold()
            else:
                return self.objectRehydrator.convertJsonResultToPythonObject(jsonResult['result'])

        result = self.objectRehydrator.convertJsonResultToPythonObject(jsonResult['result'])
        return Exceptions.ComputationError(result, jsonResult['trace'])
    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())
Esempio n. 6
0
        def onResultCallback(jsonResult):
            result = jsonResult
            try:
                if not isinstance(jsonResult, Exception):
                    result = self._translate_download_result(jsonResult)
            except Exception as e:
                # TODO need a better way of wrapping exceptions.
                # Alexandros has some ideas here, but this is
                # better than the experience without the wrapping
                # (which is hanging)
                def clip(s):
                    if len(s) > 250:
                        return s[:250] + "... (" + str(
                            len(s) - 250) + " characters remaining)"
                    return s

                logging.error(
                    "Rehydration failed: %s\nResult was %s of type %s",
                    traceback.format_exc(), clip(repr(jsonResult)),
                    type(jsonResult))
                result = Exceptions.ForaToPythonConversionError(e)
            self._resolve_future(future, result)
Esempio n. 7
0
        def convertInner(objectDef):
            if 'primitive' in objectDef:
                res = objectDef['primitive']
                if isinstance(res, str):
                    return intern(str(base64.b64decode(res)))
                else:
                    return res

            if 'tuple' in objectDef:
                return tuple([convert(x) for x in objectDef['tuple']])
            if 'list' in objectDef:
                return [convert(x) for x in objectDef['list']]
            if 'dict' in objectDef:
                return {
                    convert(key): convert(val)
                    for key, val in zip(objectDef['dict']['keys'],
                                        objectDef['dict']['values'])
                }
            if 'untranslatableException' in objectDef:
                return Exceptions.ForaToPythonConversionError(
                    "untranslatable FORA exception: %s" %
                    objectDef['untranslatableException'])
            if 'singleton' in objectDef:
                singletonName = objectDef['singleton']
                return NamedSingletons.singletonNameToObject[singletonName]
            if 'InvalidPyforaOperation' in objectDef:
                return Exceptions.InvalidPyforaOperation(
                    objectDef['InvalidPyforaOperation'])
            if 'homogenousListNumpyDataStringsAndSizes' in objectDef:
                stringsAndSizes = objectDef[
                    'homogenousListNumpyDataStringsAndSizes']

                dtype = cPickle.loads(base64.b64decode(objectDef['dtype']))
                data = numpy.zeros(shape=objectDef['length'], dtype=dtype)

                curOffset = 0
                for dataAndSize in stringsAndSizes:
                    arrayText = dataAndSize['data']
                    size = dataAndSize['length']
                    data[curOffset:curOffset + size] = numpy.ndarray(
                        shape=size,
                        dtype=dtype,
                        buffer=base64.b64decode(arrayText))
                    curOffset += size

                #we use the first element as a prototype when decoding
                firstElement = convert(objectDef['firstElement'])

                data = data.tolist()
                assert isinstance(
                    data[0], type(firstElement)), "%s of type %s is not %s" % (
                        data[0], type(data[0]), type(firstElement))
                return data

            if 'builtinException' in objectDef:
                builtinExceptionTypeName = objectDef['builtinException']
                builtinExceptionType = NamedSingletons.singletonNameToObject[
                    builtinExceptionTypeName]
                args = convert(objectDef['args'])
                return builtinExceptionType(*args)
            if 'classInstance' in objectDef:
                members = {
                    k: convert(v)
                    for k, v in objectDef['members'].iteritems()
                }
                classObject = convert(objectDef['classInstance'])
                return self._invertPureClassInstanceIfNecessary(
                    self._instantiateClass(classObject, members))
            if 'pyAbortException' in objectDef:
                pyAbortExceptionTypeName = objectDef['pyAbortException']
                pyAbortExceptionType = PyAbortSingletons.singletonNameToObject[
                    pyAbortExceptionTypeName]
                args = convert(objectDef['args'])
                return pyAbortExceptionType(*args)
            if 'boundMethodOn' in objectDef:
                instance = convert(objectDef['boundMethodOn'])
                try:
                    return getattr(instance, objectDef['methodName'])
                except AttributeError:
                    raise Exceptions.ForaToPythonConversionError(
                        "Expected %s to have a method of name %s which it didn't"
                        % (instance, objectDef['methodName']))
            if 'functionInstance' in objectDef:
                members = {
                    k: convert(v)
                    for k, v in objectDef['members'].iteritems()
                }
                return self._instantiateFunction(
                    objectDef['functionInstance'][0],
                    objectDef['functionInstance'][1], members)
            if 'classObject' in objectDef:
                members = {
                    k: convert(v)
                    for k, v in objectDef['members'].iteritems()
                }
                return self._classObjectFromFilenameAndLine(
                    objectDef['classObject'][0], objectDef['classObject'][1],
                    members)
            if 'stacktrace' in objectDef:
                return objectDef['stacktrace']

            raise Exceptions.ForaToPythonConversionError(
                "not implemented: cant convert %s" % objectDef)