Skip to content

Fix an error when the first change is a binary - #75

Merged
matiasb merged 1 commit into
matiasb:masterfrom
glacials:master
Jan 22, 2021
Merged

matiasb merged 1 commit into
matiasb:masterfrom
glacials:master

Conversation

@glacials

@glacials glacials commented Jan 8, 2021

Copy link
Copy Markdown
Contributor

This fixes a bug where, in a diff where the first change is a binary file, unidiff errors with

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 538, in from_filename
    instance = cls(f)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 421, in __init__
    self._parse(data, encoding=encoding, metadata_only=metadata_only)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 520, in _parse
    patch_info.append(line)
AttributeError: 'NoneType' object has no attribute 'append'

Reproduction steps:

# Requires `go` to generate example binaries, but you can replace them
# with any two different binaries from any source

$ echo "package main\nfunc main(){}" > binary_a.go
$ go build binary_a.go
$ echo "package main\nimport \"fmt\"\nfunc main(){fmt.Println(\"HI\")}" > binary_b.go
$ go build binary_b.go
$ diff --unified binary_a binary_b > changes.diff
$ cat changes.diff
Binary files binary_a and binary_b differ
$ pip3 show unidiff
Name: unidiff
Version: 0.6.0
Summary: Unified diff parsing/metadata extraction library.
Home-page: http://github.com/matiasb/python-unidiff
Author: Matias Bordese
Author-email: mbordese@gmail.com
License: MIT
Location: /home/linuxbrew/.linuxbrew/lib/python3.9/site-packages
Requires: 
Required-by: 
$ python3
>>> from unidiff import PatchSet
>>> PatchSet.from_filename('./changes.diff')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 538, in from_filename
    instance = cls(f)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 421, in __init__
    self._parse(data, encoding=encoding, metadata_only=metadata_only)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 520, in _parse
    patch_info.append(line)
AttributeError: 'NoneType' object has no attribute 'append'
>>>

This fixes a bug where, in a diff where the first change is a binary
file, unidiff errors with
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 538, in from_filename
    instance = cls(f)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 421, in __init__
    self._parse(data, encoding=encoding, metadata_only=metadata_only)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 520, in _parse
    patch_info.append(line)
AttributeError: 'NoneType' object has no attribute 'append'
```

Reproduction steps:
```
\# Requires `go` to generate example binaries, but you can replace them
\# with any two different binaries from any source

$ echo "package main\nfunc main(){}" > binary_a.go
$ go build binary_a.go
$ echo "package main\nimport \"fmt\"\nfunc main(){fmt.Println(\"HI\")}" > binary_b.go
$ go build binary_b.go

$ diff binary_a binary_b > changes.diff
$ cat changes.diff
Binary files binary_a and binary_b differ

$ python3
>>> from unidiff import PatchSet
>>> PatchSet.from_filename('./changes.diff')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 538, in from_filename
    instance = cls(f)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 421, in __init__
    self._parse(data, encoding=encoding, metadata_only=metadata_only)
  File "/home/linuxbrew/.linuxbrew/Cellar/python@3.9/3.9.1_5/lib/python3.9/site-packages/unidiff/patch.py", line 520, in _parse
    patch_info.append(line)
AttributeError: 'NoneType' object has no attribute 'append'
>>>
```
@matiasb

matiasb commented Jan 22, 2021

Copy link
Copy Markdown
Owner

Nice catch! Thanks for the fix 👍

@matiasb
matiasb merged commit 9a473c8 into matiasb:master Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants