Skip to content

Commit

Permalink
Merge pull request #533 from python-rope/unicode-handling
Browse files Browse the repository at this point in the history
Remove usage of `unicode` type
  • Loading branch information
lieryan committed Nov 25, 2022
2 parents c0433a8 + e71c145 commit 82ba232
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
7 changes: 1 addition & 6 deletions rope/base/ast.py
Expand Up @@ -3,15 +3,10 @@

from rope.base import fscommands

try:
unicode
except NameError:
unicode = str


def parse(source, filename="<string>"):
# NOTE: the raw string should be given to `compile` function
if isinstance(source, unicode):
if isinstance(source, str):
source = fscommands.unicode_to_file_data(source)
if b"\r" in source:
source = source.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
Expand Down
18 changes: 12 additions & 6 deletions rope/base/change.py
Expand Up @@ -2,9 +2,11 @@
import difflib
import os
import time
from typing import Union

import rope.base.fscommands
from rope.base import taskhandle, exceptions, utils
from rope.base import exceptions, taskhandle, utils
from rope.base.fscommands import FileContent


class Change:
Expand Down Expand Up @@ -329,11 +331,15 @@ def _get_fscommands(self, resource):
return self.direct_commands
return self.fscommands

def write_file(self, resource, contents):
data = rope.base.fscommands.unicode_to_file_data(
contents,
newlines=resource.newlines,
)
def write_file(self, resource, contents: Union[str, FileContent]):
data: FileContent
if not isinstance(contents, bytes):
data = rope.base.fscommands.unicode_to_file_data(
contents,
newlines=resource.newlines,
)
else:
data = contents
fscommands = self._get_fscommands(resource)
fscommands.write(resource.real_path, data)
for observer in list(self.project.observers):
Expand Down
20 changes: 9 additions & 11 deletions rope/base/fscommands.py
Expand Up @@ -12,11 +12,10 @@
import subprocess

import rope.base.utils.pycompat as pycompat
import typing

try:
unicode
except NameError:
unicode = str

FileContent = typing.NewType("FileContent", bytes)


def create_fscommands(root):
Expand Down Expand Up @@ -214,19 +213,18 @@ def _execute(args, cwd=None):
return process.returncode


def unicode_to_file_data(contents, encoding=None, newlines=None):
if not isinstance(contents, unicode):
return contents
def unicode_to_file_data(contents: str, encoding=None, newlines=None) -> FileContent:
assert isinstance(contents, str)
if newlines and newlines != "\n":
contents = contents.replace("\n", newlines)
if encoding is None:
encoding = read_str_coding(contents)
if encoding is not None:
return contents.encode(encoding)
return FileContent(contents.encode(encoding))
try:
return contents.encode()
return FileContent(contents.encode())
except UnicodeEncodeError:
return contents.encode("utf-8")
return FileContent(contents.encode("utf-8"))


def file_data_to_unicode(data, encoding=None):
Expand All @@ -242,7 +240,7 @@ def file_data_to_unicode(data, encoding=None):


def _decode_data(data, encoding):
if isinstance(data, unicode):
if isinstance(data, str):
return data
if encoding is None:
encoding = read_str_coding(data)
Expand Down
10 changes: 6 additions & 4 deletions rope/base/pyobjects.py
@@ -1,4 +1,5 @@
from rope.base.fscommands import _decode_data
from typing import Optional

from rope.base import ast, exceptions, utils


Expand Down Expand Up @@ -224,13 +225,14 @@ def get_module(self):
current_object = current_object.parent
return current_object

def get_doc(self):
def get_doc(self) -> Optional[str]:
if len(self.get_ast().body) > 0:
expr = self.get_ast().body[0]
if isinstance(expr, ast.Expr) and isinstance(expr.value, ast.Str):
docstring = expr.value.s
coding = self.get_module().coding
return _decode_data(docstring, coding)
assert isinstance(docstring, str)
return docstring
return None

def _get_defined_objects(self):
if self.defineds is None:
Expand Down
7 changes: 1 addition & 6 deletions rope/base/pyobjectsdef.py
Expand Up @@ -17,11 +17,6 @@
)
from rope.base.utils import pycompat

try:
unicode
except NameError:
unicode = str


class PyFunction(pyobjects.PyFunction):
def __init__(self, pycore, ast_node, parent):
Expand Down Expand Up @@ -204,7 +199,7 @@ def _init_source(self, pycore, source_code, resource):
source_bytes = resource.read_bytes()
source_code, _ = fscommands.file_data_to_unicode(source_bytes)
else:
if isinstance(source_code, unicode):
if isinstance(source_code, str):
source_bytes = fscommands.unicode_to_file_data(source_code)
else:
source_bytes = source_code
Expand Down
9 changes: 2 additions & 7 deletions ropetest/contrib/codeassisttest.py
Expand Up @@ -16,11 +16,6 @@
)
from ropetest import testutils

try:
unicode
except NameError:
unicode = str


class CodeAssistTest(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -731,7 +726,7 @@ def test_get_pydoc_unicode(self):
def foo():
u"юникод-объект"''')
doc = get_doc(self.project, src, src.index("foo") + 1)
self.assertTrue(isinstance(doc, unicode))
self.assertTrue(isinstance(doc, str))
self.assertTrue("юникод-объект" in doc)

def test_get_pydoc_utf8_bytestring(self):
Expand All @@ -740,7 +735,7 @@ def test_get_pydoc_utf8_bytestring(self):
def foo():
"байтстринг"''')
doc = get_doc(self.project, src, src.index("foo") + 1)
self.assertTrue(isinstance(doc, unicode))
self.assertTrue(isinstance(doc, str))
self.assertTrue("байтстринг" in doc)

def test_get_pydoc_for_functions(self):
Expand Down

0 comments on commit 82ba232

Please sign in to comment.