Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch AdImage remote creation raises I/O exception. #32

Closed
nilesnelson opened this issue Feb 6, 2015 · 2 comments
Closed

Batch AdImage remote creation raises I/O exception. #32

nilesnelson opened this issue Feb 6, 2015 · 2 comments

Comments

@nilesnelson
Copy link

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)
@ghost
Copy link

ghost commented Aug 4, 2015

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.

@rituparnamukherjee
Copy link
Contributor

Not able to reproduce this issue. If you are still facing this, please re open and we will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants