Using closest_point_on_mesh on object manipulated as bmesh

I am trying to script some object manipulation using the bmesh module.

During steps of this scripting I would like to be able to use the closest_point_on_mesh function. However I get unexpected results.

A minimal working example looks like this:

import bpy
import bmesh
from mathutils import Vector


bpy.ops.mesh.primitive_ico_sphere_add(
  subdivisions=1,
  size=1,
  location=(0,0,0))

bpy.ops.object.select_pattern(pattern='Icosphere')

obj = bpy.context.active_object
bpy.ops.object.mode_set(mode='EDIT')

bm = bmesh.new()
bm.from_mesh(obj.data)

# do some stuff to the bmesh

bpy.ops.object.mode_set(mode='OBJECT')
bm.to_mesh(obj.data)

# get a face for testing
face = bm.faces[0]

# getting the "middle" of the face
xyz = Vector((0,0,0))
for v in face.verts:
  xyz += v.co/3

# the middle of the face
print(xyz,'xyz')

location,normal,index = obj.closest_point_on_mesh(xyz)

# prints 0,0,0. expected the same as xyz three lines above
print(location,'location')

I expect the value of location to be the same as (or close to) the value of xyz, but it prints 0,0,0.

What am I doing wrong? Is there a better way of doing this?

(Note: I posted this on blender.stackexchange as well, but I haven’t gotten any replies there. I will update both threads with any working answer. http://blender.stackexchange.com/questions/18092/using-closest-point-on-mesh-on-object-manipulated-as-bmesh)

@inconvergent Nothing wrong with your script. Following is output on my PC

<Vector (0.1491, -0.4588, -0.6315)> xyz
<Vector (0.1491, -0.4588, -0.6315)> location
<Vector (0.1876, -0.5774, -0.7947)> normal
0 index