Diaoul / babelfish

BabelFish is a Python library to work with countries and languages
BSD 3-Clause "New" or "Revised" License
25 stars 14 forks source link

Python3 Converter Exception #7

Closed Toilal closed 10 years ago

Toilal commented 10 years ago

I have an error in guessit with Python3 (3.3.3 x86 on windows). It runs without any problem on Python 2.7.

For: Movies/Persepolis (2007)/[XCT] Persepolis [H264+Aac-128(Fr-Eng)+ST(Fr-Eng)+Ind].mkv
Traceback (most recent call last):
  File "D:\devel\workspace\babelfish\babelfish\converters\__init__.py", line 156, in convert
    return self.to_symbol[alpha3]
KeyError: 'xct'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python\x86\Python33\Lib\runpy.py", line 160, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "D:\Python\x86\Python33\Lib\runpy.py", line 73, in _run_code
    exec(code, run_globals)
  File ".\guessit\__main__.py", line 160, in <module>
    main()
  File ".\guessit\__main__.py", line 154, in main
    advanced=options.advanced)
  File ".\guessit\__main__.py", line 37, in detect_filename
    print('GuessIt found:', guess_file_info(filename, filetype, info).nice_string(advanced))
  File ".\guessit\__init__.py", line 134, in guess_file_info
    result.append(_guess_filename(filename, filetype))
  File ".\guessit\__init__.py", line 105, in _guess_filename
    mtree = IterativeMatcher(filename, filetype=filetype)
  File ".\guessit\matcher.py", line 120, in __init__
    self._apply_transfo(transformer)
  File ".\guessit\matcher.py", line 132, in _apply_transfo
    transformer.process(self.match_tree, *all_args, **all_kwargs)
  File ".\guessit\transfo\guess_language.py", line 118, in process
    SingleNodeGuesser(self.guess_language, None, self.log, *args, **kwargs).process(mtree)
  File ".\guessit\transfo\__init__.py", line 151, in process
    find_and_split_node(node, strategy, self.skip_nodes, self.logger)
  File ".\guessit\transfo\__init__.py", line 80, in find_and_split_node
    matcher_result = matcher(*all_args)
  File ".\guessit\transfo\guess_language.py", line 37, in guess_language
    guess = search_language(string)
  File ".\guessit\language.py", line 366, in search_language
    if language != 'mul' and not hasattr(language, 'alpha2'):
  File ".\guessit\language.py", line 221, in alpha2
    return self.lang.alpha2
  File "D:\devel\workspace\babelfish\babelfish\language.py", line 130, in __getattr__
    return get_language_converter(name).convert(alpha3, country, script)
  File "D:\devel\workspace\babelfish\babelfish\converters\__init__.py", line 158, in convert
    raise LanguageConvertError(alpha3, country, script)
babelfish.exceptions.LanguageConvertError: xct
Diaoul commented 10 years ago

Can you create a unittest that reproduces this in babelfish? Currently the tests are passing: https://travis-ci.org/Diaoul/babelfish

Toilal commented 10 years ago

I'll try to debug directly from guessit to fix the problem, but i think it may be a problem with entry points loading ... Wait and see :-)

Toilal commented 10 years ago

Or resources loading

Toilal commented 10 years ago

I found the cause of the problem.

hasattr(language.alpha2) with Python2 returns None, but raise LanguageConverterError on Python.

From python doc, it's the attended behavior :+1:

Python2: hasattr(object, name)¶ The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

Python3: hasattr(object, name) The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)

To be Python3 compatible, the __getattr__ method should raise AttributeError in this case, instead of LanguageConverterError