Batch AdImage remote creation raises I/O exception.
When calling AdImage().remote_create(batch=batch); batch.execute() the exception ValueError: I/O operation on closed file is raised.
When using the AdImage().remote_create() I found that this worked as intended and the AdImage object was returned with an 'id' and 'hash', but when attempting to do this while using a batch I ran into the I/O exception.
When looking into a possible fix for this, I tried looking at the AdImage's remote_create function (line number 1366 for me) and noticed that open_file was being closed before the batch could be executed. Commenting out the line that closed the file for the sake of testing made the call complete but an error response {u'error': {u'message': u'File <IMG_PATH> has not been attached', u'type': u'GraphBatchException'}} was returned.
Here is the script I started with:
from facebookads import FacebookSession, FacebookAdsApi
from facebookads.objects import AdAccount, AdImage
APP_ID= "<APP_ID>"
APP_SECRET = "<APP_SECRET>"
ACCESS_TOKEN="<ACCESS_TOKEN"
IMG_PATH= "<IMG PATH>"
session = FacebookSession(APP_ID, APP_SECRET, ACCESS_TOKEN)
api = FacebookAdsApi(session)
FacebookAdsApi.set_default_api(api)
account = AdAccount.get_my_account()
img = AdImage(parent_id=account.get_id_assured())
img[AdImage.Field.filename] = IMG_PATH
batch = account.get_api_assured().new_batch()
img.remote_create(batch=batch)
batch.execute()
Here is the stack trace that was outputted:
File "/Users/nilesnelson/projects/facebook-python-ads-sdk/facebookads/api.py", line 416, in execute
files=files,
File "/Users/nilesnelson/projects/facebook-python-ads-sdk/facebookads/api.py", line 270, in call
files=files,
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/sessions.py", line 443, in request
prep = self.prepare_request(req)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/sessions.py", line 374, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 307, in prepare
self.prepare_body(data, files, json)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 449, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 152, in _encode_files
rf = RequestField(name=k, data=fp.read(),
ValueError: I/O operation on closed file
Here is a test for integration.py that should do the same thing as the script:
class AdImageBatchTestCase(FacebookAdsTestCase):
def setup_ad_image(self):
img = objects.AdImage(
parent_id=self.TEST_ACCOUNT.get_id_assured(),
)
self.delete_in_teardown(img)
img[objects.AdImage.Field.filename] = self.TEST_IMAGE_PATH
return img
def test_batch_create_ad_image(self):
img = self.create_ad_image()
batch = FacebookAdsTestCase.TEST_API.new_batch()
img.remote_create(batch=batch)
batch.execute()
self.assertNotEquals(img['id'], None)
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.
Not able to reproduce this issue. If you are still facing this, please re open and we will take a look.