Skip to content

Instantly share code, notes, and snippets.

@remram44
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save remram44/76ff36bd1daf2226efe0 to your computer and use it in GitHub Desktop.

Select an option

Save remram44/76ff36bd1daf2226efe0 to your computer and use it in GitHub Desktop.
import contextlib
import os
import sys
import tarfile
import traceback
VERSION = sys.version_info[0]
PY3 = VERSION == 3
if os.name == 'nt':
def uni(s):
return [s.encode('iso-8859-1'), s.encode('utf-8')]
else:
def uni(s):
return [s.encode('utf-8')]
@contextlib.contextmanager
def test(s):
try:
yield
except:
sys.stderr.write("FAIL: %s\n" % s)
traceback.print_exc(file=sys.stderr)
else:
sys.stderr.write("SUCC: %s\n" % s)
sys.stderr.write("Running tests on Python %d, %s\n" % (VERSION, os.name))
sys.stderr.write("Unpacking tests\n")
########################################
# Tests tarfile.open()
tars = []
tarnames = dict()
for n in [u'/tmp/t\xE9st.tar.gz'] + uni(u'/tmp/t\xE9st.tar.gz'):
with test("Opening tar file from %s" % type(n).__name__):
tu = tarfile.open(n, 'r:gz')
tars.append(tu)
tarnames[id(tu)] = "%s tar" % type(n).__name__
########################################
# Tests TarFile.extractfile()
for tar in tars:
for n in [u'some\xE9file.txt'] + uni(u'some\xE9file.txt'):
with test("Opening %s name from %s" % (
type(n).__name__, tarnames[id(tar)])):
f = tar.extractfile(n)
c = f.read()
assert isinstance(c, bytes)
assert c == b'content'
########################################
# Tests TarFile.extract()
for tar in tars:
for n in [u'some\xE9file.txt'] + uni(u'some\xE9file.txt'):
for d in [u'/tmp/tmp\xE9'] + uni(u'/tmp/tmp\xE9'):
with test("Extracting %s name to %s dir from %s" % (
type(n).__name__, type(d).__name__, tarnames[id(tar)])):
tar.extract(n, path=d)
########################################
# Tests TarFile.extractall()
for tar in tars:
for d in [u'/tmp/tmp\xE9'] + uni(u'/tmp/tmp\xE9'):
with test("Extracting all to %s dir from %s" % (
type(d).__name__, tarnames[id(tar)])):
tar.extractall(path=d)
########################################
# Tests TarFile.getmembers()
for tar in tars:
m = tar.getmembers()[0]
sys.stderr.write("getmembers() in %s gives %s names\n" % (
tarnames[id(tar)], type(m.name).__name__))
for tar in tars:
tar.close()
sys.stderr.write("Packing tests\n")
########################################
# Tests tarfile.open()
tars = []
tarnames = dict()
with test("Opening tar file from unicode"):
tu = tarfile.open(u'/tmp/t\xE9stwu.tar.gz', 'w:gz')
tars.append(tu)
tarnames[id(tu)] = "unicode tar"
with test("Opening tar file from bytestring"):
tb = tarfile.open(uni(u'/tmp/t\xE9stwb.tar.gz')[0], 'w:gz')
tars.append(tb)
tarnames[id(tb)] = "bytestring tar"
########################################
# Tests TarFile.add()
for tar in tars:
for n in [u'/tmp/other\xE9file.txt'] + uni(u'/tmp/other\xE9file.txt'):
for a in [u'DATA/other\xE9file.txt'] + uni(u'DATA/other\xE9file.txt'):
with test("Adding %s to %s in %s" % (
type(n).__name__, type(a).__name__, tarnames[id(tar)])):
tar.add(n, a)
for tar in tars:
tar.close()
"""
Running tests on Python 2, posix
Unpacking tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytes
FAIL: Opening unicode name from unicode tar
SUCC: Opening bytes name from unicode tar
FAIL: Opening unicode name from bytes tar
SUCC: Opening bytes name from bytes tar
FAIL: Extracting unicode name to unicode dir from unicode tar
FAIL: Extracting unicode name to bytes dir from unicode tar
FAIL: Extracting bytes name to unicode dir from unicode tar
SUCC: Extracting bytes name to bytes dir from unicode tar
FAIL: Extracting unicode name to unicode dir from bytes tar
FAIL: Extracting unicode name to bytes dir from bytes tar
FAIL: Extracting bytes name to unicode dir from bytes tar
SUCC: Extracting bytes name to bytes dir from bytes tar
FAIL: Extracting all to unicode dir from unicode tar
SUCC: Extracting all to bytes dir from unicode tar
FAIL: Extracting all to unicode dir from bytes tar
SUCC: Extracting all to bytes dir from bytes tar
getmembers() in unicode tar gives bytes names
getmembers() in bytes tar gives bytes names
Packing tests
FAIL: Opening tar file from unicode
SUCC: Opening tar file from bytestring
SUCC: Adding unicode to unicode in bytestring tar
SUCC: Adding unicode to bytes in bytestring tar
SUCC: Adding bytes to unicode in bytestring tar
SUCC: Adding bytes to bytes in bytestring tar
Running tests on Python 3, posix
Unpacking tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytes
SUCC: Opening unicode name from unicode tar
FAIL: Opening bytes name from unicode tar
SUCC: Opening unicode name from bytes tar
FAIL: Opening bytes name from bytes tar
SUCC: Extracting unicode name to unicode dir from unicode tar
FAIL: Extracting unicode name to bytes dir from unicode tar
FAIL: Extracting bytes name to unicode dir from unicode tar
FAIL: Extracting bytes name to bytes dir from unicode tar
SUCC: Extracting unicode name to unicode dir from bytes tar
FAIL: Extracting unicode name to bytes dir from bytes tar
FAIL: Extracting bytes name to unicode dir from bytes tar
FAIL: Extracting bytes name to bytes dir from bytes tar
SUCC: Extracting all to unicode dir from unicode tar
FAIL: Extracting all to bytes dir from unicode tar
SUCC: Extracting all to unicode dir from bytes tar
FAIL: Extracting all to bytes dir from bytes tar
getmembers() in unicode tar gives unicode names
getmembers() in bytes tar gives unicode names
Packing tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytestring
SUCC: Adding unicode to unicode in unicode tar
FAIL: Adding unicode to bytes in unicode tar
SUCC: Adding bytes to unicode in unicode tar
FAIL: Adding bytes to bytes in unicode tar
SUCC: Adding unicode to unicode in bytestring tar
FAIL: Adding unicode to bytes in bytestring tar
SUCC: Adding bytes to unicode in bytestring tar
FAIL: Adding bytes to bytes in bytestring tar
Running tests on Python 2, nt
Unpacking tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytes # ISO
FAIL: Opening unicode name from unicode tar
SUCC: Opening bytes name from unicode tar # UTF-8
FAIL: Opening unicode name from bytes tar
SUCC: Opening bytes name from bytes tar # UTF-8
FAIL: Extracting unicode name to unicode dir from unicode tar
FAIL: Extracting unicode name to bytes dir from unicode tar
FAIL: Extracting bytes name to unicode dir from unicode tar
FAIL: Extracting bytes name to bytes dir from unicode tar
FAIL: Extracting unicode name to unicode dir from bytes tar
FAIL: Extracting unicode name to bytes dir from bytes tar
FAIL: Extracting bytes name to unicode dir from bytes tar
SUCC: Extracting bytes name to bytes dir from bytes tar
FAIL: Extracting all to unicode dir from unicode tar
SUCC: Extracting all to bytes dir from unicode tar
FAIL: Extracting all to unicode dir from bytes tar
SUCC: Extracting all to bytes dir from bytes tar
getmembers() in unicode tar gives bytes names
getmembers() in bytes tar gives bytes names
Packing tests
FAIL: Opening tar file from unicode
SUCC: Opening tar file from bytestring
SUCC: Adding unicode to unicode in bytestring tar
SUCC: Adding unicode to bytes in bytestring tar # BOTH!
SUCC: Adding bytes to unicode in bytestring tar # ISO
SUCC: Adding bytes to bytes in bytestring tar # ISO, BOTH!
Running tests on Python 3, nt
Unpacking tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytes # ISO
SUCC: Opening unicode name from unicode tar
FAIL: Opening bytes name from unicode tar
SUCC: Opening unicode name from bytes tar
FAIL: Opening bytes name from bytes tar
SUCC: Extracting unicode name to unicode dir from unicode tar
FAIL: Extracting unicode name to bytes dir from unicode tar
FAIL: Extracting bytes name to unicode dir from unicode tar
FAIL: Extracting bytes name to bytes dir from unicode tar
SUCC: Extracting unicode name to unicode dir from bytes tar
FAIL: Extracting unicode name to bytes dir from bytes tar
FAIL: Extracting bytes name to unicode dir from bytes tar
FAIL: Extracting bytes name to bytes dir from bytes tar
SUCC: Extracting all to unicode dir from unicode tar
FAIL: Extracting all to bytes dir from unicode tar
SUCC: Extracting all to unicode dir from bytes tar
FAIL: Extracting all to bytes dir from bytes tar
getmembers() in unicode tar gives unicode names
getmembers() in bytes tar gives unicode names
Packing tests
SUCC: Opening tar file from unicode
SUCC: Opening tar file from bytestring
SUCC: Adding unicode to unicode in unicode tar
FAIL: Adding unicode to bytes in unicode tar
SUCC: Adding bytes to unicode in unicode tar
FAIL: Adding bytes to bytes in unicode tar
FAIL: Adding bytes to unicode in unicode tar
FAIL: Adding bytes to bytes in unicode tar
SUCC: Adding unicode to unicode in bytestring tar
FAIL: Adding unicode to bytes in bytestring tar
SUCC: Adding bytes to unicode in bytestring tar
FAIL: Adding bytes to bytes in bytestring tar
FAIL: Adding bytes to unicode in bytestring tar
FAIL: Adding bytes to bytes in bytestring tar
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment