bpy.ops.mesh.fill() Warning: No edges selected I'm attempting to write a background script to process an obj file. It's a single mesh with a hole at both ends; I want to fill in those holes with triangles for rendering purposes.

The shape is along the Y axis, with holes at X=0 and X=[largest X]. I appear to be able to find and select those edges, but bpy.ops.mesh.fill() continues to give a "No edges selected" error. I've tried using bmesh, and also bpy.ops.mesh.loop_multi_select(ring=False) (in every combination!) but nothing seems to make any difference. I'm about to give up and write the fill manually. My code so far is as follows:

import sys
import bpy
import bmesh

argv = sys.argv
argv = argv[argv.index("--") + 1:]  # get all args after "--"
filename = argv[0]
filenameOut = argv[1]

#Delete the cube mesh
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.delete(type='VERT')

#Import object:
status = bpy.ops.import_scene.obj(filepath=filename)

#Deselect everything
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bpy.ops.object.mode_set(mode='OBJECT')

mesh=bpy.data.objects[0].data
EPSILON = 1.0e-5

#Find the highest X coordinate
maxX=0
for vert in mesh.vertices:
    if vert.co[0]&gt;maxX:
        maxX=vert.co[0]

print(maxX)

i=0
for edge in mesh.edges:
    v1=mesh.vertices[edge.vertices[0]]
    v2=mesh.vertices[edge.vertices[1]]
    if -EPSILON &lt;= v1.co[0] &lt;= EPSILON and -EPSILON &lt;= v2.co[0] &lt;= EPSILON:
        i=i+1
        edge.select = True
        v1.select = True
        v2.select = True
print(i)

i=0;
for edge in mesh.edges:
    v1=mesh.vertices[edge.vertices[0]]
    v2=mesh.vertices[edge.vertices[1]]
    if maxX-EPSILON &lt;= v1.co[0] &lt;= maxX+EPSILON and maxX-EPSILON &lt;= v2.co[0] &lt;= maxX+EPSILON:
        i=i+1
        edge.select = True
        v1.select = True
        v2.select = True
print(i)

bpy.ops.object.mode_set(mode='EDIT')

#Just checking...
i=0
for edge in mesh.edges:
    if edge.select:
        i=i+1
print(i)

print("a")
bpy.ops.mesh.fill()
print("b")

bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.merge()

bpy.ops.export_scene.obj(filepath=filenameOut)


Output is as follows:

[importing file OK]
1.587500810623169
164
164
328
a
Warning: No edges selected
Warning: No edges selected
b
[exporting file]


Uh. What? 
I could give the bmesh code as well, but it too seems to successfully select edges... only for the fill() to  give the same error.
I believe it's because I hadn't marked the object as active. I added

scn = bpy.context.scene
scn.objects.active = bpy.data.objects[0]


before doing the fill, and that seemed to fix the error. Of course, the fill was completely ... not what I'd expected so I'm persevering with a manual one for now!