Skip to content

Commit 22d96c4

Browse files
committed
Add the PSL constructor for loading list from file
1 parent d90d908 commit 22d96c4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/upa_url_bind.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ NB_MODULE(upa_url, m) {
204204
.def("finalize", &public_suffix_list_py::finalize)
205205

206206
// Load Public Suffix List from file
207+
.def("__init__", [](public_suffix_list_py* t, std::string_view filename) {
208+
new (t) public_suffix_list_py{};
209+
if (!t->load(filename))
210+
throw std::runtime_error("Error loading Public Suffix List from file.");
211+
}, nb::arg("filename"))
207212
.def_static("load", [](std::string_view filename)
208213
-> std::optional<public_suffix_list_py> {
209214
public_suffix_list_py psl;

tests/test_psl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ def test_psl_push(self):
2525
psl.push('^.com')
2626
self.assertFalse(psl.finalize())
2727

28+
def test_psl_construct(self):
29+
# Use constructor to load the list
30+
dir = os.path.dirname(os.path.realpath(__file__))
31+
psl = PSL(os.path.join(dir, 'PSL.dat'))
32+
# public suffix
33+
self.assertEqual(psl.public_suffix('any.org.uk'), 'org.uk')
34+
35+
# Load error
36+
with self.assertRaises(RuntimeError):
37+
PSL(os.path.join(dir, 'no_such_file.dat'))
38+
2839
def test_psl(self):
2940
# Load list
3041
dir = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)