It is very easy to do with Shapely. The secret is the union predicate which divides the lines in segments at each intersection.
The Polygon is converted to a LinearRing or LineString an the union is done.
from shapely.geometry import LineString, Polygon
# union of the line and the LinearRing of the Polygon
result = line.union(LineString(list(polygon.exterior.coords)))
The result is a MultiLineString
Now we can use use the polygonize function
from shapely.ops import polygonize
for geom in polygonize(result):
In PyQGIS the combine predicate is the equivalent of union, see Python: Union selected polygons in a layer)
ring = poly.asPolygon()
linearRing = ring.asPolyline()
result = line.combine(linearRing)
The result is the same MultiLineString but PyQGIS has no polygonize function. There is an algorithm in the Processing Toobox (qgis:polygonize) but it crashes with MultiLineStrings in my case. So I continue to use shapely.