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

Add file_types to allow checking if variable isinstance of file #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Six provides constants that may differ between Python versions. Ones ending
:func:`py3:bytes` in Python 3.


.. data:: file_types

Possible types for files. This is :func:`py2:file` and :func:`py2:io.IOBase`
in Python 2 and just :func:`py3:io.IOBase` in Python 3.


.. data:: MAXSIZE

The maximum size of a container like :func:`py3:list` or :func:`py3:dict`.
Expand Down
4 changes: 3 additions & 1 deletion six.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import absolute_import

import functools
import io
import itertools
import operator
import sys
Expand All @@ -43,6 +44,7 @@
class_types = type,
text_type = str
binary_type = bytes
file_types = (io.IOBase, )

MAXSIZE = sys.maxsize
else:
Expand All @@ -51,6 +53,7 @@
class_types = (type, types.ClassType)
text_type = unicode
binary_type = str
file_types = (file, io.IOBase)

if sys.platform.startswith("java"):
# Jython always uses 32 bits.
Expand Down Expand Up @@ -634,7 +637,6 @@ def u(s):
byte2int = operator.itemgetter(0)
indexbytes = operator.getitem
iterbytes = iter
import io
StringIO = io.StringIO
BytesIO = io.BytesIO
_assertCountEqual = "assertCountEqual"
Expand Down
6 changes: 6 additions & 0 deletions test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def test_binary_type():
assert type(six.b("hi")) is six.binary_type


def test_file_types():
import tempfile
with tempfile.TemporaryFile() as fh:
assert isinstance(fh, six.file_types)


def test_MAXSIZE():
try:
# This shouldn't raise an overflow error.
Expand Down