Cell.shapes(): "Cannot call non-const method on a const reference"

edited February 2019 in Python scripting

Hello,

I get an error with Klayout 0.25.7 on Windows with the following script when I have an instance in the selection.

import pya
lv = pya.LayoutView.current()
for obj in lv.object_selection:
  if obj.is_cell_inst(): 
    cell = obj.inst().cell
    for layer in lv.each_layer():
      layer_index = layer.layer_index()
      shapes = cell.shapes(layer_index)

The error message is Cannot call non-const method on a const reference in Cell.shapes.

The same script works correctly with Klayout 0.52.2 on Debian Linux.

Olivier

Comments

  • Hi Olivier,

    I can't reproduce the issue, but I think I have some idea what happens. I'll try further to find a way to reproduce it.

    The workaround is probably to use the cell index to obtain the cell:

    # instead of 
    #    cell = obj.inst().cell
    # use:
      cell = lv.cellview(obj.cv_index).layout().cell(obj.inst().cell_index)
    

    Matthias

  • Hello,
    I just did the following:
    (1) I reproduced the error with my script above => the error is reproduced.
    (2) I changed the code with the line that you indicated => the script now works.
    (3) I changed back to the original script => the original script now works!
    (4) I quit and restart Klayout and check the original script => the error is reproduced.

    Point (3) is quite strange to me!
    Olivier

  • Hi Olivier,

    the issue should now be fixed with 0.25.8.

    (3) may happen because when you run a script and have code inside some classes, the classes may not be redefined. Remember that you're working inside a live Ruby/Python interpreter: classes you have defined or files you have "required" or imported may not be overwritten when you rerun a script.

    I don't know your full setup, so I'm just guessing.

    Matthias

  • I now have Klayout 0.25.8 under Windows, and the error mentioned above still occurs.
    Olivier

    PS: Your explanation about point (3) makes sense, but my full setup is just the script above within Klayout. I didn't define any class or imported any module (except "pya").

  • Hi Olivier,

    the ticket is not fixed yet - it's a bigger issue. I'd suggest to use the workaround for some time.

    I can reproduce (3), but I have not debugged the issue yet. But I think I can explain the issue:

    The problem is basically that C++ has a concept of "const objects" which means objects cannot be modified (some kind of read-only mode). Such a concept does not exist in Python, so there needs to be some emulation. Modifying an object in Python which C++ thinks is const is not doing any good.

    The emulation works as long as objects are const or non-const. A C++ object will then be turned into a Python reference and this reference disallows modification if it was taken from a const object. But if such an object is used in non-const context, then the reference will be turned into a non-const one and this will not happen here automatically. The workaround enforces this and as the original C++ object is the same than before, after having it in non-const mode once it will remain there. Hence the problem disappears, even if you switch back to the original code.

    I admit that's confusing, but that's one of the nasty things that happen when bridging C++ and Python.

    Matthias

  • Hi,

    I'm getting an error similar to this, except in Cell.each_inst. The error "Cannot call non-const method on a const reference in Cell.each_inst" appears when running the following macro, with cells containing other cell instances selected:


    import pya lv = pya.LayoutView.current() for obj in lv.object_selection: if obj.is_cell_inst(): cell = obj.inst().cell for inst in cell.each_inst(): print("{}: {}".format(inst.cell.name, inst.dtrans))

    The error happens randomly, I would say about 50% of the time. If the error happens once, then it appears to happen every time when running the macro again, until KLayout is restarted. Likewise, if the macro runs without error once, then it seems to run without error any number of times until KLayout is restarted.

    I'm using KLayout 0.26.7 on Windows, and I believe this didn't happen in 0.26.4 (not completely sure).

    It looks like the workaround suggested above (using cell_index instead of cell) works, but for our actual code it would be nicer to not need it. I notice the linked github issue was closed, but it seems that at least this closely related bug still exists.

    Janne

  • I have reopened the issue. But for now, please use the cell_index workaround.

Sign In or Register to comment.