{
    "file_diffs": [
        {
            "old_file_content": "import os\nfrom glob import glob\nfrom itertools import product\n\nfrom helper import unittest, PillowTestCase\n\nfrom PIL import Image\n\n\n_TGA_DIR = os.path.join(\"Tests\", \"images\", \"tga\")\n_TGA_DIR_COMMON = os.path.join(_TGA_DIR, \"common\")\n\n\nclass TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)\n\n\nif __name__ == '__main__':\n    unittest.main()\n",
            "new_file_content": "import os\nfrom glob import glob\nfrom itertools import product\n\nfrom helper import unittest, PillowTestCase\n\nfrom PIL import Image\n\n\n_TGA_DIR = os.path.join(\"Tests\", \"images\", \"tga\")\n_TGA_DIR_COMMON = os.path.join(_TGA_DIR, \"common\")\n\n\nclass TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        # Save with custom id section greater than 255 characters\n        id_section = b\"Test content\" * 25\n        self.assert_warning(UserWarning,\n                            lambda: im.save(out, id_section=id_section))\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)\n\n\nif __name__ == '__main__':\n    unittest.main()\n",
            "header": {
                "file": {
                    "path": "Tests/test_file_tga.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "1fcd62b88",
                "new_commit_hash": "77695f2d1",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/Tests/test_file_tga.py"
            },
            "plus_file": {
                "path": "b/Tests/test_file_tga.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 124,
                            "length": 6
                        },
                        "new_range": {
                            "start": 124,
                            "length": 13
                        },
                        "section": "class TestFileTga(PillowTestCase):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        test_im = Image.open(out)",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "        # Save with custom id section greater than 255 characters",
                                "type": "added"
                            },
                            {
                                "content": "        id_section = b\"Test content\" * 25",
                                "type": "added"
                            },
                            {
                                "content": "        self.assert_warning(UserWarning,",
                                "type": "added"
                            },
                            {
                                "content": "                            lambda: im.save(out, id_section=id_section))",
                                "type": "added"
                            },
                            {
                                "content": "        test_im = Image.open(out)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "        test_file = \"Tests/images/tga_id_field.tga\"",
                                "type": "context"
                            },
                            {
                                "content": "        im = Image.open(test_file)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "Tests/test_file_tga.py",
                            "type": "class",
                            "name": "TestFileTga",
                            "content": "class TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        # Save with custom id section greater than 255 characters\n        id_section = b\"Test content\" * 25\n        self.assert_warning(UserWarning,\n                            lambda: im.save(out, id_section=id_section))\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 14,
                            "end_lineno": 203,
                            "parent": null
                        },
                        {
                            "file_name": "Tests/test_file_tga.py",
                            "type": "method",
                            "name": "TestFileTga.test_save_id_section",
                            "content": "class TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        # Save with custom id section greater than 255 characters\n        id_section = b\"Test content\" * 25\n        self.assert_warning(UserWarning,\n                            lambda: im.save(out, id_section=id_section))\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 111,
                            "end_lineno": 140,
                            "parent": {
                                "file_name": "Tests/test_file_tga.py",
                                "type": "class",
                                "name": "TestFileTga",
                                "content": "class TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        # Save with custom id section greater than 255 characters\n        id_section = b\"Test content\" * 25\n        self.assert_warning(UserWarning,\n                            lambda: im.save(out, id_section=id_section))\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 14,
                                "end_lineno": 203,
                                "parent": null
                            }
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# TGA file handling\n#\n# History:\n# 95-09-01 fl   created (reads 24-bit files only)\n# 97-01-04 fl   support more TGA versions, including compressed images\n# 98-07-04 fl   fixed orientation and alpha layer bugs\n# 98-09-11 fl   fixed orientation for runlength decoder\n#\n# Copyright (c) Secret Labs AB 1997-98.\n# Copyright (c) Fredrik Lundh 1995-97.\n#\n# See the README file for information on usage and redistribution.\n#\n\n\nfrom . import Image, ImageFile, ImagePalette\nfrom ._binary import i8, i16le as i16, o8, o16le as o16\n\n__version__ = \"0.3\"\n\n\n#\n# --------------------------------------------------------------------\n# Read RGA file\n\n\nMODES = {\n    # map imagetype/depth to rawmode\n    (1, 8):  \"P\",\n    (3, 1):  \"1\",\n    (3, 8):  \"L\",\n    (3, 16): \"LA\",\n    (2, 16): \"BGR;5\",\n    (2, 24): \"BGR\",\n    (2, 32): \"BGRA\",\n}\n\n\n##\n# Image plugin for Targa files.\n\nclass TgaImageFile(ImageFile.ImageFile):\n\n    format = \"TGA\"\n    format_description = \"Targa\"\n\n    def _open(self):\n\n        # process header\n        s = self.fp.read(18)\n\n        idlen = i8(s[0])\n\n        colormaptype = i8(s[1])\n        imagetype = i8(s[2])\n\n        depth = i8(s[16])\n\n        flags = i8(s[17])\n\n        self.size = i16(s[12:]), i16(s[14:])\n\n        # validate header fields\n        if colormaptype not in (0, 1) or\\\n           self.size[0] <= 0 or self.size[1] <= 0 or\\\n           depth not in (1, 8, 16, 24, 32):\n            raise SyntaxError(\"not a TGA file\")\n\n        # image mode\n        if imagetype in (3, 11):\n            self.mode = \"L\"\n            if depth == 1:\n                self.mode = \"1\"  # ???\n            elif depth == 16:\n                self.mode = \"LA\"\n        elif imagetype in (1, 9):\n            self.mode = \"P\"\n        elif imagetype in (2, 10):\n            self.mode = \"RGB\"\n            if depth == 32:\n                self.mode = \"RGBA\"\n        else:\n            raise SyntaxError(\"unknown TGA mode\")\n\n        # orientation\n        orientation = flags & 0x30\n        if orientation == 0x20:\n            orientation = 1\n        elif not orientation:\n            orientation = -1\n        else:\n            raise SyntaxError(\"unknown TGA orientation\")\n\n        self.info[\"orientation\"] = orientation\n\n        if imagetype & 8:\n            self.info[\"compression\"] = \"tga_rle\"\n\n        if idlen:\n            self.info[\"id_section\"] = self.fp.read(idlen)\n\n        if colormaptype:\n            # read palette\n            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])\n            if mapdepth == 16:\n                self.palette = ImagePalette.raw(\n                    \"BGR;16\", b\"\\0\"*2*start + self.fp.read(2*size))\n            elif mapdepth == 24:\n                self.palette = ImagePalette.raw(\n                    \"BGR\", b\"\\0\"*3*start + self.fp.read(3*size))\n            elif mapdepth == 32:\n                self.palette = ImagePalette.raw(\n                    \"BGRA\", b\"\\0\"*4*start + self.fp.read(4*size))\n\n        # setup tile descriptor\n        try:\n            rawmode = MODES[(imagetype & 7, depth)]\n            if imagetype & 8:\n                # compressed\n                self.tile = [(\"tga_rle\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, orientation, depth))]\n            else:\n                self.tile = [(\"raw\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, 0, orientation))]\n        except KeyError:\n            pass  # cannot decode\n\n#\n# --------------------------------------------------------------------\n# Write TGA file\n\n\nSAVE = {\n    \"1\": (\"1\", 1, 0, 3),\n    \"L\": (\"L\", 8, 0, 3),\n    \"LA\": (\"LA\", 16, 0, 3),\n    \"P\": (\"P\", 8, 1, 1),\n    \"RGB\": (\"BGR\", 24, 0, 2),\n    \"RGBA\": (\"BGRA\", 32, 0, 2),\n}\n\n\ndef _save(im, fp, filename):\n\n    try:\n        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]\n    except KeyError:\n        raise IOError(\"cannot write mode %s as TGA\" % im.mode)\n\n    if \"rle\" in im.encoderinfo:\n        rle = im.encoderinfo[\"rle\"]\n    else:\n        compression = im.encoderinfo.get(\"compression\",\n                                         im.info.get(\"compression\"))\n        rle = compression == \"tga_rle\"\n    if rle:\n        imagetype += 8\n\n    id_section = im.encoderinfo.get(\"id_section\",\n                                    im.info.get(\"id_section\", \"\"))\n    idlen = len(id_section)\n\n    if colormaptype:\n        colormapfirst, colormaplength, colormapentry = 0, 256, 24\n    else:\n        colormapfirst, colormaplength, colormapentry = 0, 0, 0\n\n    if im.mode in (\"LA\", \"RGBA\"):\n        flags = 8\n    else:\n        flags = 0\n\n    orientation = im.encoderinfo.get(\"orientation\",\n                                     im.info.get(\"orientation\", -1))\n    if orientation > 0:\n        flags = flags | 0x20\n\n    fp.write(o8(idlen) +\n             o8(colormaptype) +\n             o8(imagetype) +\n             o16(colormapfirst) +\n             o16(colormaplength) +\n             o8(colormapentry) +\n             o16(0) +\n             o16(0) +\n             o16(im.size[0]) +\n             o16(im.size[1]) +\n             o8(bits) +\n             o8(flags))\n\n    if id_section:\n        fp.write(id_section)\n\n    if colormaptype:\n        fp.write(im.im.getpalette(\"RGB\", \"BGR\"))\n\n    if rle:\n        ImageFile._save(\n            im,\n            fp,\n            [(\"tga_rle\", (0, 0) + im.size, 0, (rawmode, orientation))])\n    else:\n        ImageFile._save(\n            im, fp, [(\"raw\", (0, 0) + im.size, 0, (rawmode, 0, orientation))])\n\n    # write targa version 2 footer\n    fp.write(b\"\\000\" * 8 + b\"TRUEVISION-XFILE.\" + b\"\\000\")\n\n#\n# --------------------------------------------------------------------\n# Registry\n\n\nImage.register_open(TgaImageFile.format, TgaImageFile)\nImage.register_save(TgaImageFile.format, _save)\n\nImage.register_extension(TgaImageFile.format, \".tga\")\n",
            "new_file_content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# TGA file handling\n#\n# History:\n# 95-09-01 fl   created (reads 24-bit files only)\n# 97-01-04 fl   support more TGA versions, including compressed images\n# 98-07-04 fl   fixed orientation and alpha layer bugs\n# 98-09-11 fl   fixed orientation for runlength decoder\n#\n# Copyright (c) Secret Labs AB 1997-98.\n# Copyright (c) Fredrik Lundh 1995-97.\n#\n# See the README file for information on usage and redistribution.\n#\n\n\nfrom . import Image, ImageFile, ImagePalette\nfrom ._binary import i8, i16le as i16, o8, o16le as o16\n\nimport warnings\n\n__version__ = \"0.3\"\n\n\n#\n# --------------------------------------------------------------------\n# Read RGA file\n\n\nMODES = {\n    # map imagetype/depth to rawmode\n    (1, 8):  \"P\",\n    (3, 1):  \"1\",\n    (3, 8):  \"L\",\n    (3, 16): \"LA\",\n    (2, 16): \"BGR;5\",\n    (2, 24): \"BGR\",\n    (2, 32): \"BGRA\",\n}\n\n\n##\n# Image plugin for Targa files.\n\nclass TgaImageFile(ImageFile.ImageFile):\n\n    format = \"TGA\"\n    format_description = \"Targa\"\n\n    def _open(self):\n\n        # process header\n        s = self.fp.read(18)\n\n        idlen = i8(s[0])\n\n        colormaptype = i8(s[1])\n        imagetype = i8(s[2])\n\n        depth = i8(s[16])\n\n        flags = i8(s[17])\n\n        self.size = i16(s[12:]), i16(s[14:])\n\n        # validate header fields\n        if colormaptype not in (0, 1) or\\\n           self.size[0] <= 0 or self.size[1] <= 0 or\\\n           depth not in (1, 8, 16, 24, 32):\n            raise SyntaxError(\"not a TGA file\")\n\n        # image mode\n        if imagetype in (3, 11):\n            self.mode = \"L\"\n            if depth == 1:\n                self.mode = \"1\"  # ???\n            elif depth == 16:\n                self.mode = \"LA\"\n        elif imagetype in (1, 9):\n            self.mode = \"P\"\n        elif imagetype in (2, 10):\n            self.mode = \"RGB\"\n            if depth == 32:\n                self.mode = \"RGBA\"\n        else:\n            raise SyntaxError(\"unknown TGA mode\")\n\n        # orientation\n        orientation = flags & 0x30\n        if orientation == 0x20:\n            orientation = 1\n        elif not orientation:\n            orientation = -1\n        else:\n            raise SyntaxError(\"unknown TGA orientation\")\n\n        self.info[\"orientation\"] = orientation\n\n        if imagetype & 8:\n            self.info[\"compression\"] = \"tga_rle\"\n\n        if idlen:\n            self.info[\"id_section\"] = self.fp.read(idlen)\n\n        if colormaptype:\n            # read palette\n            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])\n            if mapdepth == 16:\n                self.palette = ImagePalette.raw(\n                    \"BGR;16\", b\"\\0\"*2*start + self.fp.read(2*size))\n            elif mapdepth == 24:\n                self.palette = ImagePalette.raw(\n                    \"BGR\", b\"\\0\"*3*start + self.fp.read(3*size))\n            elif mapdepth == 32:\n                self.palette = ImagePalette.raw(\n                    \"BGRA\", b\"\\0\"*4*start + self.fp.read(4*size))\n\n        # setup tile descriptor\n        try:\n            rawmode = MODES[(imagetype & 7, depth)]\n            if imagetype & 8:\n                # compressed\n                self.tile = [(\"tga_rle\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, orientation, depth))]\n            else:\n                self.tile = [(\"raw\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, 0, orientation))]\n        except KeyError:\n            pass  # cannot decode\n\n#\n# --------------------------------------------------------------------\n# Write TGA file\n\n\nSAVE = {\n    \"1\": (\"1\", 1, 0, 3),\n    \"L\": (\"L\", 8, 0, 3),\n    \"LA\": (\"LA\", 16, 0, 3),\n    \"P\": (\"P\", 8, 1, 1),\n    \"RGB\": (\"BGR\", 24, 0, 2),\n    \"RGBA\": (\"BGRA\", 32, 0, 2),\n}\n\n\ndef _save(im, fp, filename):\n\n    try:\n        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]\n    except KeyError:\n        raise IOError(\"cannot write mode %s as TGA\" % im.mode)\n\n    if \"rle\" in im.encoderinfo:\n        rle = im.encoderinfo[\"rle\"]\n    else:\n        compression = im.encoderinfo.get(\"compression\",\n                                         im.info.get(\"compression\"))\n        rle = compression == \"tga_rle\"\n    if rle:\n        imagetype += 8\n\n    id_section = im.encoderinfo.get(\"id_section\",\n                                    im.info.get(\"id_section\", \"\"))\n    idlen = len(id_section)\n    if idlen > 255:\n        idlen = 255\n        id_section = id_section[:255]\n        warnings.warn(\"id_section has been trimmed to 255 characters\")\n\n    if colormaptype:\n        colormapfirst, colormaplength, colormapentry = 0, 256, 24\n    else:\n        colormapfirst, colormaplength, colormapentry = 0, 0, 0\n\n    if im.mode in (\"LA\", \"RGBA\"):\n        flags = 8\n    else:\n        flags = 0\n\n    orientation = im.encoderinfo.get(\"orientation\",\n                                     im.info.get(\"orientation\", -1))\n    if orientation > 0:\n        flags = flags | 0x20\n\n    fp.write(o8(idlen) +\n             o8(colormaptype) +\n             o8(imagetype) +\n             o16(colormapfirst) +\n             o16(colormaplength) +\n             o8(colormapentry) +\n             o16(0) +\n             o16(0) +\n             o16(im.size[0]) +\n             o16(im.size[1]) +\n             o8(bits) +\n             o8(flags))\n\n    if id_section:\n        fp.write(id_section)\n\n    if colormaptype:\n        fp.write(im.im.getpalette(\"RGB\", \"BGR\"))\n\n    if rle:\n        ImageFile._save(\n            im,\n            fp,\n            [(\"tga_rle\", (0, 0) + im.size, 0, (rawmode, orientation))])\n    else:\n        ImageFile._save(\n            im, fp, [(\"raw\", (0, 0) + im.size, 0, (rawmode, 0, orientation))])\n\n    # write targa version 2 footer\n    fp.write(b\"\\000\" * 8 + b\"TRUEVISION-XFILE.\" + b\"\\000\")\n\n#\n# --------------------------------------------------------------------\n# Registry\n\n\nImage.register_open(TgaImageFile.format, TgaImageFile)\nImage.register_save(TgaImageFile.format, _save)\n\nImage.register_extension(TgaImageFile.format, \".tga\")\n",
            "header": {
                "file": {
                    "path": "src/PIL/TgaImagePlugin.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "c60cb3ef1",
                "new_commit_hash": "c622f1f5e",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/src/PIL/TgaImagePlugin.py"
            },
            "plus_file": {
                "path": "b/src/PIL/TgaImagePlugin.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 20,
                            "length": 6
                        },
                        "new_range": {
                            "start": 20,
                            "length": 8
                        },
                        "section": ""
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "from . import Image, ImageFile, ImagePalette",
                                "type": "context"
                            },
                            {
                                "content": "from ._binary import i8, i16le as i16, o8, o16le as o16",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "import warnings",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "__version__ = \"0.3\"",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [],
                    "added_entities": [
                        {
                            "file_name": "src/PIL/TgaImagePlugin.py",
                            "type": "import",
                            "name": "warnings",
                            "content": "import warnings",
                            "ast_type_str": "Import",
                            "start_lineno": 23,
                            "end_lineno": 23,
                            "parent": null
                        }
                    ],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 163,
                            "length": 6
                        },
                        "new_range": {
                            "start": 165,
                            "length": 10
                        },
                        "section": "def _save(im, fp, filename):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    id_section = im.encoderinfo.get(\"id_section\",",
                                "type": "context"
                            },
                            {
                                "content": "                                    im.info.get(\"id_section\", \"\"))",
                                "type": "context"
                            },
                            {
                                "content": "    idlen = len(id_section)",
                                "type": "context"
                            },
                            {
                                "content": "    if idlen > 255:",
                                "type": "added"
                            },
                            {
                                "content": "        idlen = 255",
                                "type": "added"
                            },
                            {
                                "content": "        id_section = id_section[:255]",
                                "type": "added"
                            },
                            {
                                "content": "        warnings.warn(\"id_section has been trimmed to 255 characters\")",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    if colormaptype:",
                                "type": "context"
                            },
                            {
                                "content": "        colormapfirst, colormaplength, colormapentry = 0, 256, 24",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/TgaImagePlugin.py",
                            "type": "function",
                            "name": "_save",
                            "content": "def _save(im, fp, filename):\n\n    try:\n        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]\n    except KeyError:\n        raise IOError(\"cannot write mode %s as TGA\" % im.mode)\n\n    if \"rle\" in im.encoderinfo:\n        rle = im.encoderinfo[\"rle\"]\n    else:\n        compression = im.encoderinfo.get(\"compression\",\n                                         im.info.get(\"compression\"))\n        rle = compression == \"tga_rle\"\n    if rle:\n        imagetype += 8\n\n    id_section = im.encoderinfo.get(\"id_section\",\n                                    im.info.get(\"id_section\", \"\"))\n    idlen = len(id_section)\n    if idlen > 255:\n        idlen = 255\n        id_section = id_section[:255]\n        warnings.warn(\"id_section has been trimmed to 255 characters\")\n\n    if colormaptype:\n        colormapfirst, colormaplength, colormapentry = 0, 256, 24\n    else:\n        colormapfirst, colormaplength, colormapentry = 0, 0, 0\n\n    if im.mode in (\"LA\", \"RGBA\"):\n        flags = 8\n    else:\n        flags = 0\n\n    orientation = im.encoderinfo.get(\"orientation\",\n                                     im.info.get(\"orientation\", -1))\n    if orientation > 0:\n        flags = flags | 0x20\n\n    fp.write(o8(idlen) +\n             o8(colormaptype) +\n             o8(imagetype) +\n             o16(colormapfirst) +\n             o16(colormaplength) +\n             o8(colormapentry) +\n             o16(0) +\n             o16(0) +\n             o16(im.size[0]) +\n             o16(im.size[1]) +\n             o8(bits) +\n             o8(flags))\n\n    if id_section:\n        fp.write(id_section)\n\n    if colormaptype:\n        fp.write(im.im.getpalette(\"RGB\", \"BGR\"))\n\n    if rle:\n        ImageFile._save(\n            im,\n            fp,\n            [(\"tga_rle\", (0, 0) + im.size, 0, (rawmode, orientation))])\n    else:\n        ImageFile._save(\n            im, fp, [(\"raw\", (0, 0) + im.size, 0, (rawmode, 0, orientation))])\n\n    # write targa version 2 footer\n    fp.write(b\"\\000\" * 8 + b\"TRUEVISION-XFILE.\" + b\"\\000\")",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 149,
                            "end_lineno": 217,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "325ca3cede708a82ae76f294c9f8d1222d26a758^",
    "new_commit_hash": "325ca3cede708a82ae76f294c9f8d1222d26a758",
    "commit_message": "Trim id_section if it is greater than 255 characters",
    "commit_date": "2018-09-08T08:26:32+10:00",
    "metadata": {}
}
{
    "repo_name": "pillow",
    "new_commit_hash": "325ca3cede708a82ae76f294c9f8d1222d26a758",
    "test_file_codes": [
        "import os\nfrom glob import glob\nfrom itertools import product\n\nfrom helper import unittest, PillowTestCase\n\nfrom PIL import Image\n\n\n_TGA_DIR = os.path.join(\"Tests\", \"images\", \"tga\")\n_TGA_DIR_COMMON = os.path.join(_TGA_DIR, \"common\")\n\n\nclass TestFileTga(PillowTestCase):\n\n    _MODES = (\"L\", \"LA\", \"P\", \"RGB\", \"RGBA\")\n    _ORIGINS = (\"tl\", \"bl\")\n\n    _ORIGIN_TO_ORIENTATION = {\n        \"tl\": 1,\n        \"bl\": -1\n    }\n\n    def test_sanity(self):\n        for mode in self._MODES:\n            png_paths = glob(\n                os.path.join(\n                    _TGA_DIR_COMMON, \"*x*_{}.png\".format(mode.lower())))\n\n            for png_path in png_paths:\n                reference_im = Image.open(png_path)\n                self.assertEqual(reference_im.mode, mode)\n\n                path_no_ext = os.path.splitext(png_path)[0]\n                for origin, rle in product(self._ORIGINS, (True, False)):\n                    tga_path = \"{}_{}_{}.tga\".format(\n                        path_no_ext, origin, \"rle\" if rle else \"raw\")\n\n                    original_im = Image.open(tga_path)\n                    if rle:\n                        self.assertEqual(\n                            original_im.info[\"compression\"], \"tga_rle\")\n                    self.assertEqual(\n                        original_im.info[\"orientation\"],\n                        self._ORIGIN_TO_ORIENTATION[origin])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            original_im.getpalette(),\n                            reference_im.getpalette())\n\n                    self.assert_image_equal(original_im, reference_im)\n\n                    # Generate a new test name every time so the\n                    # test will not fail with permission error\n                    # on Windows.\n                    out = self.tempfile(\"temp.tga\")\n\n                    original_im.save(out, rle=rle)\n                    saved_im = Image.open(out)\n                    if rle:\n                        self.assertEqual(\n                            saved_im.info[\"compression\"],\n                            original_im.info[\"compression\"])\n                    self.assertEqual(\n                        saved_im.info[\"orientation\"],\n                        original_im.info[\"orientation\"])\n                    if mode == \"P\":\n                        self.assertEqual(\n                            saved_im.getpalette(),\n                            original_im.getpalette())\n\n                    self.assert_image_equal(saved_im, original_im)\n\n    def test_id_field(self):\n        # tga file with id field\n        test_file = \"Tests/images/tga_id_field.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (100, 100))\n\n    def test_id_field_rle(self):\n        # tga file with id field\n        test_file = \"Tests/images/rgb32rle.tga\"\n\n        # Act\n        im = Image.open(test_file)\n\n        # Assert\n        self.assertEqual(im.size, (199, 199))\n\n    def test_save(self):\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n        self.assertEqual(test_im.info[\"id_section\"], im.info[\"id_section\"])\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (100, 100))\n\n    def test_save_id_section(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Check there is no id section\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n        # Save with custom id section\n        im.save(out, id_section=b\"Test content\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], b\"Test content\")\n\n        # Save with custom id section greater than 255 characters\n        id_section = b\"Test content\" * 25\n        self.assert_warning(UserWarning,\n                            lambda: im.save(out, id_section=id_section))\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"id_section\"], id_section[:255])\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n\n        # Save with no id section\n        im.save(out, id_section=\"\")\n        test_im = Image.open(out)\n        self.assertNotIn(\"id_section\", test_im.info)\n\n    def test_save_orientation(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"orientation\"], -1)\n\n        out = self.tempfile(\"temp.tga\")\n\n        im.save(out, orientation=1)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"orientation\"], 1)\n\n    def test_save_rle(self):\n        test_file = \"Tests/images/rgb32rle.tga\"\n        im = Image.open(test_file)\n        self.assertEqual(im.info[\"compression\"], \"tga_rle\")\n\n        out = self.tempfile(\"temp.tga\")\n\n        # Save\n        im.save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n        # Save without compression\n        im.save(out, compression=None)\n        test_im = Image.open(out)\n        self.assertNotIn(\"compression\", test_im.info)\n\n        # RGBA save\n        im.convert(\"RGBA\").save(out)\n        test_im = Image.open(out)\n        self.assertEqual(test_im.size, (199, 199))\n\n        test_file = \"Tests/images/tga_id_field.tga\"\n        im = Image.open(test_file)\n        self.assertNotIn(\"compression\", im.info)\n\n        # Save with compression\n        im.save(out, compression=\"tga_rle\")\n        test_im = Image.open(out)\n        self.assertEqual(test_im.info[\"compression\"], \"tga_rle\")\n\n    def test_save_l_transparency(self):\n        # There are 559 transparent pixels in la.tga.\n        num_transparent = 559\n\n        in_file = \"Tests/images/la.tga\"\n        im = Image.open(in_file)\n        self.assertEqual(im.mode, \"LA\")\n        self.assertEqual(\n            im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        out = self.tempfile(\"temp.tga\")\n        im.save(out)\n\n        test_im = Image.open(out)\n        self.assertEqual(test_im.mode, \"LA\")\n        self.assertEqual(\n            test_im.getchannel(\"A\").getcolors()[0][0], num_transparent)\n\n        self.assert_image_equal(im, test_im)\n\n\nif __name__ == '__main__':\n    unittest.main()\n",
        "#!/usr/bin/env python\nimport unittest\nimport sys\nimport time\nimport traceback\nimport platform\nimport os\n\n\nclass PytestLikeResult(unittest.TestResult):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.start_time = None\n        # Store (test, outcome, info) for each test:\n        #   outcome ∈ { \"passed\", \"failed\", \"error\", \"skipped\" }\n        #   info can hold the exception or skip reason, etc.\n        self.results = []\n\n    def startTestRun(self):\n        \"\"\"Called once before all tests.\"\"\"\n        self.start_time = time.time()\n        # Print a header similar to pytest\n        print(\"=\" * 29 + \" test session starts \" + \"=\" * 29)\n        # You can customize the platform/versions/paths as you like:\n        print(\n            f\"platform {platform.system().lower()} -- Python {platform.python_version()}\"\n        )\n        print(f\"rootdir: {os.path.abspath(os.curdir)}\")\n\n    def stopTestRun(self):\n        \"\"\"Called once after all tests are done.\"\"\"\n        elapsed = time.time() - self.start_time\n        print()  # blank line after final dot\n\n        # Summarize\n        ntests = len(self.results)\n        print(f\"collected {ntests} items\")\n\n        # Show details of failures/errors\n        failures = [r for r in self.results if r[1] == \"failed\"]\n        errors = [r for r in self.results if r[1] == \"error\"]\n\n        if failures:\n            print(\"\\n\" + \"=\" * 35 + \" FAILURES \" + \"=\" * 35)\n            for test, outcome, err_info in failures:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n        if errors:\n            print(\"\\n\" + \"=\" * 35 + \" ERRORS \" + \"=\" * 37)\n            for test, outcome, err_info in errors:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n\n        # Optional short summary lines for passes/fails/skips\n        print(\"\\n==================== short test summary info ====================\")\n        for test, outcome, info in self.results:\n            if outcome in (\"passed\", \"failed\", \"error\", \"skipped\"):\n                print(f\"{outcome.upper()} {test}\")\n\n        # Final one-line summary: “1 failed, 1 errors, 10 passed in 2.04s”\n        passed_count = sum(1 for r in self.results if r[1] == \"passed\")\n        failed_count = len(failures)\n        error_count = len(errors)\n        skipped_count = sum(1 for r in self.results if r[1] == \"skipped\")\n\n        summary_bits = []\n        if failed_count:\n            summary_bits.append(f\"{failed_count} failed\")\n        if error_count:\n            summary_bits.append(f\"{error_count} error{'s' if error_count != 1 else ''}\")\n        if passed_count:\n            summary_bits.append(f\"{passed_count} passed\")\n        if skipped_count:\n            summary_bits.append(f\"{skipped_count} skipped\")\n\n        summary_str = \", \".join(summary_bits) if summary_bits else \"no tests run\"\n        print(\n            f\"=================== {summary_str} in {elapsed:.2f}s ===================\"\n        )\n\n    def startTest(self, test):\n        \"\"\"Called right before each test method.\"\"\"\n        super().startTest(test)\n        # Print a dot or some indicator\n        print(\".\", end=\"\", flush=True)\n\n    def addSuccess(self, test):\n        super().addSuccess(test)\n        self.results.append((self._test_id(test), \"passed\", None))\n\n    def addFailure(self, test, err):\n        super().addFailure(test, err)\n        self.results.append((self._test_id(test), \"failed\", err))\n\n    def addError(self, test, err):\n        super().addError(test, err)\n        self.results.append((self._test_id(test), \"error\", err))\n\n    def addSkip(self, test, reason):\n        super().addSkip(test, reason)\n        self.results.append((self._test_id(test), \"skipped\", reason))\n\n    def _print_traceback(self, err_info):\n        \"\"\"Helper to print the traceback like pytest does.\"\"\"\n        if isinstance(err_info, tuple) and len(err_info) == 3:\n            exc_type, exc_value, tb = err_info\n            traceback.print_exception(exc_type, exc_value, tb, file=sys.stdout)\n        else:\n            print(str(err_info))\n\n    def _test_id(self, test):\n        try:\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}::{test._testMethodName}\"\n        except AttributeError:\n            # Fallback for _ErrorHolder objects\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}\"\n\n\nclass PytestLikeRunner(unittest.TextTestRunner):\n    \"\"\"A custom TextTestRunner that uses the PytestLikeResult.\"\"\"\n\n    resultclass = PytestLikeResult\n\n    def run(self, test):\n        result = self._makeResult()\n        # Overriding run to ensure we call startTestRun/stopTestRun\n        result.startTestRun()\n        test(result)\n        result.stopTestRun()\n        return result\n\n\ndef main():\n    loader = unittest.TestLoader()\n    suite = loader.discover(\"r2e_tests\")  # discover tests in r2e_tests/\n    runner = PytestLikeRunner(verbosity=0)\n    result = runner.run(suite)\n    # Exit with code 0 if all tests passed/skipped, 1 if there were fails/errors\n    sys.exit(not result.wasSuccessful())\n\n\nif __name__ == \"__main__\":\n    main()\n"
    ],
    "test_file_names": [
        "test_1.py",
        "unittest_custom_runner.py"
    ],
    "setup_res_code": 0,
    "setup_res_stdout": "Starting Pillow installation attempts...\nVerifying Pillow installation...\n✅ Pillow installation successful!\nSuccessfully installed Pillow\n",
    "setup_res_stderr": "Using CPython 3.9.21\nCreating virtual environment at: .venv\nActivate with: source .venv/bin/activate\nResolved 12 packages in 10ms\nInstalled 12 packages in 224ms\n + coverage==7.6.10\n + exceptiongroup==1.2.2\n + iniconfig==2.0.0\n + packaging==24.2\n + pluggy==1.5.0\n + pyqt5==5.15.11\n + pyqt5-qt5==5.15.16\n + pyqt5-sip==12.16.1\n + pytest==8.3.4\n + pytest-cov==6.0.0\n + setuptools==75.8.0\n + tomli==2.2.1\nResolved 1 package in 884ms\nPrepared 1 package in 15.46s\nInstalled 1 package in 1ms\n + pillow==5.3.0.dev0 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_325ca3cede708a82ae76f294c9f8d1222d26a758)\n",
    "new_commit_res_code": 0,
    "new_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_325ca3cede708a82ae76f294c9f8d1222d26a758\n........\ncollected 8 items\n\n==================== short test summary info ====================\nPASSED test_1::TestFileTga::test_id_field\nPASSED test_1::TestFileTga::test_id_field_rle\nPASSED test_1::TestFileTga::test_sanity\nPASSED test_1::TestFileTga::test_save\nPASSED test_1::TestFileTga::test_save_id_section\nPASSED test_1::TestFileTga::test_save_l_transparency\nPASSED test_1::TestFileTga::test_save_orientation\nPASSED test_1::TestFileTga::test_save_rle\n=================== 8 passed in 0.18s ===================\n",
    "new_commit_res_stderr": "",
    "old_commit_res_code": 1,
    "old_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_325ca3cede708a82ae76f294c9f8d1222d26a758\n......=== orphaned temp file: /tmp/tempb_wn5jz0.tga\n.=== orphaned temp file: /tmp/tempin95tkav.tga\n.=== orphaned temp file: /tmp/tempmz2uepqk.tga\n\ncollected 8 items\n\n=================================== FAILURES ===================================\n__________ TestFileTga.test_save_id_section __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_325ca3cede708a82ae76f294c9f8d1222d26a758/r2e_tests/test_1.py\", line 129, in test_save_id_section\n    self.assert_warning(UserWarning,\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_325ca3cede708a82ae76f294c9f8d1222d26a758/r2e_tests/helper.py\", line 180, in assert_warning\n    self.assertGreaterEqual(len(w), 1)\nAssertionError: 0 not greater than or equal to 1\n\n==================== short test summary info ====================\nPASSED test_1::TestFileTga::test_id_field\nPASSED test_1::TestFileTga::test_id_field_rle\nPASSED test_1::TestFileTga::test_sanity\nPASSED test_1::TestFileTga::test_save\nFAILED test_1::TestFileTga::test_save_id_section\nPASSED test_1::TestFileTga::test_save_l_transparency\nPASSED test_1::TestFileTga::test_save_orientation\nPASSED test_1::TestFileTga::test_save_rle\n=================== 1 failed, 7 passed in 0.09s ===================\n",
    "old_commit_res_stderr": ""
}
Tests/test_file_tga.py src/PIL/TgaImagePlugin.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** Saving TGA images with excessively long `id_section` lacks trimming and warnings

**Description:**
When attempting to save a TGA image with an `id_section` longer than 255 characters, the `id_section` is neither trimmed to the required length nor is a warning issued to notify the user. This oversight can lead to compatibility issues and unexpected behavior when processing TGA files that involve lengthy `id_section` data.

**Example Code:**
```python
from PIL import Image

# Create a sample image
im = Image.new('RGB', (100, 100))
out = 'temp.tga'
id_section = b'Test content' * 25  # Generates a bytes object exceeding 255 characters
im.save(out, id_section=id_section)
test_im = Image.open(out)
print(len(test_im.info.get('id_section', b'')))  # Expected output: 255
```

**Expected Behavior:**
The `id_section` should be automatically trimmed to a maximum of 255 characters to comply with TGA specifications. Additionally, a warning should be emitted to inform the user that the `id_section` has been truncated.

**Actual Behavior:**
The `id_section` remains longer than 255 characters without any warning, potentially causing issues with file compatibility and data integrity.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### src/PIL/TgaImagePlugin.py
#
# The Python Imaging Library.
# $Id$
#
# TGA file handling
#
# History:
# 95-09-01 fl   created (reads 24-bit files only)
# 97-01-04 fl   support more TGA versions, including compressed images
# 98-07-04 fl   fixed orientation and alpha layer bugs
# 98-09-11 fl   fixed orientation for runlength decoder
#
# Copyright (c) Secret Labs AB 1997-98.
# Copyright (c) Fredrik Lundh 1995-97.
#
# See the README file for information on usage and redistribution.
#


from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16le as i16, o8, o16le as o16

__version__ = "0.3"


#
# --------------------------------------------------------------------
# Read RGA file


MODES = {
    # map imagetype/depth to rawmode
    (1, 8):  "P",
    (3, 1):  "1",
    (3, 8):  "L",
    (3, 16): "LA",
    (2, 16): "BGR;5",
    (2, 24): "BGR",
    (2, 32): "BGRA",
}


##
# Image plugin for Targa files.

class TgaImageFile(ImageFile.ImageFile):

    format = "TGA"
    format_description = "Targa"

    def _open(self):

        # process header
        s = self.fp.read(18)

        idlen = i8(s[0])

        colormaptype = i8(s[1])
        imagetype = i8(s[2])

        depth = i8(s[16])

        flags = i8(s[17])

        self.size = i16(s[12:]), i16(s[14:])

        # validate header fields
        if colormaptype not in (0, 1) or\
           self.size[0] <= 0 or self.size[1] <= 0 or\
           depth not in (1, 8, 16, 24, 32):
            raise SyntaxError("not a TGA file")

        # image mode
        if imagetype in (3, 11):
            self.mode = "L"
            if depth == 1:
                self.mode = "1"  # ???
            elif depth == 16:
                self.mode = "LA"
        elif imagetype in (1, 9):
            self.mode = "P"
        elif imagetype in (2, 10):
            self.mode = "RGB"
            if depth == 32:
                self.mode = "RGBA"
        else:
            raise SyntaxError("unknown TGA mode")

        # orientation
        orientation = flags & 0x30
        if orientation == 0x20:
            orientation = 1
        elif not orientation:
            orientation = -1
        else:
            raise SyntaxError("unknown TGA orientation")

        self.info["orientation"] = orientation

        if imagetype & 8:
            self.info["compression"] = "tga_rle"

        if idlen:
            self.info["id_section"] = self.fp.read(idlen)

        if colormaptype:
            # read palette
            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
            if mapdepth == 16:
                self.palette = ImagePalette.raw(
                    "BGR;16", b"\0"*2*start + self.fp.read(2*size))
            elif mapdepth == 24:
                self.palette = ImagePalette.raw(
                    "BGR", b"\0"*3*start + self.fp.read(3*size))
            elif mapdepth == 32:
                self.palette = ImagePalette.raw(
                    "BGRA", b"\0"*4*start + self.fp.read(4*size))

        # setup tile descriptor
        try:
            rawmode = MODES[(imagetype & 7, depth)]
            if imagetype & 8:
                # compressed
                self.tile = [("tga_rle", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, orientation, depth))]
            else:
                self.tile = [("raw", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, 0, orientation))]
        except KeyError:
            pass  # cannot decode

#
# --------------------------------------------------------------------
# Write TGA file


SAVE = {
    "1": ("1", 1, 0, 3),
    "L": ("L", 8, 0, 3),
    "LA": ("LA", 16, 0, 3),
    "P": ("P", 8, 1, 1),
    "RGB": ("BGR", 24, 0, 2),
    "RGBA": ("BGRA", 32, 0, 2),
}


def _save(im, fp, filename):

    try:
        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
    except KeyError:
        raise IOError("cannot write mode %s as TGA" % im.mode)

    if "rle" in im.encoderinfo:
        rle = im.encoderinfo["rle"]
    else:
        compression = im.encoderinfo.get("compression",
                                         im.info.get("compression"))
        rle = compression == "tga_rle"
    if rle:
        imagetype += 8

    id_section = im.encoderinfo.get("id_section",
                                    im.info.get("id_section", ""))
    idlen = len(id_section)

    if colormaptype:
        colormapfirst, colormaplength, colormapentry = 0, 256, 24
    else:
        colormapfirst, colormaplength, colormapentry = 0, 0, 0

    if im.mode in ("LA", "RGBA"):
        flags = 8
    else:
        flags = 0

    orientation = im.encoderinfo.get("orientation",
                                     im.info.get("orientation", -1))
    if orientation > 0:
        flags = flags | 0x20

    fp.write(o8(idlen) +
             o8(colormaptype) +
             o8(imagetype) +
             o16(colormapfirst) +
             o16(colormaplength) +
             o8(colormapentry) +
             o16(0) +
             o16(0) +
             o16(im.size[0]) +
             o16(im.size[1]) +
             o8(bits) +
             o8(flags))

    if id_section:
        fp.write(id_section)

    if colormaptype:
        fp.write(im.im.getpalette("RGB", "BGR"))

    if rle:
        ImageFile._save(
            im,
            fp,
            [("tga_rle", (0, 0) + im.size, 0, (rawmode, orientation))])
    else:
        ImageFile._save(
            im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))])

    # write targa version 2 footer
    fp.write(b"\000" * 8 + b"TRUEVISION-XFILE." + b"\000")

#
# --------------------------------------------------------------------
# Registry


Image.register_open(TgaImageFile.format, TgaImageFile)
Image.register_save(TgaImageFile.format, _save)

Image.register_extension(TgaImageFile.format, ".tga")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
[ISSUE]
**Title:** Saving TGA images with excessively long `id_section` lacks trimming and warnings

**Description:**
When attempting to save a TGA image with an `id_section` longer than 255 characters, the `id_section` is neither trimmed to the required length nor is a warning issued to notify the user. This oversight can lead to compatibility issues and unexpected behavior when processing TGA files that involve lengthy `id_section` data.

**Example Code:**
```python
from PIL import Image

# Create a sample image
im = Image.new('RGB', (100, 100))
out = 'temp.tga'
id_section = b'Test content' * 25  # Generates a bytes object exceeding 255 characters
im.save(out, id_section=id_section)
test_im = Image.open(out)
print(len(test_im.info.get('id_section', b'')))  # Expected output: 255
```

**Expected Behavior:**
The `id_section` should be automatically trimmed to a maximum of 255 characters to comply with TGA specifications. Additionally, a warning should be emitted to inform the user that the `id_section` has been truncated.

**Actual Behavior:**
The `id_section` remains longer than 255 characters without any warning, potentially causing issues with file compatibility and data integrity.

[/ISSUE]
{
    "TestFileTga.test_id_field": "PASSED",
    "TestFileTga.test_id_field_rle": "PASSED",
    "TestFileTga.test_sanity": "PASSED",
    "TestFileTga.test_save": "PASSED",
    "TestFileTga.test_save_id_section": "PASSED",
    "TestFileTga.test_save_l_transparency": "PASSED",
    "TestFileTga.test_save_orientation": "PASSED",
    "TestFileTga.test_save_rle": "PASSED"
}
diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py
index c60cb3ef1..c622f1f5e 100644
--- a/src/PIL/TgaImagePlugin.py
+++ b/src/PIL/TgaImagePlugin.py
@@ -20,6 +20,8 @@
 from . import Image, ImageFile, ImagePalette
 from ._binary import i8, i16le as i16, o8, o16le as o16
 
+import warnings
+
 __version__ = "0.3"
 
 
@@ -163,6 +165,10 @@ def _save(im, fp, filename):
     id_section = im.encoderinfo.get("id_section",
                                     im.info.get("id_section", ""))
     idlen = len(id_section)
+    if idlen > 255:
+        idlen = 255
+        id_section = id_section[:255]
+        warnings.warn("id_section has been trimmed to 255 characters")
 
     if colormaptype:
         colormapfirst, colormaplength, colormapentry = 0, 256, 24
 

{"src/PIL/TgaImagePlugin.py": "#\n# The Python Imaging Library.\n# $Id$\n#\n# TGA file handling\n#\n# History:\n# 95-09-01 fl   created (reads 24-bit files only)\n# 97-01-04 fl   support more TGA versions, including compressed images\n# 98-07-04 fl   fixed orientation and alpha layer bugs\n# 98-09-11 fl   fixed orientation for runlength decoder\n#\n# Copyright (c) Secret Labs AB 1997-98.\n# Copyright (c) Fredrik Lundh 1995-97.\n#\n# See the README file for information on usage and redistribution.\n#\n\n\nfrom . import Image, ImageFile, ImagePalette\nfrom ._binary import i8, i16le as i16, o8, o16le as o16\n\n__version__ = \"0.3\"\n\n\n#\n# --------------------------------------------------------------------\n# Read RGA file\n\n\nMODES = {\n    # map imagetype/depth to rawmode\n    (1, 8):  \"P\",\n    (3, 1):  \"1\",\n    (3, 8):  \"L\",\n    (3, 16): \"LA\",\n    (2, 16): \"BGR;5\",\n    (2, 24): \"BGR\",\n    (2, 32): \"BGRA\",\n}\n\n\n##\n# Image plugin for Targa files.\n\nclass TgaImageFile(ImageFile.ImageFile):\n\n    format = \"TGA\"\n    format_description = \"Targa\"\n\n    def _open(self):\n\n        # process header\n        s = self.fp.read(18)\n\n        idlen = i8(s[0])\n\n        colormaptype = i8(s[1])\n        imagetype = i8(s[2])\n\n        depth = i8(s[16])\n\n        flags = i8(s[17])\n\n        self.size = i16(s[12:]), i16(s[14:])\n\n        # validate header fields\n        if colormaptype not in (0, 1) or\\\n           self.size[0] <= 0 or self.size[1] <= 0 or\\\n           depth not in (1, 8, 16, 24, 32):\n            raise SyntaxError(\"not a TGA file\")\n\n        # image mode\n        if imagetype in (3, 11):\n            self.mode = \"L\"\n            if depth == 1:\n                self.mode = \"1\"  # ???\n            elif depth == 16:\n                self.mode = \"LA\"\n        elif imagetype in (1, 9):\n            self.mode = \"P\"\n        elif imagetype in (2, 10):\n            self.mode = \"RGB\"\n            if depth == 32:\n                self.mode = \"RGBA\"\n        else:\n            raise SyntaxError(\"unknown TGA mode\")\n\n        # orientation\n        orientation = flags & 0x30\n        if orientation == 0x20:\n            orientation = 1\n        elif not orientation:\n            orientation = -1\n        else:\n            raise SyntaxError(\"unknown TGA orientation\")\n\n        self.info[\"orientation\"] = orientation\n\n        if imagetype & 8:\n            self.info[\"compression\"] = \"tga_rle\"\n\n        if idlen:\n            self.info[\"id_section\"] = self.fp.read(idlen)\n\n        if colormaptype:\n            # read palette\n            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])\n            if mapdepth == 16:\n                self.palette = ImagePalette.raw(\n                    \"BGR;16\", b\"\\0\"*2*start + self.fp.read(2*size))\n            elif mapdepth == 24:\n                self.palette = ImagePalette.raw(\n                    \"BGR\", b\"\\0\"*3*start + self.fp.read(3*size))\n            elif mapdepth == 32:\n                self.palette = ImagePalette.raw(\n                    \"BGRA\", b\"\\0\"*4*start + self.fp.read(4*size))\n\n        # setup tile descriptor\n        try:\n            rawmode = MODES[(imagetype & 7, depth)]\n            if imagetype & 8:\n                # compressed\n                self.tile = [(\"tga_rle\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, orientation, depth))]\n            else:\n                self.tile = [(\"raw\", (0, 0)+self.size,\n                              self.fp.tell(), (rawmode, 0, orientation))]\n        except KeyError:\n            pass  # cannot decode\n\n#\n# --------------------------------------------------------------------\n# Write TGA file\n\n\nSAVE = {\n    \"1\": (\"1\", 1, 0, 3),\n    \"L\": (\"L\", 8, 0, 3),\n    \"LA\": (\"LA\", 16, 0, 3),\n    \"P\": (\"P\", 8, 1, 1),\n    \"RGB\": (\"BGR\", 24, 0, 2),\n    \"RGBA\": (\"BGRA\", 32, 0, 2),\n}\n\n\ndef _save(im, fp, filename):\n\n    try:\n        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]\n    except KeyError:\n        raise IOError(\"cannot write mode %s as TGA\" % im.mode)\n\n    if \"rle\" in im.encoderinfo:\n        rle = im.encoderinfo[\"rle\"]\n    else:\n        compression = im.encoderinfo.get(\"compression\",\n                                         im.info.get(\"compression\"))\n        rle = compression == \"tga_rle\"\n    if rle:\n        imagetype += 8\n\n    id_section = im.encoderinfo.get(\"id_section\",\n                                    im.info.get(\"id_section\", \"\"))\n    idlen = len(id_section)\n\n    if colormaptype:\n        colormapfirst, colormaplength, colormapentry = 0, 256, 24\n    else:\n        colormapfirst, colormaplength, colormapentry = 0, 0, 0\n\n    if im.mode in (\"LA\", \"RGBA\"):\n        flags = 8\n    else:\n        flags = 0\n\n    orientation = im.encoderinfo.get(\"orientation\",\n                                     im.info.get(\"orientation\", -1))\n    if orientation > 0:\n        flags = flags | 0x20\n\n    fp.write(o8(idlen) +\n             o8(colormaptype) +\n             o8(imagetype) +\n             o16(colormapfirst) +\n             o16(colormaplength) +\n             o8(colormapentry) +\n             o16(0) +\n             o16(0) +\n             o16(im.size[0]) +\n             o16(im.size[1]) +\n             o8(bits) +\n             o8(flags))\n\n    if id_section:\n        fp.write(id_section)\n\n    if colormaptype:\n        fp.write(im.im.getpalette(\"RGB\", \"BGR\"))\n\n    if rle:\n        ImageFile._save(\n            im,\n            fp,\n            [(\"tga_rle\", (0, 0) + im.size, 0, (rawmode, orientation))])\n    else:\n        ImageFile._save(\n            im, fp, [(\"raw\", (0, 0) + im.size, 0, (rawmode, 0, orientation))])\n\n    # write targa version 2 footer\n    fp.write(b\"\\000\" * 8 + b\"TRUEVISION-XFILE.\" + b\"\\000\")\n\n#\n# --------------------------------------------------------------------\n# Registry\n\n\nImage.register_open(TgaImageFile.format, TgaImageFile)\nImage.register_save(TgaImageFile.format, _save)\n\nImage.register_extension(TgaImageFile.format, \".tga\")\n"}
A user will ask you to solve a task. You should first draft your thinking process (inner monologue). Then, generate the solution.

Your response format must follow the template below:
<think>
Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct solution.
</think>
<solution>
Final solution presented to the user.
</solution>
system
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** Saving TGA images with excessively long `id_section` lacks trimming and warnings

**Description:**
When attempting to save a TGA image with an `id_section` longer than 255 characters, the `id_section` is neither trimmed to the required length nor is a warning issued to notify the user. This oversight can lead to compatibility issues and unexpected behavior when processing TGA files that involve lengthy `id_section` data.

**Example Code:**
```python
from PIL import Image

# Create a sample image
im = Image.new('RGB', (100, 100))
out = 'temp.tga'
id_section = b'Test content' * 25  # Generates a bytes object exceeding 255 characters
im.save(out, id_section=id_section)
test_im = Image.open(out)
print(len(test_im.info.get('id_section', b'')))  # Expected output: 255
```

**Expected Behavior:**
The `id_section` should be automatically trimmed to a maximum of 255 characters to comply with TGA specifications. Additionally, a warning should be emitted to inform the user that the `id_section` has been truncated.

**Actual Behavior:**
The `id_section` remains longer than 255 characters without any warning, potentially causing issues with file compatibility and data integrity.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### src/PIL/TgaImagePlugin.py
#
# The Python Imaging Library.
# $Id$
#
# TGA file handling
#
# History:
# 95-09-01 fl   created (reads 24-bit files only)
# 97-01-04 fl   support more TGA versions, including compressed images
# 98-07-04 fl   fixed orientation and alpha layer bugs
# 98-09-11 fl   fixed orientation for runlength decoder
#
# Copyright (c) Secret Labs AB 1997-98.
# Copyright (c) Fredrik Lundh 1995-97.
#
# See the README file for information on usage and redistribution.
#


from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16le as i16, o8, o16le as o16

__version__ = "0.3"


#
# --------------------------------------------------------------------
# Read RGA file


MODES = {
    # map imagetype/depth to rawmode
    (1, 8):  "P",
    (3, 1):  "1",
    (3, 8):  "L",
    (3, 16): "LA",
    (2, 16): "BGR;5",
    (2, 24): "BGR",
    (2, 32): "BGRA",
}


##
# Image plugin for Targa files.

class TgaImageFile(ImageFile.ImageFile):

    format = "TGA"
    format_description = "Targa"

    def _open(self):

        # process header
        s = self.fp.read(18)

        idlen = i8(s[0])

        colormaptype = i8(s[1])
        imagetype = i8(s[2])

        depth = i8(s[16])

        flags = i8(s[17])

        self.size = i16(s[12:]), i16(s[14:])

        # validate header fields
        if colormaptype not in (0, 1) or\
           self.size[0] <= 0 or self.size[1] <= 0 or\
           depth not in (1, 8, 16, 24, 32):
            raise SyntaxError("not a TGA file")

        # image mode
        if imagetype in (3, 11):
            self.mode = "L"
            if depth == 1:
                self.mode = "1"  # ???
            elif depth == 16:
                self.mode = "LA"
        elif imagetype in (1, 9):
            self.mode = "P"
        elif imagetype in (2, 10):
            self.mode = "RGB"
            if depth == 32:
                self.mode = "RGBA"
        else:
            raise SyntaxError("unknown TGA mode")

        # orientation
        orientation = flags & 0x30
        if orientation == 0x20:
            orientation = 1
        elif not orientation:
            orientation = -1
        else:
            raise SyntaxError("unknown TGA orientation")

        self.info["orientation"] = orientation

        if imagetype & 8:
            self.info["compression"] = "tga_rle"

        if idlen:
            self.info["id_section"] = self.fp.read(idlen)

        if colormaptype:
            # read palette
            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
            if mapdepth == 16:
                self.palette = ImagePalette.raw(
                    "BGR;16", b"\0"*2*start + self.fp.read(2*size))
            elif mapdepth == 24:
                self.palette = ImagePalette.raw(
                    "BGR", b"\0"*3*start + self.fp.read(3*size))
            elif mapdepth == 32:
                self.palette = ImagePalette.raw(
                    "BGRA", b"\0"*4*start + self.fp.read(4*size))

        # setup tile descriptor
        try:
            rawmode = MODES[(imagetype & 7, depth)]
            if imagetype & 8:
                # compressed
                self.tile = [("tga_rle", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, orientation, depth))]
            else:
                self.tile = [("raw", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, 0, orientation))]
        except KeyError:
            pass  # cannot decode

#
# --------------------------------------------------------------------
# Write TGA file


SAVE = {
    "1": ("1", 1, 0, 3),
    "L": ("L", 8, 0, 3),
    "LA": ("LA", 16, 0, 3),
    "P": ("P", 8, 1, 1),
    "RGB": ("BGR", 24, 0, 2),
    "RGBA": ("BGRA", 32, 0, 2),
}


def _save(im, fp, filename):

    try:
        rawmode, bits, colormaptype, imagetype = SAVE[im.mode]
    except KeyError:
        raise IOError("cannot write mode %s as TGA" % im.mode)

    if "rle" in im.encoderinfo:
        rle = im.encoderinfo["rle"]
    else:
        compression = im.encoderinfo.get("compression",
                                         im.info.get("compression"))
        rle = compression == "tga_rle"
    if rle:
        imagetype += 8

    id_section = im.encoderinfo.get("id_section",
                                    im.info.get("id_section", ""))
    idlen = len(id_section)

    if colormaptype:
        colormapfirst, colormaplength, colormapentry = 0, 256, 24
    else:
        colormapfirst, colormaplength, colormapentry = 0, 0, 0

    if im.mode in ("LA", "RGBA"):
        flags = 8
    else:
        flags = 0

    orientation = im.encoderinfo.get("orientation",
                                     im.info.get("orientation", -1))
    if orientation > 0:
        flags = flags | 0x20

    fp.write(o8(idlen) +
             o8(colormaptype) +
             o8(imagetype) +
             o16(colormapfirst) +
             o16(colormaplength) +
             o8(colormapentry) +
             o16(0) +
             o16(0) +
             o16(im.size[0]) +
             o16(im.size[1]) +
             o8(bits) +
             o8(flags))

    if id_section:
        fp.write(id_section)

    if colormaptype:
        fp.write(im.im.getpalette("RGB", "BGR"))

    if rle:
        ImageFile._save(
            im,
            fp,
            [("tga_rle", (0, 0) + im.size, 0, (rawmode, orientation))])
    else:
        ImageFile._save(
            im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))])

    # write targa version 2 footer
    fp.write(b"\000" * 8 + b"TRUEVISION-XFILE." + b"\000")

#
# --------------------------------------------------------------------
# Registry


Image.register_open(TgaImageFile.format, TgaImageFile)
Image.register_save(TgaImageFile.format, _save)

Image.register_extension(TgaImageFile.format, ".tga")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
user