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 out utils.string and utils.types subpackages #95

Merged
merged 4 commits into from
Jul 24, 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
48 changes: 23 additions & 25 deletions solcx/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import textwrap

from .utils.string import force_text


def force_text_maybe(value, encoding="iso-8859-1"):
if value is not None:
return force_text(value)

from typing import Optional

DEFAULT_MESSAGE = "An error occurred during execution"


class SolcError(Exception):
message = DEFAULT_MESSAGE

def __init__(self, command, return_code, stdin_data, stdout_data, stderr_data, message=None):
def __init__(
self,
command: str,
return_code: int,
stdin_data: Optional[str],
stdout_data: str,
stderr_data: str,
message: Optional[str] = None,
) -> None:
if message is not None:
self.message = message
self.command = command
self.return_code = return_code
self.stdin_data = force_text_maybe(stdin_data, "utf8")
self.stderr_data = force_text_maybe(stderr_data, "utf8")
self.stdout_data = force_text_maybe(stdout_data, "utf8")

def __str__(self):
return textwrap.dedent(
f"""
{self.message}
> command: `{' '.join(self.command)}`
> return code: `{self.return_code}`
> stdout:
{self.stdout_data}
> stderr:
{self.stderr_data}
"""
self.stdin_data = stdin_data
self.stderr_data = stderr_data
self.stdout_data = stdout_data

def __str__(self) -> str:
return (
f"{self.message}"
f"\n> command: `{' '.join(self.command)}`"
f"\n> return code: `{self.return_code}`"
"\n> stdout:"
f"\n{self.stdout_data}"
"\n> stderr:"
f"\n{self.stderr_data}"
).strip()


Expand Down
80 changes: 0 additions & 80 deletions solcx/utils/string.py

This file was deleted.

25 changes: 0 additions & 25 deletions solcx/utils/types.py

This file was deleted.

12 changes: 6 additions & 6 deletions solcx/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

from .exceptions import SolcError
from .install import get_executable
from .utils.string import coerce_return_to_text, force_bytes


@coerce_return_to_text
def solc_wrapper(
solc_binary=None,
stdin=None,
Expand Down Expand Up @@ -164,12 +162,14 @@ def solc_wrapper(
command.append("-")

if stdin is not None:
# solc seems to expects utf-8 from stdin:
# see Scanner class in Solidity source
stdin = force_bytes(stdin, "utf8")
stdin = str(stdin)

proc = subprocess.Popen(
command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf8",
)

stdoutdata, stderrdata = proc.communicate(stdin)
Expand Down