Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

edited February 22 in Python scripting


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.



  • 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)


  • 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!

  • 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.


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

    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.


Sign In or Register to comment.