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

Strip FEFF from start of file #92

Merged
merged 1 commit into from
Nov 6, 2020
Merged
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
2 changes: 2 additions & 0 deletions polib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,8 @@ def parse(self):
fpath = '%s ' % self.instance.fpath if self.instance.fpath else ''
for line in self.fhandle:
self.current_line += 1
if self.current_line == 1:
line = line.replace('\ufeff', '')
line = line.strip()
if line == '':
continue
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ufeff.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# test for pofile/mofile with ufeff
msgid ""
msgstr ""
"Project-Id-Version: django
"

msgid "foo"
msgstr "bar"
42 changes: 32 additions & 10 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestFunctions(unittest.TestCase):

def test_pofile_and_mofile1(self):
"""
Test bad usage of pofile/mofile.
Test bad usage of pofile/mofile.
"""
data = u('''# test for pofile/mofile with string buffer
msgid ""
Expand Down Expand Up @@ -97,6 +97,28 @@ def test_pofile_and_mofile9(self):
po = polib.pofile('tests/test_obsolete_previousmsgid.po')
self.assertTrue(isinstance(po, polib.POFile))

def test_ufeff_data_pofile(self):
"""
Test that an ufeff prefixed pofile returns a POFile instance.
"""
data = u('''\ufeff# test for pofile/mofile with ufeff
msgid ""
msgstr ""
"Project-Id-Version: django\n"

msgid "foo"
msgstr "bar"
''')
po = polib.pofile(data)
self.assertTrue(isinstance(po, polib.POFile))

def test_ufeff_pofile(self):
"""
Test that an ufeff prefixed pofile returns a POFile instance.
"""
po = polib.pofile('tests/test_ufeff.po')
self.assertTrue(isinstance(po, polib.POFile))

def test_previous_msgid_1(self):
"""
Test previous msgid multiline.
Expand Down Expand Up @@ -206,7 +228,7 @@ def test_unescaped_double_quote4(self):
exc = sys.exc_info()[1]
msg = 'Syntax error in po file (line 4): unescaped double quote found'
self.assertEqual(str(exc), msg)

def test_detect_encoding1(self):
"""
Test that given encoding is returned when file has no encoding defined.
Expand Down Expand Up @@ -238,7 +260,7 @@ def test_detect_encoding4(self):
try:
self.assertEqual(polib.detect_encoding(data), 'UTF-8')
finally:
f.close()
f.close()

def test_detect_encoding5(self):
"""
Expand Down Expand Up @@ -275,26 +297,26 @@ def test_unescape(self):
polib.unescape('\\\\t and \\\\n and \\\\r and \\\\" and \\\\\\\\'),
'\\t and \\n and \\r and \\" and \\\\'
)

def test_pofile_with_subclass(self):
"""
Test that the pofile function correctly returns an instance of the
Test that the pofile function correctly returns an instance of the
passed in class
"""
class CustomPOFile(polib.POFile):
pass

pofile = polib.pofile('tests/test_indented.po', klass=CustomPOFile)
self.assertEqual(pofile.__class__, CustomPOFile)

def test_mofile_with_subclass(self):
"""
Test that the mofile function correctly returns an instance of the
Test that the mofile function correctly returns an instance of the
passed in class
"""
class CustomMOFile(polib.MOFile):
pass

mofile = polib.mofile('tests/test_utf8.mo', klass=CustomMOFile)
self.assertEqual(mofile.__class__, CustomMOFile)

Expand Down Expand Up @@ -659,7 +681,7 @@ def test_save_as_pofile(self):
self.assertEqual(s1, s2)
finally:
os.remove(tmpfile)

def test_msgctxt(self):
#import pdb; pdb.set_trace()
mo = polib.mofile('tests/test_msgctxt.mo')
Expand Down