Ejemplo n.º 1
0
    def reverseParseCheck(self, f):
        pyast = python_ast.convertFunctionToAlgebraicPyAst(f)
        native_ast = python_ast.convertAlgebraicToPyAst(pyast)
        pyast2 = python_ast.convertPyAstToAlgebraic(native_ast, pyast.filename)
        f2 = python_ast.evaluateFunctionPyAst(pyast2)
        pyast3 = python_ast.convertFunctionToAlgebraicPyAst(f2)

        self.assertEqual(pyast, pyast2)
        self.assertEqual(pyast, pyast3)
    def test_basic_parsing(self):
        pyast = python_ast.convertFunctionToAlgebraicPyAst(
            lambda: X)  # noqa: F821

        self.assertTrue(pyast.matches.Lambda)
        self.assertTrue(pyast.body.matches.Name)
        self.assertEqual(pyast.body.id, "X")
Ejemplo n.º 3
0
    def representationFor(self, inst):
        ''' Return the representation of a given instance or None.

            For certain types, we want to special-case how they are serialized.
            For those types, we return a representation object and for other
            types we return None.

            @param inst: an instance to be serialized
            @return a representation object or None
        '''
        if isinstance(inst, numpy.ndarray):
            result = inst.__reduce__()
            #compress the numpy data
            if self.numpyCompressionEnabled:
                result = (result[0], result[1], result[2][:-1] +
                          (lz4.frame.compress(result[2][-1]), ))
            return result

        if isinstance(inst, numpy.number):
            return inst.__reduce__() + (None, )

        if isinstance(inst, numpy.dtype):
            return (numpy.dtype, (str(inst), ), None)

        if isinstance(inst, FunctionType):
            representation = {}
            representation["qualname"] = inst.__qualname__
            representation["name"] = inst.__name__
            representation["module"] = inst.__module__

            all_names = set()

            def walkCodeObject(code):
                all_names.update(code.co_names)
                #there are 'code' objects for embedded list comprehensions.
                for c in code.co_consts:
                    if type(c) is type(code):
                        walkCodeObject(c)

            walkCodeObject(inst.__code__)

            representation["freevars"] = {
                k: v
                for k, v in inst.__globals__.items() if k in all_names
            }

            for ix, x in enumerate(inst.__code__.co_freevars):
                representation["freevars"][x] = inst.__closure__[
                    ix].cell_contents

            args = (convertFunctionToAlgebraicPyAst(
                inst, keepLineInformation=self.encodeLineInformationForCode), )

            return (createEmptyFunction, args, representation)

        return None
    def reverseParseAndEvalCheck(self, f, args):
        try:
            iter(args)
        except TypeError:
            args = tuple([args])

        pyast = python_ast.convertFunctionToAlgebraicPyAst(f)

        f_2 = python_ast.evaluateFunctionPyAst(pyast)

        self.assertEqual(f(*args), f_2(*args))
Ejemplo n.º 5
0
    def reverseParseAndEvalCheck(self, f, arg):
        pyast = python_ast.convertFunctionToAlgebraicPyAst(f)

        f_2 = python_ast.evaluateFunctionPyAst(pyast)

        self.assertEqual(f(arg), f_2(arg))
Ejemplo n.º 6
0
    def representationFor(self, inst):
        ''' Return the representation of a given instance or None.

            For certain types, we want to special-case how they are serialized.
            For those types, we return a representation object and for other
            types we return None.

            The representation consists of a tuple (factory, args, representation).

            During reconstruction, we call factory(*args) to produce an emnpty
            'skeleton' object, and then call `setInstanceStateFromRepresentation`
            with the resulting object and the 'representation'. The values returned
            for  'factory' and 'args' may not have circular dependencies with the current
            object - we deserialize those first, call factory(*args) to get the
            resulting object, and that object gets returned to any objects inside of
            'representation' that have references to the original object.

            @param inst: an instance to be serialized
            @return a representation object or None
        '''
        if GoogleProtobufMessage is not None and isinstance(inst, GoogleProtobufMessage):
            return (type(inst), (), inst.SerializeToString())

        if isinstance(inst, type):
            isTF = isTypeFunctionType(inst)
            if isTF is not None:
                return (reconstructTypeFunctionType, isTF, None)

        if isinstance(inst, numpy.ndarray):
            return inst.__reduce__()

        if isinstance(inst, numpy.number):
            return inst.__reduce__() + (None,)

        if isinstance(inst, numpy.dtype):
            return (numpy.dtype, (str(inst),), None)

        if isinstance(inst, datetime.datetime):
            return inst.__reduce__() + (None,)

        if isinstance(inst, datetime.date):
            return inst.__reduce__() + (None,)

        if isinstance(inst, datetime.time):
            return inst.__reduce__() + (None,)

        if isinstance(inst, datetime.timedelta):
            return inst.__reduce__() + (None,)

        if isinstance(inst, datetime.tzinfo):
            return inst.__reduce__() + (None,)

        if isinstance(inst, FunctionType):
            representation = {}
            representation["qualname"] = inst.__qualname__
            representation["name"] = inst.__name__
            representation["module"] = inst.__module__

            all_names = set()

            def walkCodeObject(code):
                all_names.update(code.co_names)
                # there are 'code' objects for embedded list comprehensions.
                for c in code.co_consts:
                    if type(c) is type(code):
                        walkCodeObject(c)

            walkCodeObject(inst.__code__)

            representation["freevars"] = {k: v for k, v in inst.__globals__.items() if k in all_names}

            for ix, x in enumerate(inst.__code__.co_freevars):
                representation["freevars"][x] = inst.__closure__[ix].cell_contents

            args = (convertFunctionToAlgebraicPyAst(inst, keepLineInformation=self.encodeLineInformationForCode),)

            return (createEmptyFunction, args, representation)

        return None