Skip to content

Commit

Permalink
Avoid opening files with deprecated 'U' mode (eliben#269)
Browse files Browse the repository at this point in the history
Opening files with 'U' mode is deprecated. When running tests with
Python warnings enabled, the warnings of the following form are emitted:

  DeprecationWarning: 'U' mode is deprecated
    return open(name, 'rU')

To open files with universal newlines on both Ptyhon 2 & 3, use the io
module. It defaults to opening with universal newlines and doesn't emit
a warning.

https://docs.python.org/3/library/io.html

> When reading input from the stream, if newline is None, universal
> newlines mode is enabled.
  • Loading branch information
jdufresne authored and eliben committed Jun 26, 2018
1 parent 443474e commit 13224c1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pycparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
__all__ = ['c_lexer', 'c_parser', 'c_ast']
__version__ = '2.18'

import io
from subprocess import check_output
from .c_parser import CParser

Expand Down Expand Up @@ -81,7 +82,7 @@ def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args='',
if use_cpp:
text = preprocess_file(filename, cpp_path, cpp_args)
else:
with open(filename, 'rU') as f:
with io.open(filename) as f:
text = f.read()

if parser is None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pprint
import re
import os, sys
import io
import unittest

sys.path[0:0] = ['.', '..']
Expand Down Expand Up @@ -1772,7 +1773,7 @@ def _open_c_file(self, name):
testdir = os.path.dirname(__file__)
name = os.path.join(testdir, 'c_files', name)
assert os.path.exists(name)
return open(name, 'rU')
return io.open(name)

def test_whole_file(self):
# See how pycparser handles a whole, real C file.
Expand Down

0 comments on commit 13224c1

Please sign in to comment.