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

refactor: replace IOError with OSError #162

Open
wants to merge 1 commit into
base: master
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
24 changes: 12 additions & 12 deletions polib.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def save(self, fpath=None, repr_method='__unicode__', newline=None):
string, controls how universal newlines works
"""
if self.fpath is None and fpath is None:
raise IOError('You must provide a file path to save() method')
raise OSError('You must provide a file path to save() method')
contents = getattr(self, repr_method)()
if fpath is None:
fpath = self.fpath
Expand Down Expand Up @@ -1376,11 +1376,11 @@ def parse(self):
if tokens[0] in keywords and nb_tokens > 1:
line = line[len(tokens[0]):].lstrip()
if re.search(r'([^\\]|^)"', line[1:-1]):
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'unescaped double quote found' %
(fpath, self.current_line))
if line[0] != '"' or line[-1] != '"':
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'string not delimited by double quotes' %
(fpath, self.current_line))
self.current_token = line
Expand All @@ -1398,11 +1398,11 @@ def parse(self):
elif line[:1] == '"':
# we are on a continuation line
if re.search(r'([^\\]|^)"', line[1:-1]):
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'unescaped double quote found' %
(fpath, self.current_line))
if line[-1] != '"':
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'string not delimited by double quotes' %
(fpath, self.current_line))
self.process('mc')
Expand Down Expand Up @@ -1431,7 +1431,7 @@ def parse(self):

elif tokens[0] == '#|':
if nb_tokens <= 1:
raise IOError('Syntax error in po file %s(line %s)' %
raise OSError('Syntax error in po file %s(line %s)' %
(fpath, self.current_line))

# Remove the marker and any whitespace right after that.
Expand All @@ -1445,14 +1445,14 @@ def parse(self):

if nb_tokens == 2:
# Invalid continuation line.
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'invalid continuation line' %
(fpath, self.current_line))

# we are on a "previous translation" comment line,
if tokens[1] not in prev_keywords:
# Unknown keyword in previous translation comment.
raise IOError('Syntax error in po file %s(line %s): '
raise OSError('Syntax error in po file %s(line %s): '
'unknown keyword %s' %
(fpath, self.current_line,
tokens[1]))
Expand All @@ -1464,7 +1464,7 @@ def parse(self):
self.process(prev_keywords[tokens[1]])

else:
raise IOError('Syntax error in po file %s(line %s)' %
raise OSError('Syntax error in po file %s(line %s)' %
(fpath, self.current_line))

if self.current_entry and len(tokens) > 0 and \
Expand Down Expand Up @@ -1535,7 +1535,7 @@ def process(self, symbol):
fpath = '%s ' % self.instance.fpath if self.instance.fpath else ''
if hasattr(self.fhandle, 'close'):
self.fhandle.close()
raise IOError('Syntax error in po file %s(line %s)' %
raise OSError('Syntax error in po file %s(line %s)' %
(fpath, self.current_line))

# state handlers
Expand Down Expand Up @@ -1740,14 +1740,14 @@ def parse(self):
elif magic_number == MOFile.MAGIC_SWAPPED:
ii = '>II'
else:
raise IOError('Invalid mo file, magic number is incorrect !')
raise OSError('Invalid mo file, magic number is incorrect !')
self.instance.magic_number = magic_number
# parse the version number and the number of strings
version, numofstrings = self._readbinary(ii, 8)
# from MO file format specs: "A program seeing an unexpected major
# revision number should stop reading the MO file entirely"
if version >> 16 not in (0, 1):
raise IOError('Invalid mo file, unexpected major revision number')
raise OSError('Invalid mo file, unexpected major revision number')
self.instance.version = version
# original strings and translation strings hash table offset
msgids_hash_offset, msgstrs_hash_offset = self._readbinary(ii, 8)
Expand Down
16 changes: 8 additions & 8 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_unescaped_double_quote1(self):
try:
po = polib.pofile(data)
self.fail("Unescaped quote not detected")
except IOError:
except OSError:
exc = sys.exc_info()[1]
msg = 'Syntax error in po file (line 3): unescaped double quote found'
self.assertEqual(str(exc), msg)
Expand All @@ -220,7 +220,7 @@ def test_unescaped_double_quote2(self):
try:
po = polib.pofile(data)
self.fail("Unescaped quote not detected")
except IOError:
except OSError:
exc = sys.exc_info()[1]
msg = 'Syntax error in po file (line 4): unescaped double quote found'
self.assertEqual(str(exc), msg)
Expand All @@ -236,7 +236,7 @@ def test_unescaped_double_quote3(self):
try:
po = polib.pofile(data)
self.fail("Unescaped quote not detected")
except IOError:
except OSError:
exc = sys.exc_info()[1]
msg = 'Syntax error in po file (line 3): unescaped double quote found'
self.assertEqual(str(exc), msg)
Expand All @@ -253,7 +253,7 @@ def test_unescaped_double_quote4(self):
try:
po = polib.pofile(data)
self.fail("Unescaped quote not detected")
except IOError:
except OSError:
exc = sys.exc_info()[1]
msg = 'Syntax error in po file (line 4): unescaped double quote found'
self.assertEqual(str(exc), msg)
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_no_double_quote_delimiters(self):
try:
polib.pofile(data)
self.fail("Strings not delimited by double quotes not detected")
except IOError as ex:
except OSError as ex:
msg = 'string not delimited by double quotes'
self.assertIn(msg, str(ex))

Expand All @@ -294,7 +294,7 @@ def test_syntax_error1(self):
try:
polib.pofile('tests/test_syntax_error1.po')
self.fail("Syntax error not detected")
except IOError:
except OSError:
exc = sys.exc_info()[1]
msg = "Syntax error in po file tests/test_syntax_error1.po (line 5)"
self.assertEqual(str(exc), msg)
Expand Down Expand Up @@ -495,7 +495,7 @@ def test_find5(self):

def test_save1(self):
pofile = polib.POFile()
self.assertRaises(IOError, pofile.save)
self.assertRaises(OSError, pofile.save)

def test_save2(self):
fd, tmpfile = tempfile.mkstemp()
Expand Down Expand Up @@ -860,7 +860,7 @@ def test_msgctxt(self):
self.assertEqual(mo.__unicode__(), expected)

def test_invalid_version(self):
self.assertRaises(IOError, polib.mofile, 'tests/test_invalid_version.mo')
self.assertRaises(OSError, polib.mofile, 'tests/test_invalid_version.mo')
polib.mofile('tests/test_version_1.1.mo')

def test_no_header(self):
Expand Down