Repository: python-pillow/Pillow

Issue:
UnboundLocalError in PIL/JpegImagePlugin.py
### What did you do?
Run the [Pytorch transfer Learning tutorial](https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html)
### What did you expect to happen?
The code executes correctly
### What actually happened?
an unboundlocalerror ocurred

    File "/home/.../.venv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop 
    samples = collate_fn([dataset[i] for i in batch_indices])
    File "/home/.../.venv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
    File "/home/.../.venv/lib/python3.7/site-packages/torchvision/datasets/folder.py", line 132, in __getitem__
     sample = self.loader(path)
    File "/home/.../.venv/lib/python3.7/site-packages/torchvision/datasets/folder.py", line 178, in default_loader
     return pil_loader(path)
    File "/home/.../.venv/lib/python3.7/site-packages/torchvision/datasets/folder.py", line 160, in pil_loader
     img = Image.open(f)
    File "/home/.../.venv/lib/python3.7/site-packages/PIL/Image.py", line 2690, in open
     im = _open_core(fp, filename, prefix)
    File "/home/.../.venv/lib/python3.7/site-packages/PIL/Image.py", line 2676, in _open_core
     im = factory(fp, filename)
    File "/home/.../.venv/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 783, in jpeg_factory
     im = JpegImageFile(fp, filename)
    File "/home/.../.venv/lib/python3.7/site-packages/PIL/ImageFile.py", line 103, in __init__
     self._open()
    File "/home/.../.venv/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 373, in _open                
    handler(self, i)
    File "/home/.../TransferLearning-Torch/.venv/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 139, in APP
     self.info["photoshop"] = photoshop
    UnboundLocalError: local variable 'photoshop' referenced before assignment


### What are your OS, Python and Pillow versions?

* OS: Manjaro Linux
* Python: 3.7.3
* Pillow: 6.0.0

I can consistently reproduce this by running the script on the tutorial above. The issue doesn't occur if I downgrade to Pillow 5.4.1


Discussion:
Thanks for reporting this. I've created PR #3771 to resolve it.

Resolving patch:
diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py
--- a/src/PIL/JpegImagePlugin.py
+++ b/src/PIL/JpegImagePlugin.py
@@ -136,7 +136,7 @@ def APP(self, marker):
                 offset = offset + size
                 if offset & 1:
                     offset += 1
-        self.info["photoshop"] = photoshop
+            self.info["photoshop"] = photoshop
     elif marker == 0xFFEE and s[:5] == b"Adobe":
         self.info["adobe"] = i16(s, 5)
         # extract Adobe custom properties


Test patch:
diff --git a/Tests/images/app13.jpg b/Tests/images/app13.jpg
new file mode 100644
Binary files /dev/null and b/Tests/images/app13.jpg differ
diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py
--- a/Tests/test_file_jpeg.py
+++ b/Tests/test_file_jpeg.py
@@ -620,6 +620,10 @@ def test_photoshop(self):
             'DisplayedUnitsY': 1,
         })
 
+        # This image does not contain a Photoshop header string
+        im = Image.open("Tests/images/app13.jpg")
+        self.assertNotIn("photoshop", im.info)
+
 
 @unittest.skipUnless(sys.platform.startswith('win32'), "Windows only")
 class TestFileCloseW32(PillowTestCase):