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

Tear out remainders of Python 2 support #946

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
28 changes: 8 additions & 20 deletions python/opencc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
from __future__ import absolute_import, unicode_literals

import os
import sys

try:
import opencc_clib
except ImportError:
from opencc.clib import opencc_clib
from opencc.clib import opencc_clib

__all__ = ['OpenCC', 'CONFIGS', '__version__']
__all__ = ['CONFIGS', 'OpenCC', '__version__']

__version__ = opencc_clib.__version__
_this_dir = os.path.dirname(os.path.abspath(__file__))
_opencc_share_dir = os.path.join(_this_dir, 'clib', 'share', 'opencc')
_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..'))
_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..'))
_opencc_configdir = os.path.join(_opencc_rootdir, 'data', 'config')

if sys.version_info.major == 2:
text_type = unicode # noqa
else:
text_type = str

if os.path.isdir(_opencc_share_dir):
CONFIGS = [f for f in os.listdir(_opencc_share_dir) if f.endswith('.json')]
elif os.path.isdir(_opencc_configdir):
Expand All @@ -36,7 +25,7 @@ def _append_path_to_env(name, path):
if value == '':
value = path
else:
value += ':' + path
value += f':{path}'
os.environ[name] = value


Expand All @@ -49,10 +38,9 @@ def __init__(self, config='t2s'):
config_under_share_dir = os.path.join(_opencc_share_dir, config)
if os.path.isfile(config_under_share_dir):
config = config_under_share_dir
super(OpenCC, self).__init__(config)
super().__init__(config)
self.config = config

def convert(self, text):
if isinstance(text, text_type):
text = text.encode('utf-8')
return super(OpenCC, self).convert(text, len(text))
def convert(self, text: str):
byte_text = text.encode('utf-8')
return super().convert(byte_text, len(byte_text))
2 changes: 0 additions & 2 deletions python/tests/test_opencc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import unicode_literals

import os
import pytest
import sys
Expand Down