Title: fix: skip non-image parts when matching by sha1 in ImageParts by TemRevil · Pull Request #1566 · python-openxml/python-docx · GitHub
Open Graph Title: fix: skip non-image parts when matching by sha1 in ImageParts by TemRevil · Pull Request #1566 · python-openxml/python-docx
X Title: fix: skip non-image parts when matching by sha1 in ImageParts by TemRevil · Pull Request #1566 · python-openxml/python-docx
Description: Closes #1565. Problem Package._gather_image_parts walks the package relationships and appends the target of every image relationship to the ImageParts collection, casting it to ImagePart: def _gather_image_parts(self): for rel in self.iter_rels(): if rel.is_external: continue if rel.reltype != RT.IMAGE: continue if rel.target_part in self.image_parts: continue self.image_parts.append(cast("ImagePart", rel.target_part)) The cast is only a type hint. If an image uses a type that does not load as an ImagePart (an SVG, or an image with broken or non-standard EXIF data as in the report), the target part is a plain Part, which has no sha1. Once such a part is in the collection, the next add_picture call runs ImageParts._get_by_sha1, which reads .sha1 on every collected part, so it raised 'Part' object has no attribute 'sha1'. Fix Skip parts that have no sha1 while matching: for image_part in self._image_parts: if not hasattr(image_part, "sha1"): continue if image_part.sha1 == sha1: return image_part This mirrors the same fix already made in python-pptx (_ImageParts._find_by_sha1, commit 4c0fd125), which the issue links to. Tests Added it_skips_parts_without_a_sha1_when_matching in tests/test_package.py. It puts a plain Part (no sha1) ahead of the matching ImagePart in the collection and asserts _get_by_sha1 returns the ImagePart rather than raising. The test fails on the current code with the reported AttributeError and passes with the fix. Verified with pytest tests/test_package.py (9 passed) and ruff check on the changed files. The unrelated collection failures elsewhere in the suite on my machine come from a pyparsing version mismatch in the cxml test helper and are present on a clean checkout too.
Open Graph Description: Closes #1565. Problem Package._gather_image_parts walks the package relationships and appends the target of every image relationship to the ImageParts collection, casting it to ImagePart: def _gath...
X Description: Closes #1565. Problem Package._gather_image_parts walks the package relationships and appends the target of every image relationship to the ImageParts collection, casting it to ImagePart: def _gath...
Opengraph URL: https://github.com/python-openxml/python-docx/pull/1566
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:e347c744-361a-9bf2-3402-2e720eed9e4e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BAC2:77A5A:9623E6:B37FE1:6AFF6C29 |
| html-safe-nonce | fafe8db48d4945e6b72a40659043eb49754b89e0e82b2e8bfb06b97f76c34950 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQUMyOjc3QTVBOjk2MjNFNjpCMzdGRTE6NkFGRjZDMjkiLCJ2aXNpdG9yX2lkIjoiOTA5NDkxNTU3MTE5MDgxOTg4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7827ba6309ed35ed5901e88522cfcda97ccb6704056bf3bcf1d1ab5ae011bdcb |
| hovercard-subject-tag | pull_request:4033440680 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-openxml/python-docx/pull/1566/files |
| twitter:image | https://avatars.githubusercontent.com/u/146881507?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/146881507?s=400&v=4 |
| og:image:alt | Closes #1565. Problem Package._gather_image_parts walks the package relationships and appends the target of every image relationship to the ImageParts collection, casting it to ImagePart: def _gath... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6d580fc7098c193d7866bf290c4c2b4859a153e87379f44161f35a4495e6b730 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-openxml/python-docx git https://github.com/python-openxml/python-docx.git |
| octolytics-dimension-user_id | 3403760 |
| octolytics-dimension-user_login | python-openxml |
| octolytics-dimension-repository_id | 13592924 |
| octolytics-dimension-repository_nwo | python-openxml/python-docx |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 13592924 |
| octolytics-dimension-repository_network_root_nwo | python-openxml/python-docx |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ec5dd94a369dad6e9ce618a1705cbe3b618a2863 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width