Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 22, 2021

Conversation

glacials
Copy link
Contributor

@glacials glacials commented Jan 8, 2021

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/[email protected]/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/[email protected]/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/[email protected]/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: [email protected]
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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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
Copy link
Owner

matiasb commented Jan 22, 2021

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