162 global PESieve_version
163 ptr_size = ctypes.sizeof(ctypes.c_voidp)
165 pesieve_dll =
"pe-sieve32.dll"
167 pesieve_dll =
"pe-sieve64.dll"
170 pesieve_path = os.path.abspath(os.getcwd()) + os.path.sep + pesieve_dll
171 lib = ctypes.cdll.LoadLibrary(pesieve_path)
172 except FileNotFoundError:
173 if 'PESIEVE_DIR' in os.environ:
174 pesieve_path = os.environ.get(
'PESIEVE_DIR') + os.path.sep + pesieve_dll
175 lib = ctypes.cdll.LoadLibrary(pesieve_path)
179 PESieve_version = ctypes.c_uint32.in_dll(lib,
"PESieve_version").value
180 if (PESieve_version < PESIEVE_MIN_VER
or PESieve_version > PESIEVE_MAX_VER):
182 exception_msg = f
"Version mismatch: the PE-sieve.dll version ({dll_version_str}) doesn't match the bindings version"
183 raise Exception(exception_msg)
185 lib.PESieve_scan.argtypes = [ctypes.POINTER(t_params)]
186 lib.PESieve_scan.restype = t_report
188 lib.PESieve_scan_ex.argtypes = [
189 ctypes.POINTER(t_params),
193 ctypes.POINTER(ctypes.c_size_t),
195 lib.PESieve_scan_ex.restype = t_report
210def PESieve_scan_ex(params: t_params, rtype: t_report_type, buf_size: int) -> tuple[t_report, str, int]:
213 if not isinstance(params, t_params):
214 raise TypeError(
"params must be t_params")
216 raise ValueError(
"buf_size must be >= 0")
218 out_size = ctypes.c_size_t(0)
221 json_buf = ctypes.create_string_buffer(buf_size)
222 report = lib.PESieve_scan_ex(
223 ctypes.byref(params),
225 ctypes.cast(json_buf, ctypes.c_char_p),
226 ctypes.c_size_t(buf_size),
227 ctypes.byref(out_size)
229 json_str = json_buf.value.decode(
"utf-8", errors=
"replace")
231 report = lib.PESieve_scan_ex(
232 ctypes.byref(params),
236 ctypes.byref(out_size)
240 return (report, json_str, out_size.value)