pub(crate) use _pyexpat::module_def;
macro_rules! create_property {
($ctx: expr, $attributes: expr, $name: expr, $class: expr, $element: ident) => {
let attr = $ctx.new_static_getset(
$name,
$class,
move |this: &Py<PyExpatLikeXmlParser>| this.$element.read().clone(),
move |this: &Py<PyExpatLikeXmlParser>, func: PyObjectRef| *this.$element.write() = func,
);
$attributes.insert($ctx.intern_str($name), attr.into());
};
}
macro_rules! create_readonly_int_property {
($ctx: expr, $attributes: expr, $name: expr, $class: expr, $element: ident) => {
let getset = crate::vm::builtins::PyGetSet::new($name, $class, $ctx).with_get(
move |this: &Py<PyExpatLikeXmlParser>, vm: &VirtualMachine| -> PyObjectRef {
vm.ctx.new_int(*this.$element.read()).into()
},
);
let attr = crate::vm::PyRef::new_ref(getset, $ctx.types.getset_type.to_owned(), None);
$attributes.insert($ctx.intern_str($name), attr.into());
};
}
macro_rules! create_bool_property {
($ctx: expr, $attributes: expr, $name: expr, $class: expr, $element: ident) => {
let attr = $ctx.new_static_getset(
$name,
$class,
move |this: &Py<PyExpatLikeXmlParser>| this.$element.read().clone(),
move |this: &Py<PyExpatLikeXmlParser>,
value: PyObjectRef,
vm: &VirtualMachine|
-> PyResult<()> {
let bool_value = value.is_true(vm)?;
*this.$element.write() = vm.ctx.new_bool(bool_value).into();
Ok(())
},
);
$attributes.insert($ctx.intern_str($name), attr.into());
};
}
#[pymodule(name = "pyexpat")]
mod _pyexpat {
use crate::vm::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
VirtualMachine,
builtins::{PyBytesRef, PyException, PyModule, PyStr, PyStrRef, PyType, PyUtf8StrRef},
extend_module,
function::{ArgBytesLike, Either, IntoFuncArgs, OptionalArg, OptionalOption},
types::Constructor,
};
use alloc::collections::VecDeque;
use alloc::rc::Rc;
use core::cell::RefCell;
use core::sync::atomic::{AtomicBool, Ordering};
use rustpython_common::lock::PyRwLock;
use std::io::Read;
use xml::common::Position;
use xml::reader::XmlEvent;
pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py<PyModule>) -> PyResult<()> {
__module_exec(vm, module);
let model = super::_model::module_def(&vm.ctx).create_module(vm)?;
let errors = super::_errors::module_def(&vm.ctx).create_module(vm)?;
extend_module!(vm, module, {
"model" => model,
"errors" => errors,
});
Ok(())
}
type MutableObject = PyRwLock<PyObjectRef>;
#[pyattr(name = "version_info")]
pub(super) const VERSION_INFO: (u32, u32, u32) = (2, 7, 1);
#[pyattr]
const XML_PARAM_ENTITY_PARSING_NEVER: i32 = 0;
#[pyattr]
const XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE: i32 = 1;
#[pyattr]
const XML_PARAM_ENTITY_PARSING_ALWAYS: i32 = 2;
#[pyattr]
#[pyattr(name = "XMLParserType")]
#[pyclass(name = "xmlparser", traverse)]
#[derive(Debug, PyPayload)]
pub(super) struct PyExpatLikeXmlParser {
#[pytraverse(skip)]
namespace_separator: Option<String>,
#[pytraverse(skip)]
base: PyRwLock<Option<String>>,
#[pytraverse(skip)]
current_line: PyRwLock<i64>,
#[pytraverse(skip)]
current_column: PyRwLock<i64>,
#[pytraverse(skip)]
current_byte_index: PyRwLock<i64>,
#[pytraverse(skip)]
backend: PyRwLock<Option<Backend>>,
#[pytraverse(skip)]
busy: AtomicBool,
#[pytraverse(skip)]
finished: AtomicBool,
start_element: MutableObject,
end_element: MutableObject,
character_data: MutableObject,
entity_decl: MutableObject,
buffer_text: MutableObject,
namespace_prefixes: MutableObject,
ordered_attributes: MutableObject,
specified_attributes: MutableObject,
#[pymember]
intern: PyObjectRef,
processing_instruction: MutableObject,
unparsed_entity_decl: MutableObject,
notation_decl: MutableObject,
start_namespace_decl: MutableObject,
end_namespace_decl: MutableObject,
comment: MutableObject,
start_cdata_section: MutableObject,
end_cdata_section: MutableObject,
default: MutableObject,
default_expand: MutableObject,
not_standalone: MutableObject,
external_entity_ref: MutableObject,
start_doctype_decl: MutableObject,
end_doctype_decl: MutableObject,
xml_decl: MutableObject,
element_decl: MutableObject,
attlist_decl: MutableObject,
skipped_entity: MutableObject,
}
type PyExpatLikeXmlParserRef = PyRef<PyExpatLikeXmlParser>;
#[inline]
fn invoke_handler<T>(vm: &VirtualMachine, handler: &MutableObject, args: T) -> PyResult<()>
where
T: IntoFuncArgs,
{
let handler = handler.read().clone();
if vm.is_none(&handler) {
return Ok(());
}
handler.call(args, vm)?;
Ok(())
}
#[derive(Default)]
struct LineTracker {
total_read: usize,
line_starts: Vec<usize>,
current_line: VecDeque<u8>,
line_scanned_bytes: usize,
line_scanned_chars: usize,
}
impl LineTracker {
fn new() -> Self {
Self {
total_read: 0,
line_starts: vec![0],
current_line: VecDeque::new(),
line_scanned_bytes: 0,
line_scanned_chars: 0,
}
}
fn record(&mut self, bytes: &[u8]) {
let mut rest = bytes;
while let Some(nl) = rest.iter().position(|&b| b == b'\n') {
self.total_read += nl + 1;
self.line_starts.push(self.total_read);
self.current_line.clear();
self.line_scanned_bytes = 0;
self.line_scanned_chars = 0;
rest = &rest[nl + 1..];
}
self.current_line.extend(rest.iter().copied());
self.total_read += rest.len();
}
fn advance_chars(slice: &[u8], take: usize) -> (usize, usize) {
let mut bytes = 0usize;
let mut chars = 0usize;
while chars < take && bytes < slice.len() {
let lead = slice[bytes];
let width = if lead < 0x80 {
1
} else if lead >> 5 == 0b110 {
2
} else if lead >> 4 == 0b1110 {
3
} else if lead >> 3 == 0b11110 {
4
} else {
break;
};
if bytes + width > slice.len() {
break;
}
bytes += width;
chars += 1;
}
(bytes, chars)
}
fn byte_index_for(&mut self, pos: xml::common::TextPosition) -> i64 {
let row = pos.row() as usize;
let line_start = self
.line_starts
.get(row)
.copied()
.unwrap_or(self.total_read);
if row + 1 != self.line_starts.len() {
return (line_start + pos.column() as usize) as i64;
}
let target_chars = pos.column() as usize;
if target_chars > self.line_scanned_chars {
let take = target_chars - self.line_scanned_chars;
let (front, back) = self.current_line.as_slices();
let (mut consumed_bytes, mut consumed_chars) = Self::advance_chars(front, take);
if consumed_chars < take && consumed_bytes == front.len() {
let (b, c) = Self::advance_chars(back, take - consumed_chars);
consumed_bytes += b;
consumed_chars += c;
}
self.current_line.drain(..consumed_bytes);
self.line_scanned_bytes += consumed_bytes;
self.line_scanned_chars += consumed_chars;
}
(line_start + self.line_scanned_bytes) as i64
}
}
#[cfg(not(target_arch = "wasm32"))]
struct FeedInner {
data: VecDeque<u8>,
closed: bool,
stopped: bool,
pushes: u64,
}
#[cfg(not(target_arch = "wasm32"))]
struct FeedBuffer {
inner: std::sync::Mutex<FeedInner>,
cv: std::sync::Condvar,
}
#[cfg(not(target_arch = "wasm32"))]
impl FeedBuffer {
fn new() -> Self {
Self {
inner: std::sync::Mutex::new(FeedInner {
data: VecDeque::new(),
closed: false,
stopped: false,
pushes: 0,
}),
cv: std::sync::Condvar::new(),
}
}
fn push(&self, chunk: &[u8], isfinal: bool) -> u64 {
let mut inner = self.inner.lock().unwrap();
inner.data.extend(chunk.iter().copied());
if isfinal {
inner.closed = true;
}
inner.pushes += 1;
let pushes = inner.pushes;
self.cv.notify_all();
pushes
}
fn stop(&self) {
let mut inner = self.inner.lock().unwrap();
inner.stopped = true;
self.cv.notify_all();
}
}
#[cfg(not(target_arch = "wasm32"))]
struct FeedReader {
buf: alloc::sync::Arc<FeedBuffer>,
tracker: Rc<RefCell<LineTracker>>,
events_tx: std::sync::mpsc::Sender<ParserMsg>,
}
#[cfg(not(target_arch = "wasm32"))]
impl Read for FeedReader {
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
let mut inner = self.buf.inner.lock().unwrap();
loop {
if inner.stopped {
return Ok(0);
}
if !inner.data.is_empty() {
let n = out.len().min(inner.data.len());
for slot in &mut out[..n] {
*slot = inner.data.pop_front().unwrap();
}
self.tracker.borrow_mut().record(&out[..n]);
return Ok(n);
}
if inner.closed {
return Ok(0);
}
let _ = self.events_tx.send(ParserMsg::NeedMore(inner.pushes));
inner = self.buf.cv.wait(inner).unwrap();
}
}
}
#[cfg(not(target_arch = "wasm32"))]
struct PreparedEvent {
event: XmlEvent,
line: i64,
column: i64,
byte_index: i64,
}
#[cfg(not(target_arch = "wasm32"))]
enum ParserMsg {
NeedMore(u64),
Event(PreparedEvent),
Finished,
Error,
}
#[cfg(not(target_arch = "wasm32"))]
static POST_FORK_CHILD: AtomicBool = AtomicBool::new(false);
#[cfg(all(not(target_arch = "wasm32"), unix))]
extern "C" fn mark_post_fork_child() {
POST_FORK_CHILD.store(true, Ordering::SeqCst);
}
#[cfg(all(not(target_arch = "wasm32"), unix))]
fn ensure_atfork_hook_registered() {
static INIT: std::sync::Once = std::sync::Once::new();
INIT.call_once(|| unsafe {
libc::pthread_atfork(None, None, Some(mark_post_fork_child));
});
}
#[cfg(all(not(target_arch = "wasm32"), not(unix)))]
fn ensure_atfork_hook_registered() {
}
#[cfg(not(target_arch = "wasm32"))]
fn post_fork_child() -> bool {
POST_FORK_CHILD.load(Ordering::SeqCst)
}
#[cfg(not(target_arch = "wasm32"))]
struct ParserStream {
buf: core::mem::ManuallyDrop<alloc::sync::Arc<FeedBuffer>>,
rx: core::mem::ManuallyDrop<std::sync::Mutex<std::sync::mpsc::Receiver<ParserMsg>>>,
thread: Option<std::thread::JoinHandle<()>>,
}
#[cfg(not(target_arch = "wasm32"))]
impl core::fmt::Debug for ParserStream {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ParserStream").finish_non_exhaustive()
}
}
#[cfg(not(target_arch = "wasm32"))]
impl ParserStream {
fn try_spawn(config: xml::ParserConfig) -> Option<Self> {
ensure_atfork_hook_registered();
if post_fork_child() {
return None;
}
let buf = alloc::sync::Arc::new(FeedBuffer::new());
let (tx, rx) = std::sync::mpsc::channel();
let thread_buf = alloc::sync::Arc::clone(&buf);
let thread = std::thread::Builder::new()
.name("pyexpat-parser".into())
.spawn(move || run_parser_thread(config, thread_buf, tx))
.ok()?;
Some(Self {
buf: core::mem::ManuallyDrop::new(buf),
rx: core::mem::ManuallyDrop::new(std::sync::Mutex::new(rx)),
thread: Some(thread),
})
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Drop for ParserStream {
fn drop(&mut self) {
if post_fork_child() {
if let Some(handle) = self.thread.take() {
core::mem::forget(handle);
}
return;
}
self.buf.stop();
if let Some(handle) = self.thread.take() {
let _ = handle.join();
}
unsafe {
core::mem::ManuallyDrop::drop(&mut self.rx);
core::mem::ManuallyDrop::drop(&mut self.buf);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn run_parser_thread(
config: xml::ParserConfig,
buf: alloc::sync::Arc<FeedBuffer>,
tx: std::sync::mpsc::Sender<ParserMsg>,
) {
let tracker = Rc::new(RefCell::new(LineTracker::new()));
let reader = FeedReader {
buf,
tracker: Rc::clone(&tracker),
events_tx: tx.clone(),
};
let mut parser = config.create_reader(std::io::BufReader::new(reader));
loop {
match parser.next() {
Ok(XmlEvent::EndDocument) => {
let _ = tx.send(ParserMsg::Finished);
break;
}
Ok(event) => {
let pos = parser.position();
let byte_index = tracker.borrow_mut().byte_index_for(pos);
let msg = ParserMsg::Event(PreparedEvent {
event,
line: pos.row() as i64 + 1,
column: pos.column() as i64,
byte_index,
});
if tx.send(msg).is_err() {
break;
}
}
Err(_) => {
let _ = tx.send(ParserMsg::Error);
break;
}
}
}
}
struct TrackingReader<R> {
inner: R,
tracker: Rc<RefCell<LineTracker>>,
}
impl<R: Read> Read for TrackingReader<R> {
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(out)?;
self.tracker.borrow_mut().record(&out[..n]);
Ok(n)
}
}
#[derive(Debug)]
struct SyncParser {
config: xml::ParserConfig,
buffer: Vec<u8>,
dispatched_events: usize,
}
impl SyncParser {
fn new(config: xml::ParserConfig) -> Self {
Self {
config,
buffer: Vec::new(),
dispatched_events: 0,
}
}
}
#[derive(Debug)]
enum Backend {
#[cfg(not(target_arch = "wasm32"))]
Threaded(ParserStream),
Sync(SyncParser),
}
impl Backend {
fn new(config: xml::ParserConfig) -> Self {
#[cfg(not(target_arch = "wasm32"))]
{
if let Some(stream) = ParserStream::try_spawn(config.clone()) {
return Self::Threaded(stream);
}
}
Self::Sync(SyncParser::new(config))
}
}
#[pyclass]
impl PyExpatLikeXmlParser {
fn new(
namespace_separator: Option<String>,
intern: OptionalOption<PyObjectRef>,
vm: &VirtualMachine,
) -> PyExpatLikeXmlParserRef {
let intern_dict = match intern {
OptionalArg::Missing => vm.ctx.new_dict().into(),
OptionalArg::Present(Some(obj)) => obj,
OptionalArg::Present(None) => vm.ctx.none(),
};
Self {
namespace_separator,
base: PyRwLock::new(None),
current_line: PyRwLock::new(1),
current_column: PyRwLock::new(0),
current_byte_index: PyRwLock::new(-1),
backend: PyRwLock::new(None),
busy: AtomicBool::new(false),
finished: AtomicBool::new(false),
start_element: MutableObject::new(vm.ctx.none()),
end_element: MutableObject::new(vm.ctx.none()),
character_data: MutableObject::new(vm.ctx.none()),
entity_decl: MutableObject::new(vm.ctx.none()),
buffer_text: MutableObject::new(vm.ctx.new_bool(false).into()),
namespace_prefixes: MutableObject::new(vm.ctx.new_bool(false).into()),
ordered_attributes: MutableObject::new(vm.ctx.new_bool(false).into()),
specified_attributes: MutableObject::new(vm.ctx.new_bool(false).into()),
intern: intern_dict,
processing_instruction: MutableObject::new(vm.ctx.none()),
unparsed_entity_decl: MutableObject::new(vm.ctx.none()),
notation_decl: MutableObject::new(vm.ctx.none()),
start_namespace_decl: MutableObject::new(vm.ctx.none()),
end_namespace_decl: MutableObject::new(vm.ctx.none()),
comment: MutableObject::new(vm.ctx.none()),
start_cdata_section: MutableObject::new(vm.ctx.none()),
end_cdata_section: MutableObject::new(vm.ctx.none()),
default: MutableObject::new(vm.ctx.none()),
default_expand: MutableObject::new(vm.ctx.none()),
not_standalone: MutableObject::new(vm.ctx.none()),
external_entity_ref: MutableObject::new(vm.ctx.none()),
start_doctype_decl: MutableObject::new(vm.ctx.none()),
end_doctype_decl: MutableObject::new(vm.ctx.none()),
xml_decl: MutableObject::new(vm.ctx.none()),
element_decl: MutableObject::new(vm.ctx.none()),
attlist_decl: MutableObject::new(vm.ctx.none()),
skipped_entity: MutableObject::new(vm.ctx.none()),
}
.into_ref(&vm.ctx)
}
#[extend_class]
fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) {
let attributes = &class.attributes;
create_property!(ctx, attributes, "StartElementHandler", class, start_element);
create_property!(ctx, attributes, "EndElementHandler", class, end_element);
create_property!(
ctx,
attributes,
"CharacterDataHandler",
class,
character_data
);
create_property!(ctx, attributes, "EntityDeclHandler", class, entity_decl);
create_bool_property!(ctx, attributes, "buffer_text", class, buffer_text);
create_bool_property!(
ctx,
attributes,
"namespace_prefixes",
class,
namespace_prefixes
);
create_bool_property!(
ctx,
attributes,
"ordered_attributes",
class,
ordered_attributes
);
create_bool_property!(
ctx,
attributes,
"specified_attributes",
class,
specified_attributes
);
create_readonly_int_property!(
ctx,
attributes,
"CurrentLineNumber",
class,
current_line
);
create_readonly_int_property!(
ctx,
attributes,
"CurrentColumnNumber",
class,
current_column
);
create_readonly_int_property!(
ctx,
attributes,
"CurrentByteIndex",
class,
current_byte_index
);
create_property!(
ctx,
attributes,
"ProcessingInstructionHandler",
class,
processing_instruction
);
create_property!(
ctx,
attributes,
"UnparsedEntityDeclHandler",
class,
unparsed_entity_decl
);
create_property!(ctx, attributes, "NotationDeclHandler", class, notation_decl);
create_property!(
ctx,
attributes,
"StartNamespaceDeclHandler",
class,
start_namespace_decl
);
create_property!(
ctx,
attributes,
"EndNamespaceDeclHandler",
class,
end_namespace_decl
);
create_property!(ctx, attributes, "CommentHandler", class, comment);
create_property!(
ctx,
attributes,
"StartCdataSectionHandler",
class,
start_cdata_section
);
create_property!(
ctx,
attributes,
"EndCdataSectionHandler",
class,
end_cdata_section
);
create_property!(ctx, attributes, "DefaultHandler", class, default);
create_property!(
ctx,
attributes,
"DefaultHandlerExpand",
class,
default_expand
);
create_property!(
ctx,
attributes,
"NotStandaloneHandler",
class,
not_standalone
);
create_property!(
ctx,
attributes,
"ExternalEntityRefHandler",
class,
external_entity_ref
);
create_property!(
ctx,
attributes,
"StartDoctypeDeclHandler",
class,
start_doctype_decl
);
create_property!(
ctx,
attributes,
"EndDoctypeDeclHandler",
class,
end_doctype_decl
);
create_property!(ctx, attributes, "XmlDeclHandler", class, xml_decl);
create_property!(ctx, attributes, "ElementDeclHandler", class, element_decl);
create_property!(ctx, attributes, "AttlistDeclHandler", class, attlist_decl);
create_property!(
ctx,
attributes,
"SkippedEntityHandler",
class,
skipped_entity
);
}
fn create_config(&self) -> xml::ParserConfig {
xml::ParserConfig::new()
.cdata_to_characters(false)
.coalesce_characters(false)
.ignore_comments(false)
.whitespace_to_characters(true)
}
#[pymethod(name = "SetParamEntityParsing")]
fn set_param_entity_parsing(_zelf: &Py<Self>, _flag: i32) -> i32 {
1
}
#[pymethod(name = "UseForeignDTD")]
fn use_foreign_dtd(_zelf: &Py<Self>, _flag: OptionalArg<bool>) {
}
#[pymethod(name = "SetBase")]
fn set_base(zelf: &Py<Self>, base: PyStrRef) {
*zelf.base.write() = Some(AsRef::<str>::as_ref(&base).to_owned());
}
#[pymethod(name = "GetBase")]
fn get_base(zelf: &Py<Self>, vm: &VirtualMachine) -> PyObjectRef {
zelf.base.read().as_ref().map_or_else(
|| vm.ctx.none(),
|base| vm.ctx.new_str(base.as_str()).into(),
)
}
fn make_name(&self, name: &xml::name::OwnedName) -> String {
match (&self.namespace_separator, &name.namespace) {
(Some(sep), Some(ns)) => format!("{}{}{}", ns, sep, name.local_name),
_ => name.local_name.clone(),
}
}
fn intern_name(&self, vm: &VirtualMachine, name: String) -> PyStrRef {
let intern_obj = self.intern.clone();
if let Ok(dict) = intern_obj.downcast::<crate::vm::builtins::PyDict>() {
if let Ok(Some(existing)) = dict.get_item_opt(name.as_str(), vm)
&& let Ok(existing) = existing.downcast::<PyStr>()
{
return existing;
}
let interned = vm.ctx.new_str(name);
let _ = dict.set_item(AsRef::<str>::as_ref(&interned), interned.clone().into(), vm);
interned
} else {
PyStr::from(name).into_ref(&vm.ctx)
}
}
fn dispatch(&self, vm: &VirtualMachine, event: XmlEvent) -> PyResult<()> {
match event {
XmlEvent::StartElement {
name, attributes, ..
} => {
let ordered = self.ordered_attributes.read().is(&vm.ctx.true_value);
let attrs: PyObjectRef = if ordered {
let mut items = Vec::with_capacity(attributes.len() * 2);
for attribute in attributes {
let key = self.intern_name(vm, self.make_name(&attribute.name));
items.push(key.into());
items.push(vm.ctx.new_str(attribute.value).into());
}
vm.ctx.new_list(items).into()
} else {
let dict = vm.ctx.new_dict();
for attribute in attributes {
let key = self.intern_name(vm, self.make_name(&attribute.name));
dict.set_item(
AsRef::<str>::as_ref(&key),
vm.ctx.new_str(attribute.value).into(),
vm,
)
.unwrap();
}
dict.into()
};
let name_str = self.intern_name(vm, self.make_name(&name));
invoke_handler(vm, &self.start_element, (name_str, attrs))
}
XmlEvent::EndElement { name, .. } => {
let name_str = self.intern_name(vm, self.make_name(&name));
invoke_handler(vm, &self.end_element, (name_str,))
}
XmlEvent::Characters(chars) => {
let str = PyStr::from(chars).into_ref(&vm.ctx);
invoke_handler(vm, &self.character_data, (str,))
}
XmlEvent::ProcessingInstruction { name, data } => {
let name = PyStr::from(name).into_ref(&vm.ctx);
let data = PyStr::from(data.unwrap_or_default()).into_ref(&vm.ctx);
invoke_handler(vm, &self.processing_instruction, (name, data))
}
XmlEvent::Comment(comment) => {
let comment = PyStr::from(comment).into_ref(&vm.ctx);
invoke_handler(vm, &self.comment, (comment,))
}
XmlEvent::CData(chars) => {
invoke_handler(vm, &self.start_cdata_section, ())?;
let str = PyStr::from(chars).into_ref(&vm.ctx);
invoke_handler(vm, &self.character_data, (str,))?;
invoke_handler(vm, &self.end_cdata_section, ())
}
_ => Ok(()),
}
}
fn teardown_backend(&self) {
let mut guard = self.backend.write();
*guard = None;
}
#[cfg(not(target_arch = "wasm32"))]
fn pump(&self, vm: &VirtualMachine, min_pushes: u64) -> PyResult<()> {
loop {
let msg = {
let guard = self.backend.read();
let Some(Backend::Threaded(stream)) = guard.as_ref() else {
return Ok(());
};
let received = stream.rx.lock().unwrap().recv();
match received {
Ok(msg) => msg,
Err(_) => return Ok(()),
}
};
match msg {
ParserMsg::NeedMore(seen) => {
if seen >= min_pushes {
return Ok(());
}
continue;
}
ParserMsg::Finished => {
self.finished.store(true, Ordering::SeqCst);
self.teardown_backend();
return Ok(());
}
ParserMsg::Error => {
self.teardown_backend();
return Ok(());
}
ParserMsg::Event(ev) => {
*self.current_line.write() = ev.line;
*self.current_column.write() = ev.column;
*self.current_byte_index.write() = ev.byte_index;
self.dispatch(vm, ev.event)?;
}
}
}
}
fn feed_sync(&self, vm: &VirtualMachine, chunk: &[u8], isfinal: bool) -> PyResult<()> {
let (config, buffer, skip) = {
let mut guard = self.backend.write();
let Some(Backend::Sync(sync)) = guard.as_mut() else {
return Ok(());
};
sync.buffer.extend_from_slice(chunk);
(
sync.config.clone(),
sync.buffer.clone(),
sync.dispatched_events,
)
};
let tracker = Rc::new(RefCell::new(LineTracker::new()));
let reader = TrackingReader {
inner: buffer.as_slice(),
tracker: Rc::clone(&tracker),
};
let mut parser = config.create_reader(reader);
let mut index = 0usize;
let result = loop {
match parser.next() {
Ok(XmlEvent::EndDocument) => break Ok(()),
Ok(event) => {
let this_index = index;
index += 1;
let pos = parser.position();
let byte_index = tracker.borrow_mut().byte_index_for(pos);
*self.current_line.write() = pos.row() as i64 + 1;
*self.current_column.write() = pos.column() as i64;
*self.current_byte_index.write() = byte_index;
if this_index < skip {
continue;
}
if let Err(e) = self.dispatch(vm, event) {
index = this_index + 1;
break Err(e);
}
}
Err(_) => break Ok(()),
}
};
{
let mut guard = self.backend.write();
if let Some(Backend::Sync(sync)) = guard.as_mut() {
sync.dispatched_events = index.max(skip);
}
}
if isfinal {
self.finished.store(true, Ordering::SeqCst);
self.teardown_backend();
}
result
}
fn feed(&self, vm: &VirtualMachine, chunk: &[u8], isfinal: bool) -> PyResult<()> {
if self.busy.swap(true, Ordering::SeqCst) {
return Err(vm.new_runtime_error("Parse() called before Parse() returned"));
}
let result = self.feed_inner(vm, chunk, isfinal);
self.busy.store(false, Ordering::SeqCst);
result
}
fn feed_inner(&self, vm: &VirtualMachine, chunk: &[u8], isfinal: bool) -> PyResult<()> {
if self.finished.load(Ordering::SeqCst) {
return Ok(());
}
{
let mut guard = self.backend.write();
if guard.is_none() {
*guard = Some(Backend::new(self.create_config()));
}
}
#[cfg(not(target_arch = "wasm32"))]
{
let is_threaded = matches!(*self.backend.read(), Some(Backend::Threaded(_)));
if is_threaded {
if post_fork_child() {
self.teardown_backend();
self.finished.store(true, Ordering::SeqCst);
return Err(vm.new_exception_msg(
PyExpatError::class(&vm.ctx).to_owned(),
"cannot continue parsing across a fork()".to_owned().into(),
));
}
let pushes = {
let guard = self.backend.read();
match guard.as_ref() {
Some(Backend::Threaded(stream)) => stream.buf.push(chunk, isfinal),
_ => return Ok(()),
}
};
return self.pump(vm, pushes);
}
}
self.feed_sync(vm, chunk, isfinal)
}
#[pymethod(name = "Parse")]
fn parse(
zelf: &Py<Self>,
data: Either<PyStrRef, PyBytesRef>,
isfinal: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult<i32> {
let bytes = match data {
Either::A(s) => s.as_bytes().to_vec(),
Either::B(b) => b.as_bytes().to_vec(),
};
zelf.feed(vm, &bytes, isfinal.unwrap_or(false))?;
Ok(1)
}
#[pymethod(name = "ParseFile")]
fn parse_file(zelf: &Py<Self>, file: PyObjectRef, vm: &VirtualMachine) -> PyResult<i32> {
let read_res = vm.call_method(&file, "read", ())?;
let bytes_like = ArgBytesLike::try_from_object(vm, read_res)?;
let buf = bytes_like.borrow_buf().to_vec();
zelf.feed(vm, &buf, true)?;
Ok(1)
}
}
#[derive(FromArgs)]
struct ParserCreateArgs {
#[pyarg(any, optional)]
encoding: Option<PyStrRef>,
#[pyarg(any, optional)]
namespace_separator: Option<PyUtf8StrRef>,
#[pyarg(any, optional)]
intern: OptionalOption<PyObjectRef>,
}
#[pyfunction(name = "ParserCreate")]
fn parser_create(
args: ParserCreateArgs,
vm: &VirtualMachine,
) -> PyResult<PyExpatLikeXmlParserRef> {
let ns_sep = match args.namespace_separator {
Some(ref s) => {
if s.as_str().chars().count() > 1 {
return Err(vm.new_value_error(
"namespace_separator must be at most one character, omitted, or None",
));
}
Some(s.as_str().to_owned())
}
None => None,
};
let _ = args.encoding;
Ok(PyExpatLikeXmlParser::new(ns_sep, args.intern, vm))
}
#[pyattr]
#[pyattr(name = "error")]
#[pyexception(name = "ExpatError", module = "xml.parsers.expat", base = PyException)]
#[derive(Debug)]
#[repr(transparent)]
pub(super) struct PyExpatError(PyException);
#[pyexception]
impl PyExpatError {}
}
#[pymodule(name = "model")]
mod _model {}
#[pymodule(name = "errors")]
mod _errors {}