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

Bug: dataclasses.FrozenInstanceError: cannot assign to field 'glyph_id', deepcopy issue, section_title_styles, add_font #890

Closed
Samu1808 opened this issue Aug 14, 2023 · 3 comments · Fixed by #891
Labels

Comments

@Samu1808
Copy link

Samu1808 commented Aug 14, 2023

Describe the bug

I encountered a weird issue in the new version 2.7.5 with 2.7.4 everything works fine.

Error details

Traceback (most recent call last):
  File "/path/to/venv/bin/sil-run-test", line 33, in <module>
    sys.exit(load_entry_point('sil-tests', 'console_scripts', 'sil-run-test')())
  File "/path/to/sil-tests/sil_tests/main/cli.py", line 62, in main
    print_results(ResultsList, args.pdf_output)
  File "/path/to/sil-tests/sil_tests/pdf_results/pdf_results.py", line 159, in print_results
    pdf.start_section("Test Results", 0)
  File "/path/to/venv/lib/python3.9/site-packages/fpdf/fpdf.py", line 217, in wrapper
    return fn(self, *args, **kwargs)
  File "/path/to/venv/lib/python3.9/site-packages/fpdf/fpdf.py", line 4642, in start_section
    with self.offset_rendering() as pdf:
  File "/usr/lib64/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/path/to/venv/lib/python3.9/site-packages/fpdf/fpdf.py", line 4546, in offset_rendering
    recorder = FPDFRecorder(self, accept_page_break=False)
  File "/path/to/venv/lib/python3.9/site-packages/fpdf/recorder.py", line 30, in __init__
    self._initial = deepcopy(self.pdf.__dict__)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 210, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib64/python3.9/copy.py", line 210, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib64/python3.9/copy.py", line 282, in _reconstruct
    setattr(y, key, value)
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'glyph_id'

Minimal code
Here is some sample code which lead to this behaviour, note that you need to define the pdf.section_title_styles and import a font the way I did it in order to get the error. It is not a issue with this particular font, I tried others aswell but this did not effect the issue.

import fpdf 

pdf = fpdf.FPDF()
pdf.section_title_styles = [
    fpdf.TitleStyle(font_family="helvetica", font_style="B", font_size_pt=24, b_margin=10),
    fpdf.TitleStyle(font_family="helvetica", font_style="B", font_size_pt=18, b_margin=2, t_margin=4),
    fpdf.TitleStyle(font_family="helvetica", font_style="B", font_size_pt=12, b_margin=1, t_margin=4)
]
pdf.add_font("monospace", "", "SourceCodePro-Regular.ttf")
pdf.add_page()
pdf.start_section("Sample Section", 0)

pdf.output("sample_output.pdf")

Environment

  • Operating System: Fedora 34 in a docker container
    • Dockerfile:
FROM fedora:34

RUN dnf install -y gcc make python python-pip python-virtualenv git glibc-devel findutils
  • Python version: 3.9.12
  • fpdf2 version used: 2.7.5

I have reviewed the release notes for version 2.7.5 but could not identify a specific change that might be related to this issue. Any guidance or resolution would be appreciated. My workaround for now is using the 2.7.4 version.

Thank you!

@Samu1808 Samu1808 added the bug label Aug 14, 2023
@andersonhc
Copy link
Collaborator

Thanks for reporting the problem @Samu1808
This issue was introduced by #820 because I made Glyph a frozen dataclass. Consequently FPDFRecorder can not use deepcopy on it.
I will open a PR to fix this problem.

@Samu1808
Copy link
Author

Oh nice, that was fast :) thanks for your work

Lucas-C added a commit that referenced this issue Aug 15, 2023
)

Co-authored-by: Lucas Cimon <925560+Lucas-C@users.noreply.github.com>
@Lucas-C
Copy link
Member

Lucas-C commented Aug 15, 2023

This was fixed by @andersonhc in #891

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

Successfully merging a pull request may close this issue.

3 participants