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

fix: use default value for encode and decode #83

Merged
merged 1 commit into from
May 31, 2024
Merged
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: 24 additions & 24 deletions ada_url/ada_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _get_obj(constructor, destructor, *args):


def _get_str(x):
ret = ffi.string(x.data, x.length).decode('utf-8') if x.length else ''
ret = ffi.string(x.data, x.length).decode() if x.length else ''
return ret


Expand Down Expand Up @@ -185,14 +185,14 @@ class URL:
scheme_type: Final[SchemeType]

def __init__(self, url: str, base: Optional[str] = None):
url_bytes = url.encode('utf-8')
url_bytes = url.encode()

if base is None:
self.urlobj = _get_obj(
lib.ada_parse, lib.ada_free, url_bytes, len(url_bytes)
)
else:
base_bytes = base.encode('utf-8')
base_bytes = base.encode()
self.urlobj = _get_obj(
lib.ada_parse_with_base,
lib.ada_free,
Expand Down Expand Up @@ -254,7 +254,7 @@ def __getattr__(self, attr: str) -> Union[str, HostType, SchemeType]:
def __setattr__(self, attr: str, value: str) -> None:
if attr in SET_ATTRIBUTES:
try:
value_bytes = value.encode('utf-8')
value_bytes = value.encode()
except Exception:
raise ValueError(f'Invalid value for {attr}') from None

Expand All @@ -276,15 +276,15 @@ def __repr__(self):
@staticmethod
def can_parse(url: str, base: Optional[str] = None) -> bool:
try:
url_bytes = url.encode('utf-8')
url_bytes = url.encode()
except Exception:
return False

if base is None:
return lib.ada_can_parse(url_bytes, len(url_bytes))

try:
base_bytes = base.encode('utf-8')
base_bytes = base.encode()
except Exception:
return False

Expand Down Expand Up @@ -347,7 +347,7 @@ class URLSearchParams:
"""

def __init__(self, params: str):
params_bytes = params.encode('utf-8')
params_bytes = params.encode()
self.paramsobj = _get_obj(
lib.ada_parse_search_params,
lib.ada_free_search_params,
Expand All @@ -363,8 +363,8 @@ def __len__(self) -> int:
return self.size

def append(self, key: str, value: str):
key_bytes = key.encode('utf-8')
value_bytes = value.encode('utf-8')
key_bytes = key.encode()
value_bytes = value.encode()
lib.ada_search_params_append(
self.paramsobj,
key_bytes,
Expand All @@ -374,11 +374,11 @@ def append(self, key: str, value: str):
)

def delete(self, key: str, value: Optional[str] = None):
key_bytes = key.encode('utf-8')
key_bytes = key.encode()
if value is None:
lib.ada_search_params_remove(self.paramsobj, key_bytes, len(key_bytes))
else:
value_bytes = value.encode('utf-8')
value_bytes = value.encode()
lib.ada_search_params_remove_value(
self.paramsobj,
key_bytes,
Expand All @@ -388,12 +388,12 @@ def delete(self, key: str, value: Optional[str] = None):
)

def get(self, key: str) -> str:
key_bytes = key.encode('utf-8')
key_bytes = key.encode()
item = lib.ada_search_params_get(self.paramsobj, key_bytes, len(key_bytes))
return _get_str(item)

def get_all(self, key: str) -> List[str]:
key_bytes = key.encode('utf-8')
key_bytes = key.encode()
items = lib.ada_search_params_get_all(self.paramsobj, key_bytes, len(key_bytes))
count = lib.ada_strings_size(items)

Expand All @@ -405,11 +405,11 @@ def get_all(self, key: str) -> List[str]:
return ret

def has(self, key: str, value: Optional[str] = None) -> bool:
key_bytes = key.encode('utf-8')
key_bytes = key.encode()
if value is None:
return lib.ada_search_params_has(self.paramsobj, key_bytes, len(key_bytes))
else:
value_bytes = value.encode('utf-8')
value_bytes = value.encode()
return lib.ada_search_params_has_value(
self.paramsobj,
key_bytes,
Expand All @@ -419,8 +419,8 @@ def has(self, key: str, value: Optional[str] = None) -> bool:
)

def set(self, key: str, value: str):
key_bytes = key.encode('utf-8')
value_bytes = value.encode('utf-8')
key_bytes = key.encode()
value_bytes = value.encode()
lib.ada_search_params_set(
self.paramsobj,
key_bytes,
Expand Down Expand Up @@ -486,7 +486,7 @@ def check_url(s: str) -> bool:

"""
try:
s_bytes = s.encode('utf-8')
s_bytes = s.encode()
except Exception:
return False

Expand All @@ -508,8 +508,8 @@ def join_url(base_url: str, s: str) -> str:

"""
try:
base_bytes = base_url.encode('utf-8')
s_bytes = s.encode('utf-8')
base_bytes = base_url.encode()
s_bytes = s.encode()
except Exception:
raise ValueError('Invalid URL') from None

Expand Down Expand Up @@ -584,7 +584,7 @@ def parse_url(s: str, attributes: Iterable[str] = PARSE_ATTRIBUTES) -> ParseAttr

"""
try:
s_bytes = s.encode('utf-8')
s_bytes = s.encode()
except Exception:
raise ValueError('Invalid URL') from None

Expand Down Expand Up @@ -629,7 +629,7 @@ def replace_url(s: str, **kwargs: str) -> str:
``ValueError`` is raised if the input URL or one of the components is not valid.
"""
try:
s_bytes = s.encode('utf-8')
s_bytes = s.encode()
except Exception:
raise ValueError('Invalid URL') from None

Expand All @@ -645,7 +645,7 @@ def replace_url(s: str, **kwargs: str) -> str:
continue

try:
value_bytes = value.encode('utf-8')
value_bytes = value.encode()
except Exception:
raise ValueError(f'Invalid value for {attr}') from None

Expand Down Expand Up @@ -745,7 +745,7 @@ def decode(s: Union[str, bytes]) -> str:
@staticmethod
def encode(s: Union[str, bytes]) -> str:
if isinstance(s, str):
s = s.encode('utf-8')
s = s.encode()

val = _get_obj(lib.ada_idna_to_ascii, lib.ada_free_owned_string, s, len(s))
return ffi.string(val.data, val.length) if val.length else b''
Expand Down
Loading