Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb1832f

Browse files
committed
Create a custom exception type for this library
1 parent0d5c8b8 commitfb1832f

File tree

7 files changed

+112
-8
lines changed

7 files changed

+112
-8
lines changed

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
55

66
##Unreleased
77
---
8+
###Breaks
9+
* Library now throws the custom`adblock.AdblockException` exception, instead of`ValueError`.
10+
811

912
##0.4.4 - (2021-04-13)
1013
---

‎adblock/__init__.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
fromadblock.adblockimport__version__,Engine,FilterSet,BlockerResult,UrlSpecificResources
1+
fromadblock.adblockimport (
2+
__version__,
3+
Engine,
4+
FilterSet,
5+
BlockerResult,
6+
UrlSpecificResources,
7+
AdblockException,
8+
BlockerException,
9+
SerializationError,
10+
DeserializationError,
11+
OptimizedFilterExistence,
12+
BadFilterAddUnsupported,
13+
FilterExists,
14+
)
215

316

4-
__all__= ("Engine","FilterSet","BlockerResult","UrlSpecificResources")
17+
__all__= (
18+
"Engine",
19+
"FilterSet",
20+
"BlockerResult",
21+
"UrlSpecificResources",
22+
"AdblockException",
23+
"BlockerException",
24+
"SerializationError",
25+
"DeserializationError",
26+
"OptimizedFilterExistence",
27+
"BadFilterAddUnsupported",
28+
"FilterExists",
29+
)

‎adblock/adblock.pyi‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
fromtypingimportOptional,Dict,List,Set
22

3+
classAdblockException(Exception):
4+
pass
5+
6+
classBlockerException(AdblockException):
7+
pass
8+
9+
classSerializationError(BlockerException):
10+
pass
11+
12+
classDeserializationError(BlockerException):
13+
pass
14+
15+
classOptimizedFilterExistence(BlockerException):
16+
pass
17+
18+
classBadFilterAddUnsupported(BlockerException):
19+
pass
20+
21+
classFilterExists(BlockerException):
22+
pass
23+
324
classBlockerResult:
425
matched:bool
526
explicit_cancel:bool

‎src/lib.rs‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use adblock::engine::Engine as RustEngine;
1717
use adblock::lists::FilterFormat;
1818
use adblock::lists::FilterSetasRustFilterSet;
1919
use pyo3::class::PyObjectProtocol;
20-
use pyo3::exceptions::PyValueError;
20+
use pyo3::create_exception;
21+
use pyo3::exceptions::PyException;
2122
use pyo3::prelude::*;
2223
use pyo3::types::PyBytes;
2324
use pyo3::PyErr;
@@ -31,12 +32,28 @@ use std::io::{Read, Write};
3132

3233
/// Brave's adblocking library in Python!
3334
#[pymodule]
34-
fnadblock(_py:Python<'_>,m:&PyModule) ->PyResult<()>{
35+
fnadblock(py:Python<'_>,m:&PyModule) ->PyResult<()>{
3536
m.add("__version__",env!("CARGO_PKG_VERSION"))?;
3637
m.add_class::<Engine>()?;
3738
m.add_class::<FilterSet>()?;
3839
m.add_class::<BlockerResult>()?;
3940
m.add_class::<UrlSpecificResources>()?;
41+
m.add("AdblockException", py.get_type::<AdblockException>())?;
42+
m.add("BlockerException", py.get_type::<BlockerException>())?;
43+
m.add("SerializationError", py.get_type::<SerializationError>())?;
44+
m.add(
45+
"DeserializationError",
46+
py.get_type::<DeserializationError>(),
47+
)?;
48+
m.add(
49+
"OptimizedFilterExistence",
50+
py.get_type::<OptimizedFilterExistence>(),
51+
)?;
52+
m.add(
53+
"BadFilterAddUnsupported",
54+
py.get_type::<BadFilterAddUnsupported>(),
55+
)?;
56+
m.add("FilterExists", py.get_type::<FilterExists>())?;
4057
Ok(())
4158
}
4259

@@ -141,9 +158,24 @@ impl Display for BlockerError {
141158
}
142159
}
143160

161+
create_exception!(adblock,AdblockException,PyException);
162+
create_exception!(adblock,BlockerException,AdblockException);
163+
create_exception!(adblock,SerializationError,BlockerException);
164+
create_exception!(adblock,DeserializationError,BlockerException);
165+
create_exception!(adblock,OptimizedFilterExistence,BlockerException);
166+
create_exception!(adblock,BadFilterAddUnsupported,BlockerException);
167+
create_exception!(adblock,FilterExists,BlockerException);
168+
144169
implInto<PyErr>forBlockerError{
145170
fninto(self) ->PyErr{
146-
PyErr::new::<PyValueError,_>(format!("{:?}",self))
171+
let msg =format!("{:?}",self);
172+
matchself{
173+
Self::SerializationError =>PyErr::new::<SerializationError,_>(msg),
174+
Self::DeserializationError =>PyErr::new::<DeserializationError,_>(msg),
175+
Self::OptimizedFilterExistence =>PyErr::new::<OptimizedFilterExistence,_>(msg),
176+
Self::BadFilterAddUnsupported =>PyErr::new::<BadFilterAddUnsupported,_>(msg),
177+
Self::FilterExists =>PyErr::new::<FilterExists,_>(msg),
178+
}
147179
}
148180
}
149181

@@ -163,7 +195,7 @@ fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
163195
match filter_format{
164196
"standard" =>Ok(FilterFormat::Standard),
165197
"hosts" =>Ok(FilterFormat::Hosts),
166-
_ =>Err(PyErr::new::<PyValueError,_>("Invalid format value")),
198+
_ =>Err(PyErr::new::<AdblockException,_>("Invalid format value")),
167199
}
168200
}
169201

‎tests/test_engine.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ def test_serde_file(tmpdir):
5252
assertdeserialization_resultisNone
5353

5454

55+
deftest_deserialize_corrupt(tmpdir):
56+
path=str(tmpdir/"corrupt_cache.dat")
57+
withopen(path,"w",encoding="utf-8")asf:
58+
f.write("abc")
59+
60+
engine=empty_engine()
61+
withpytest.raises(adblock.DeserializationError):
62+
engine.deserialize_from_file(path)
63+
withpytest.raises(adblock.DeserializationError):
64+
engine.deserialize(b"abc")
65+
5566
deftest_serde():
5667
engine=empty_engine()
5768
serialization_result=engine.serialize()

‎tests/test_exceptions.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
importadblock
2+
3+
deftest_correct_baseclasses():
4+
assertissubclass(adblock.AdblockException,Exception)
5+
assertissubclass(adblock.BlockerException,adblock.AdblockException)
6+
assertissubclass(adblock.SerializationError,adblock.BlockerException)
7+
assertissubclass(adblock.DeserializationError,adblock.BlockerException)
8+
assertissubclass(adblock.OptimizedFilterExistence,adblock.BlockerException)
9+
assertissubclass(adblock.BadFilterAddUnsupported,adblock.BlockerException)
10+
assertissubclass(adblock.FilterExists,adblock.BlockerException)

‎tests/test_imports.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ def get_added_classes():
1414
match=re.match(r"m\.add_class::<(.+)>\(\)\?;",line.strip())
1515
ifmatchisnotNone:
1616
classes.append(match.group(1))
17+
continue
1718
returnclasses
1819

1920

2021
deftest_added_classes():
2122
"""
2223
Make sure that there's no class that we added in Rust but didn't import in
23-
`__init__.py` and vice versa.
24+
`__init__.py`.
2425
"""
2526
added_classes=get_added_classes()
26-
assertadded_classes==list(adblock.__all__)
27+
forcinadded_classes:
28+
assertcinadblock.__all__
2729

2830

2931
deftest_dunder_all_classes_imported():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp