py-pdf / pypdf

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files
https://pypdf.readthedocs.io/en/latest/
Other
7.73k stars 1.36k forks source link

`Ressources` deprecation does not work for some python versions #2697

Closed ztravis closed 4 weeks ago

ztravis commented 1 month ago

The Ressources object under pypdf.constants was deprecated and replaced with a correctly-spelled variant. The deprecated version uses @classmethod @property static-esque properties so that a deprecation message can be logged, but I think this is only supported in a limited range of python versions (3.9 and 3.10) according to https://docs.python.org/3/library/functions.html#classmethod. For example, on python3.8 these end up being type method, which causes unexpected behavior when they're used (e.g. as object keys), e.g.:

x = DictionaryObject()
x[NameObject(Ressources.EXT_G_STATE)] = DictionaryObject()
x.write_to_stream(stream) # Writes b"<<\n<bound#20method#20?#20of#20<class#20'pypdf.constants.Ressources'>> <<\n>>\n>>"
stefan6419846 commented 1 month ago

Thanks for the report. Do you want to submit a corresponding PR to fix this?

It there are no other easier ways to achieve this, I would recommend to copy and re-use the classproperty implementation from Django with appropriate attribution (the license is compatible): https://docs.djangoproject.com/en/5.0/_modules/django/utils/functional/#classproperty

ztravis commented 4 weeks ago

Sorry I missed your comment! Thank you for the suggestion and fix. Next time I will be happy to open a PR.