Skip to content

add support for returning type of urllib.parse.ParseResult #13

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

Open
wants to merge 1 commit into
base: main
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
6 changes: 5 additions & 1 deletion can_ada-stubs/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Iterator, overload
from typing import Iterator, overload, TYPE_CHECKING

if TYPE_CHECKING:
from urllib.parse import ParseResult

__version__: str

Expand Down Expand Up @@ -68,3 +71,4 @@ def can_parse(input: str, base_input: str | None = ...) -> bool: ...
def idna_decode(arg0: str) -> str: ...
def idna_encode(arg0: str) -> bytes: ...
def parse(arg0: str) -> URL: ...
def parse_compat(arg0: str) -> ParseResult: ...
67 changes: 67 additions & 0 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace py = pybind11;

static py::object get_parse_result_class() {
static py::object cls = py::module_::import("urllib.parse").attr("ParseResult");
return cls;
}

PYBIND11_MODULE(can_ada, m) {
#ifdef VERSION_INFO
m.attr("__version__") = Py_STRINGIFY(VERSION_INFO);
Expand Down Expand Up @@ -142,4 +147,66 @@ PYBIND11_MODULE(can_ada, m) {
return url.value();
});

m.def("parse_compat", [](std::string_view input) {
ada::result<ada::url_aggregator> result = ada::parse<ada::url_aggregator>(input);
if (!result) {
throw py::value_error("URL could not be parsed.");
}

auto& url = result.value();

std::string scheme = [&] {
std::string s = std::string(url.get_protocol());
return (!s.empty() && s.back() == ':') ? s.substr(0, s.size() - 1) : s;
}();


std::string netloc;
if (url.has_non_empty_username()) {
netloc += std::string(url.get_username());
if (url.has_password()) {
netloc += ":" + std::string(url.get_password());
}
netloc += "@";
}
netloc += std::string(url.get_host());
if (url.has_port()) {
netloc += ":" + std::string(url.get_port());
}

std::string path, params;
// not really correct, but this is urllib.parse.urlparse behaviour
[&] {
std::string raw_path = std::string(url.get_pathname());
size_t last_slash = raw_path.rfind('/');
std::string last_segment = (last_slash != std::string::npos)
? raw_path.substr(last_slash + 1)
: raw_path;

size_t semi = last_segment.find(';');
if (semi != std::string::npos) {
path = (last_slash != std::string::npos ? raw_path.substr(0, last_slash + 1) : "")
+ last_segment.substr(0, semi);
params = last_segment.substr(semi + 1);
} else {
path = raw_path;
params = "";
}
}();

std::string query = [&] {
std::string s = std::string(url.get_search());
return (!s.empty() && s.front() == '?') ? s.substr(1) : s;
}();

std::string fragment = [&] {
std::string s = std::string(url.get_hash());
return (!s.empty() && s.front() == '#') ? s.substr(1) : s;
}();


return get_parse_result_class()(scheme, netloc, path, params, query, fragment);
});


}
14 changes: 14 additions & 0 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def can_ada_parse():
# not valid WHATWG URLs.
pass

def can_ada_parse_compat():
for line in data():
try:
can_ada.parse_compat(line)
except ValueError:
# There are a small number of URLs in the sample data that are
# not valid WHATWG URLs.
pass

@pytest.mark.slow
def test_urllib_parse(benchmark):
Expand All @@ -53,3 +61,9 @@ def test_ada_python_parse(benchmark):
@pytest.mark.slow
def test_can_ada_parse(benchmark):
benchmark(can_ada_parse)



@pytest.mark.slow
def test_can_ada_parse_compat(benchmark):
benchmark(can_ada_parse_compat)