<commit_before>"""
    weasyprint.tests.conftest
    -------------------------

    Configuration for WeasyPrint tests.

    This module adds a PNG export based on GhostScript. As GhostScript is
    released under AGPL, the whole testing suite is released under AGPL.

"""

import io
import shutil
from subprocess import run

import pytest
from PIL import Image

from .. import HTML
from ..document import Document


def document_write_png(self, target=None, resolution=96, antialiasing=1):
    # TODO: don’t crash if GhostScript can’t be found
    # TODO: fix that for Windows
    command = [
        'gs', '-q', '-sstdout=%stderr', '-dNOPAUSE', '-dSAFER',
        f'-dTextAlphaBits={antialiasing}',
        f'-dGraphicsAlphaBits={antialiasing}', '-sDEVICE=png16m',
        f'-r{resolution}', '-sOutputFile=-', '-']
    command = run(command, input=self.write_pdf(), capture_output=True)
    pngs = command.stdout
    magic_number = b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a'

    # TODO: use a different way to find PNG files in stream
    if pngs.count(magic_number) == 1:
        if target is None:
            return pngs
        png = io.BytesIO(pngs)
    else:
        images = []
        for i, png in enumerate(pngs[8:].split(magic_number)):
            images.append(Image.open(io.BytesIO(magic_number + png)))

        width = max(image.width for image in images)
        height = sum(image.height for image in images)<commit_msg>Don’t use capture_output, unsupported by Python 3.6<commit_after>"""
    weasyprint.tests.conftest
    -------------------------

    Configuration for WeasyPrint tests.

    This module adds a PNG export based on GhostScript. As GhostScript is
    released under AGPL, the whole testing suite is released under AGPL.

"""

import io
import shutil
from subprocess import PIPE, run

import pytest
from PIL import Image

from .. import HTML
from ..document import Document


def document_write_png(self, target=None, resolution=96, antialiasing=1):
    # TODO: don’t crash if GhostScript can’t be found
    # TODO: fix that for Windows
    command = [
        'gs', '-q', '-sstdout=%stderr', '-dNOPAUSE', '-dSAFER',
        f'-dTextAlphaBits={antialiasing}',
        f'-dGraphicsAlphaBits={antialiasing}', '-sDEVICE=png16m',
        f'-r{resolution}', '-sOutputFile=-', '-']
    command = run(command, input=self.write_pdf(), stdout=PIPE)
    pngs = command.stdout
    magic_number = b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a'

    # TODO: use a different way to find PNG files in stream
    if pngs.count(magic_number) == 1:
        if target is None:
            return pngs
        png = io.BytesIO(pngs)
    else:
        images = []
        for i, png in enumerate(pngs[8:].split(magic_number)):
            images.append(Image.open(io.BytesIO(magic_number + png)))

        width = max(image.width for image in images)
        height = sum(image.height for image in images)
{'fraction_non_alphanumeric': 0.12878787878787878, 'fraction_numerical': 0.013085399449035813, 'mean_word_length': 2.767833981841764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2618825', 'n_tokens_mistral': 953, 'n_tokens_neox': 905, 'n_words': 266}