You could try:

    layer = qgis.utils.iface.activeLayer()? 

I do not know why you still need to use it in the latest QGIS.