nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: RaRe-Technologies/smart_open
Pull request #689 (2022-03-18 09:40:52)

Make UploadFailedError picklable
#### Make UploadFailedError picklable

#### Motivation

The [ray](https://github.com/ray-project/ray) library is not able to handle unpicklable exceptions sufficiently, see https://github.com/ray-project/ray/issues/21075 for a similar issue.

#### Tests

The [PickleUploadFailedTest](https://github.com/Imerso3D/smart_open/blob/e4016299a728ea6f346e3780b1da5900450a971f/smart_open/tests/test_gcs.py#L932) test case has been added to `test_gcs.py`.

#### Checklist

Before you create the PR, please make sure you have:

- [x] Picked a concise, informative and complete title
- [x] Clearly explained the motivation behind the PR
- [x] Linked to any existing issues that your PR will be solving
- [x] Included tests for any new functionality
- [x] Checked that all unit tests pass



mpenkov: Hi, can you please make this PR editable for maintainers?
birgerbr: > Hi, can you please make this PR editable for maintainers?
> 
> As a maintainer, I'd like to merge the most recent upstream into your PR, but need you to give me permission first.

I added you with write permission to https://github.com/Imerso3D/smart_open.
Is that sufficient?
mpenkov: Yes, that's a little excessive (you can set it for individual PRs instead of the whole repo) but it will get the job done. Thanks.

Patch:
diff --git a/smart_open/gcs.py b/smart_open/gcs.py
index 1baa4b4..dc0e504 100644
--- a/smart_open/gcs.py
+++ b/smart_open/gcs.py
@@ -78,6 +78,9 @@ class UploadFailedError(Exception):
         self.status_code = status_code
         self.text = text
 
+    def __reduce__(self):
+        return UploadFailedError, (self.args[0], self.status_code, self.text)
+
 
 def _fail(response, part_num, content_length, total_size, headers):
     status_code = response.status_code


Test patch:
diff --git a/smart_open/tests/test_gcs.py b/smart_open/tests/test_gcs.py
index f528ca7..22eb7f4 100644
--- a/smart_open/tests/test_gcs.py
+++ b/smart_open/tests/test_gcs.py
@@ -10,6 +10,7 @@ import inspect
 import io
 import logging
 import os
+import pickle
 import time
 import uuid
 import unittest
@@ -928,6 +929,16 @@ class MakeRangeStringTest(unittest.TestCase):
         self.assertEqual(smart_open.gcs._make_range_string(start, stop), 'bytes 1-2/*')
 
 
+class PickleUploadFailedTest(unittest.TestCase):
+    def test_pickle_upload_failed(self):
+        original = smart_open.gcs.UploadFailedError("foo", 123, "bar")
+        recovered: smart_open.gcs.UploadFailedError = pickle.loads(pickle.dumps(original))
+
+        self.assertEqual(original.args, recovered.args)
+        self.assertEqual(original.text, recovered.text)
+        self.assertEqual(original.status_code, recovered.status_code)
+
+
 if __name__ == '__main__':
     logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
     unittest.main()