File: test_ipython.py

package info (click to toggle)
pikepdf 9.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,760 kB
  • sloc: python: 9,926; cpp: 4,224; makefile: 260; sh: 77
file content (89 lines) | stat: -rw-r--r-- 2,506 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# SPDX-FileCopyrightText: 2022 James R. Barlow
# SPDX-License-Identifier: CC0-1.0

"""
Test IPython/Jupyter display hooks
"""

from __future__ import annotations

import subprocess
from io import BytesIO

import pytest
from conftest import fails_if_no_mutool

import pikepdf


@pytest.fixture
def pal(resources):
    return pikepdf.open(resources / 'pal-1bit-trivial.pdf')


def test_display_raw_page(pal):
    raw_page0 = pal.pages[0]
    mimebundle = raw_page0._repr_mimebundle_(
        include=['application/pdf'], exclude=['application/malware']
    )
    assert 'application/pdf' in mimebundle


def test_display_rich_page(pal):
    page0 = pal.pages[0]
    mimebundle = page0._repr_mimebundle_(
        include=['application/pdf'], exclude=['application/malware']
    )
    assert 'application/pdf' in mimebundle


def test_draw_page(pal, monkeypatch):
    # Test page drawing error handling independent of whether mudraw is installed

    page0 = pal.pages[0]

    def raise_filenotfound(prog_args, *args, **kwargs):
        raise FileNotFoundError(prog_args[0])

    monkeypatch.setattr(pikepdf._methods, 'run', raise_filenotfound)
    mimebundle = page0._repr_mimebundle_(
        include=['image/png'], exclude=['application/pdf']
    )
    assert (
        'image/png' not in mimebundle
    ), "Generated image/png when mudraw() was rigged to fail"

    def return_simple_svg(prog_args, *args, **kwargs):
        bio = BytesIO(b'<svg xmlns="http://www.w3.org/2000/svg"></svg>')
        return subprocess.CompletedProcess(prog_args, 0, stdout=bio.read(), stderr=b'')

    monkeypatch.setattr(pikepdf._methods, 'run', return_simple_svg)
    mimebundle = page0._repr_mimebundle_(
        include=['image/svg+xml'], exclude=['application/pdf']
    )
    assert (
        'image/svg+xml' in mimebundle
    ), "Did not generate image/svg+xml when mudraw() was rigged to succeed"


def test_display_image(pal):
    im0 = pal.pages[0].Resources.XObject['/Im0']
    pim = pikepdf.PdfImage(im0)
    result = pim._repr_png_()
    assert result[1:4] == b'PNG'


@fails_if_no_mutool
def test_display_pdf(pal):
    mimebundle = pal._repr_mimebundle_(
        include=['application/pdf'], exclude=['text/css']
    )
    assert 'application/pdf' in mimebundle and mimebundle['application/pdf'].startswith(
        b'%PDF'
    )


def test_object_key_completion(pal):
    page0 = pal.pages[0]
    assert '/Type' in page0._ipython_key_completions_()
    assert page0.MediaBox._ipython_key_completions_() is None