Face normals in background mode

I’m having an issue when creating a cube in Python. When I create it, three of the face normals are all in the wrong direction. I can fix this by using recalcNormals(), but this line causes Blender to crash when I run the script in background mode. My code is as follows,

 
def makeCube ( self, w, h, d, name ):
  w = w / 2
  h = h / 2
  d = d / 2
  
  # define vertices and faces for a pyramid
  coords=[ [w, h, d], [w, h, -d], [-w, h, d], [-w, h, -d], 
  [w, -h, d], [w, -h, -d], [-w, -h, d], [-w, -h, -d], [0, 0, 0] ]
  
  faces= [ [0,1,3,2], [4,5,7,6], [0,1,5,4], [2,3,7,6], [0,2,6,4], [1,3,7,5] ]
  me = Mesh.New(name)          # create a new mesh
  me.verts.extend(coords)          # add vertices to mesh
  me.faces.extend(faces)           # add faces to the mesh (also adds edges)
  me.vertexColors = 1              # enable vertex colors 
  me.faces[1].col[0].r = 255       # make each vertex a different color
  me.faces[1].col[1].g = 255
  me.faces[1].col[2].b = 255
  scn = Scene.GetCurrent()          # link object to current scene
  ob = scn.objects.new(me, name)
  
  Window.EditMode(0)
  me.recalcNormals()
  
  return me

My question is, how can I correctly calculate the correct normals without crashing Blender?