Skip to content

getMath() is swapping objects somehow #9

@Karrenbelt

Description

@Karrenbelt

This is not the kind of behaviour that is expected from a getter; is it returning the original and keeping a copy?

Somehow the memory handling is not working correctly

import libsbml 
assert libsbml.__version__ == '5.18.0'
import string

def unique(sequence):
    seen = set()
    return [x for x in sequence if not (x in seen or seen.add(x))]

def simplify(li):
    d = dict(zip(unique(li), string.ascii_uppercase))
    return [d[el] for el in li]

level = 3
version = 2
f_def = libsbml.FunctionDefinition(level, version)
i_ass = libsbml.InitialAssignment(level, version)
k_law = libsbml.KineticLaw(level, version)

# same for libsbml.parseL3Formula('x'), thus not because it is empty or type is unknown 
a = libsbml.ASTNode() 
assert a is not None and a.isWellFormedASTNode()

ast_node_ids = []
for obj in [f_def, i_ass, k_law]:
    ids = []
    ids.append(id(a))             # 0. 'A'
    assert obj.setMath(a) == 0
    ids.append(id(a))             # 1. 'A'
    ids.append(id(obj.getMath())) # 2. 'B'
    ids.append(id(obj.getMath())) # 3. 'B'
    ids.append(id(obj.getMath())) # 4. 'B'

    # assigning to variables
    b = obj.getMath()
    ids.append(id(b))             # 5. 'B'
    ids.append(id(obj.getMath())) # 6. 'C'
    c = obj.getMath()
    ids.append(id(b))             # 7. 'B'
    ids.append(id(obj.getMath())) # 8. 'D'

    ## assigning to same variable as before: c
    c = obj.getMath()
    ids.append(id(obj.getMath())) # 9. 'C'

    ast_node_ids.append(ids)

# generate simpler representation
simpler = list(map(simplify, ast_node_ids))

for li in simpler:
    print(li)

output:

['A', 'A', 'B', 'B', 'B', 'B', 'C', 'B', 'D', 'C']
['A', 'A', 'B', 'B', 'B', 'B', 'C', 'B', 'D', 'C']
['A', 'A', 'B', 'B', 'B', 'B', 'C', 'B', 'D', 'C']

expected:

['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions