Example #1
0
    def test_collectDataMembersSetInInit_error_2(self):
        class E2:
            def __init__(*args):
                self.x = 0

        with self.assertRaises(Exceptions.PythonToForaConversionError):
            PyAstUtil.collectDataMembersSetInInit(E2)
Example #2
0
    def test_collectDataMembersSetInInit_error_2(self):
        class E2:
            def __init__(*args):
                self.x = 0

        with self.assertRaises(Exceptions.PythonToForaConversionError):
            PyAstUtil.collectDataMembersSetInInit(E2)
Example #3
0
    def test_collectDataMembersSetInInit_error_3(self):
        class E3:
            def __init__(self):
                self.x = 0
                def f(x):
                    return x

        with self.assertRaises(Exceptions.PythonToForaConversionError):
            PyAstUtil.collectDataMembersSetInInit(E3)
Example #4
0
    def test_collectDataMembersSetInInit_error_4(self):
        class E4:
            def __init__(self):
                self.x = 0
                class c(object):
                    def __init__(self):
                        self.z = 0

        with self.assertRaises(Exceptions.PythonToForaConversionError):
            PyAstUtil.collectDataMembersSetInInit(E4)
Example #5
0
    def _registerClassInstance(self, objectId, classInstance):
        """
        `_registerClassInstance`: register a `class` instance
        with `self.objectRegistry`.

        Recursively call `walkPyObject` on the class of the `classInstance`
        and on the data members of the instance.
        """
        classObject = classInstance.__class__
        classId = self.walkPyObject(classObject)

        try:
            dataMemberNames = PyAstUtil.collectDataMembersSetInInit(
                classObject)
        except Exceptions.CantGetSourceTextError:
            self._raiseConversionErrorForSourceTextError(classInstance)
        except:
            logging.error('Failed on %s (of type %s)', classInstance,
                          type(classInstance))
            raise
        classMemberNameToClassMemberId = {}
        for dataMemberName in dataMemberNames:
            memberId = self.walkPyObject(getattr(classInstance,
                                                 dataMemberName))
            classMemberNameToClassMemberId[dataMemberName] = memberId

        self._objectRegistry.defineClassInstance(
            objectId=objectId,
            classId=classId,
            classMemberNameToClassMemberId=classMemberNameToClassMemberId)
Example #6
0
    def _registerClassInstance(self, objectId, classInstance):
        """
        `_registerClassInstance`: register a `class` instance
        with `self.objectRegistry`.

        Recursively call `walkPyObject` on the class of the `classInstance`
        and on the data members of the instance.
        """
        classObject = classInstance.__class__
        classId = self.walkPyObject(classObject)

        try:
            dataMemberNames = PyAstUtil.collectDataMembersSetInInit(classObject)
        except Exceptions.CantGetSourceTextError:
            self._raiseConversionErrorForSourceTextError(classInstance)
        except:
            logging.error('Failed on %s (of type %s)',
                          classInstance, type(classInstance))
            raise
        classMemberNameToClassMemberId = {}
        for dataMemberName in dataMemberNames:
            memberId = self.walkPyObject(getattr(classInstance, dataMemberName))
            classMemberNameToClassMemberId[dataMemberName] = memberId

        self._objectRegistry.defineClassInstance(
            objectId=objectId,
            classId=classId,
            classMemberNameToClassMemberId=classMemberNameToClassMemberId
            )
Example #7
0
    def test_collectDataMembersSetInInit_4(self):
        class C4:
            def __init__(self, arg):
                (self.x, self.y), self.z = arg

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C4)

        self.assertEqual(set(dataMembers), set(['x', 'y', 'z']))
Example #8
0
    def test_collectDataMembersSetInInit_3(self):
        class C3:
            def __init__(self):
                self.x = self.y = 0

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C3)

        self.assertEqual(set(dataMembers), set(['x', 'y']))
Example #9
0
    def test_collectDataMembersSetInInit_1(self):
        class C:
            def __init__(self, x):
                self.x = x
                self.y = x**2.0

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C)

        self.assertEqual(set(dataMembers), set(['x', 'y']))
Example #10
0
    def test_collectDataMembersSetInInit_2(self):
        class C2:
            def __init__(self, x):
                if x > 0:
                    self.x = x

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C2)

        # in our translation, we're _always_ producing an x member
        self.assertEqual(set(dataMembers), set(['x']))
Example #11
0
    def test_collectDataMembersSetInInit_4(self):
        class C4:
            def __init__(self, arg):
                (self.x, self.y), self.z = arg

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C4)

        self.assertEqual(
            dataMembers,
            set(['x', 'y', 'z'])
            )
Example #12
0
    def test_collectDataMembersSetInInit_3(self):
        class C3:
            def __init__(self):
                self.x = self.y = 0

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C3)

        self.assertEqual(
            dataMembers,
            set(['x', 'y'])
            )
Example #13
0
    def test_collectDataMembersSetInInit_1(self):
        class C:
            def __init__(self, x):
                self.x = x
                self.y = x ** 2.0

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C)
            
        self.assertEqual(
            dataMembers,
            set(['x', 'y'])
            )
Example #14
0
    def test_collectDataMembersSetInInit_2(self):
        class C2:
            def __init__(self, x):
                if x > 0:
                    self.x = x
                

        dataMembers = PyAstUtil.collectDataMembersSetInInit(C2)
        
        # in our translation, we're _always_ producing an x member
        self.assertEqual(
            dataMembers,
            set(['x'])
            )
Example #15
0
    def _registerClassInstance(self, objectId, classInstance):
        """
        `_registerClassInstance`: register a `class` instance
        with `self.objectRegistry`.

        Recursively call `walkPyObject` on the class of the `classInstance`
        and on the data members of the instance.
        """
        classObject = classInstance.__class__
        classId = self.walkPyObject(classObject)

        dataMemberNames = classInstance.__dict__.keys() if hasattr(classInstance, '__dict__') \
            else PyAstUtil.collectDataMembersSetInInit(classObject)
        classMemberNameToClassMemberId = {}
        for dataMemberName in dataMemberNames:
            memberId = self.walkPyObject(getattr(classInstance, dataMemberName))
            classMemberNameToClassMemberId[dataMemberName] = memberId

        self._objectRegistry.defineClassInstance(
            objectId=objectId,
            classId=classId,
            classMemberNameToClassMemberId=classMemberNameToClassMemberId
            )
Example #16
0
    def _registerClassInstance(self, objectId, classInstance):
        """
        `_registerClassInstance`: register a `class` instance
        with `self.objectRegistry`.

        Recursively call `walkPyObject` on the class of the `classInstance`
        and on the data members of the instance.
        """
        classObject = classInstance.__class__
        classId = self.walkPyObject(classObject)

        dataMemberNames = classInstance.__dict__.keys() if hasattr(classInstance, '__dict__') \
            else PyAstUtil.collectDataMembersSetInInit(classObject)
        classMemberNameToClassMemberId = {}
        for dataMemberName in dataMemberNames:
            memberId = self.walkPyObject(getattr(classInstance,
                                                 dataMemberName))
            classMemberNameToClassMemberId[dataMemberName] = memberId

        self._objectRegistry.defineClassInstance(
            objectId=objectId,
            classId=classId,
            classMemberNameToClassMemberId=classMemberNameToClassMemberId)