Skip to content

return default ipc path whether it exists or not #3245

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

Merged
merged 3 commits into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions newsfragments/3245.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``get_default_ipc_path()`` and ``get_dev_ipc_path()`` now return the path value without checking if the ``geth.ipc`` file exists.
83 changes: 83 additions & 0 deletions tests/core/providers/test_ipc_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import pathlib
import pytest
import socket
import sys
import tempfile
from threading import (
Thread,
)
import time
from unittest.mock import (
patch,
)

from web3.auto.gethdev import (
w3,
Expand All @@ -16,6 +20,8 @@
)
from web3.providers.ipc import (
IPCProvider,
get_default_ipc_path,
get_dev_ipc_path,
)
from web3.types import (
RPCEndpoint,
Expand Down Expand Up @@ -49,6 +55,83 @@ def test_ipc_tilda_in_path():
assert IPCProvider(pathlib.Path("~/foo")).ipc_path == expectedPath


@pytest.mark.parametrize(
"platform, expected_result, expected_error",
[
("darwin", "/Library/Ethereum/geth.ipc", None),
("linux", "/.ethereum/geth.ipc", None),
("freebsd", "/.ethereum/geth.ipc", None),
("win32", r"\\.\pipe\geth.ipc", None),
(
"unknown",
None,
{
"error": ValueError,
"match": (
"Unsupported platform 'unknown'. Only darwin/linux/win32/"
"freebsd are supported. You must specify the ipc_path"
),
},
),
],
)
def test_get_default_ipc_path(platform, expected_result, expected_error):
with patch.object(sys, "platform", platform):
if expected_error:
with pytest.raises(expected_error["error"], match=expected_error["match"]):
get_default_ipc_path()
else:
assert get_default_ipc_path().endswith(expected_result)


@pytest.mark.parametrize(
"provider_env_uri",
[
"/sample/path/to/ipc/geth.ipc",
"",
],
)
@pytest.mark.parametrize(
"platform, expected_result, expected_error",
[
("darwin", "/var/path/to/tmp/T/geth.ipc", None),
("linux", "/tmp/geth.ipc", None),
("freebsd", "/tmp/geth.ipc", None),
("win32", r"\\.\pipe\geth.ipc", None),
(
"unknown",
None,
{
"error": ValueError,
"match": (
"Unsupported platform 'unknown'. Only darwin/linux/win32/"
"freebsd are supported. You must specify the ipc_path"
),
},
),
],
)
def test_get_dev_ipc_path_(provider_env_uri, platform, expected_result, expected_error):
with patch.object(sys, "platform", platform):
with patch.dict(
os.environ,
{
"TMPDIR": "/var/path/to/tmp/T/",
"WEB3_PROVIDER_URI": provider_env_uri,
},
):

if provider_env_uri:
assert get_dev_ipc_path() == provider_env_uri
elif expected_error:
with pytest.raises(
expected_error["error"], match=expected_error["match"]
):
get_dev_ipc_path()
else:
assert get_dev_ipc_path().endswith(expected_result)


@pytest.fixture
def simple_ipc_server(jsonrpc_ipc_pipe_path):
serv = socket.socket(socket.AF_UNIX)
Expand Down
45 changes: 11 additions & 34 deletions web3/providers/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
)
from typing import (
Any,
Optional,
Type,
Union,
)
Expand Down Expand Up @@ -86,62 +85,40 @@ def reset(self) -> socket.socket:
return self.sock


def get_default_ipc_path() -> Optional[str]:
def get_default_ipc_path() -> str:
if sys.platform == "darwin":
ipc_path = os.path.expanduser(
os.path.join("~", "Library", "Ethereum", "geth.ipc")
)
if os.path.exists(ipc_path):
return ipc_path
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed in https://github.com/ethereum/web3.py/pull/2917/files the return None was added to make typing work. I think we can remove the Optional from the return type now that None is being removed.

return os.path.expanduser(os.path.join("~", "Library", "Ethereum", "geth.ipc"))

elif sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
ipc_path = os.path.expanduser(os.path.join("~", ".ethereum", "geth.ipc"))
if os.path.exists(ipc_path):
return ipc_path
return None
return os.path.expanduser(os.path.join("~", ".ethereum", "geth.ipc"))

elif sys.platform == "win32":
ipc_path = r"\\.\pipe\geth.ipc"
if os.path.exists(ipc_path):
return ipc_path
return None
return r"\\.\pipe\geth.ipc"

else:
raise ValueError(
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
"freebsd are supported. You must specify the ipc_path"
)


def get_dev_ipc_path() -> Optional[str]:
def get_dev_ipc_path() -> str:
if os.environ.get("WEB3_PROVIDER_URI", ""):
ipc_path = os.environ.get("WEB3_PROVIDER_URI")
if os.path.exists(ipc_path):
return ipc_path
return None
return os.environ.get("WEB3_PROVIDER_URI")

elif sys.platform == "darwin":
tmpdir = os.environ.get("TMPDIR", "")
ipc_path = os.path.expanduser(os.path.join(tmpdir, "geth.ipc"))
if os.path.exists(ipc_path):
return ipc_path
return None
return os.path.expanduser(os.path.join(tmpdir, "geth.ipc"))

elif sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
ipc_path = os.path.expanduser(os.path.join("/tmp", "geth.ipc"))
if os.path.exists(ipc_path):
return ipc_path
return None
return os.path.expanduser(os.path.join("/tmp", "geth.ipc"))

elif sys.platform == "win32":
ipc_path = os.path.join("\\\\", ".", "pipe", "geth.ipc")
if os.path.exists(ipc_path):
return ipc_path
return r"\\.\pipe\geth.ipc"

else:
raise ValueError(
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
"freebsd are supported. You must specify the ipc_path"
)

Expand Down