-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Fix JsonItemExporter puts lone comma in the output if encoder fails #5952
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
Fix JsonItemExporter puts lone comma in the output if encoder fails #5952
Conversation
Based on Mikhail's observation that to_bytes can fail also, leading to the same dangling comma as the failure to encode to JSON. Added a new test case to avoid reversion.
…ts_lone_comma-issue-3090
- Add initial changes from cathal's PR - scrapy#3090
Can you please check why some existing tests failed? Looks like some extra commas are added. |
- Handle exception not to add empty item. - scrapy#3090
- Add comment for handling the exception - scrapy#3090
- Remove unused import - scrapy#3090
Codecov Report
@@ Coverage Diff @@
## master #5952 +/- ##
==========================================
+ Coverage 88.86% 89.21% +0.34%
==========================================
Files 162 162
Lines 11199 11289 +90
Branches 1821 1833 +12
==========================================
+ Hits 9952 10071 +119
+ Misses 962 927 -35
- Partials 285 291 +6
|
- Fix invalid json issue - scrapy#3090
scrapy/exporters.py
Outdated
itemdict = dict(self._get_serialized_fields(item)) | ||
data = to_bytes(self.encoder.encode(itemdict), self.encoding) | ||
except TypeError as e: | ||
raise e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this catch+reraise does anything (except breaking the traceback IIRC)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But as this code does nothing then removing it won't break the test.
tests/test_exporters.py
Outdated
self.assertRaises(TypeError, self.ie.export_item, i2) | ||
self.ie.export_item(i3) | ||
self.ie.finish_exporting() | ||
print(f"values are=={self.output.getvalue()}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is a debug print?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, I'll remove it thanks! :)
- Perform CR changes - scrapy#3090
Thanks @adnan-awan! |
Continuation of #3111.
Resolves #3090, closes #3111.