summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Broytman <phd@phdru.name>2016-08-05 19:58:48 +0300
committerOleg Broytman <phd@phdru.name>2016-08-06 01:30:45 +0300
commit44e66710fb2222b0aa60c21a834c46831166f44c (patch)
treef81aef8580c8b97aed80622c53d8e9293d32f7c6
parente2eca17927462f7893dbe5eb440eefb1e7419a59 (diff)
downloadsqlparse-44e66710fb2222b0aa60c21a834c46831166f44c.tar.gz
Fix a bug: recognize file (Python 2) as a stream
-rw-r--r--sqlparse/lexer.py8
-rw-r--r--tests/test_regressions.py7
2 files changed, 14 insertions, 1 deletions
diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py
index 3cf2be4..06318c6 100644
--- a/sqlparse/lexer.py
+++ b/sqlparse/lexer.py
@@ -20,6 +20,12 @@ from sqlparse.compat import StringIO, string_types, u
from sqlparse.utils import consume
+try:
+ file_types = (file, StringIO, TextIOBase)
+except NameError: # Python 3
+ file_types = (StringIO, TextIOBase)
+
+
class Lexer(object):
"""Lexer
Empty class. Leaving for backwards-compatibility
@@ -41,7 +47,7 @@ class Lexer(object):
"""
if isinstance(text, string_types):
text = u(text, encoding)
- elif isinstance(text, (StringIO, TextIOBase)):
+ elif isinstance(text, file_types):
text = u(text.read(), encoding)
iterable = enumerate(text)
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 5d04131..b0d66e1 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -235,6 +235,13 @@ def test_null_with_as():
assert formatted == tformatted
+def test_issue190_open_file(filepath):
+ path = filepath('stream.sql')
+ stream = open(path)
+ p = sqlparse.parse(stream)[0]
+ assert p.get_type() == 'INSERT'
+
+
def test_issue193_splitting_function():
sql = """ CREATE FUNCTION a(x VARCHAR(20)) RETURNS VARCHAR(20)
BEGIN