Skip to content

PyPayload::class takes ctx instead of vm #4731

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

Merged
merged 1 commit into from
Mar 21, 2023
Merged
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
4 changes: 2 additions & 2 deletions derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre

// We need this to make extend mechanism work:
impl ::rustpython_vm::PyPayload for #class_name {
fn class(vm: &::rustpython_vm::VirtualMachine) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
vm.ctx.exceptions.#ctx_name
fn class(ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
ctx.exceptions.#ctx_name
}
}

Expand Down
2 changes: 1 addition & 1 deletion derive-impl/src/pypayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) fn impl_pypayload(input: DeriveInput) -> Result<TokenStream> {

let ret = quote! {
impl ::rustpython_vm::PyPayload for #ty {
fn class(_vm: &::rustpython_vm::VirtualMachine) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
<Self as ::rustpython_vm::class::StaticType>::static_type()
}
}
Expand Down
10 changes: 5 additions & 5 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ mod array {
)
})?;

if cls.is(PyArray::class(vm)) && !kwargs.is_empty() {
if cls.is(PyArray::class(&vm.ctx)) && !kwargs.is_empty() {
return Err(
vm.new_type_error("array.array() takes no keyword arguments".to_owned())
);
Expand Down Expand Up @@ -952,7 +952,7 @@ mod array {
let bytes = bytes.get_bytes();

for b in bytes.chunks(BLOCKSIZE) {
let b = PyBytes::from(b.to_vec()).into_ref(vm);
let b = PyBytes::from(b.to_vec()).into_ref(&vm.ctx);
vm.call_method(&f, "write", (b,))?;
}
Ok(())
Expand Down Expand Up @@ -1072,7 +1072,7 @@ mod array {
if let Some(other) = other.payload::<PyArray>() {
self.read()
.add(&other.read(), vm)
.map(|array| PyArray::from(array).into_ref(vm))
.map(|array| PyArray::from(array).into_ref(&vm.ctx))
} else {
Err(vm.new_type_error(format!(
"can only append array (not \"{}\") to array",
Expand Down Expand Up @@ -1105,7 +1105,7 @@ mod array {
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
self.read()
.mul(value, vm)
.map(|x| Self::from(x).into_ref(vm))
.map(|x| Self::from(x).into_ref(&vm.ctx))
}

#[pymethod(magic)]
Expand Down Expand Up @@ -1565,7 +1565,7 @@ mod array {
}

fn check_array_type(typ: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyTypeRef> {
if !typ.fast_issubclass(PyArray::class(vm)) {
if !typ.fast_issubclass(PyArray::class(&vm.ctx)) {
return Err(
vm.new_type_error(format!("{} is not a subtype of array.array", typ.name()))
);
Expand Down
20 changes: 10 additions & 10 deletions stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ mod mmap {
MmapObj::Write(mmap) => mmap[pos..end_pos].to_vec(),
};

let result = PyBytes::from(bytes).into_ref(vm);
let result = PyBytes::from(bytes).into_ref(&vm.ctx);

self.advance_pos(num_bytes);

Expand All @@ -763,7 +763,7 @@ mod mmap {

self.advance_pos(1);

Ok(PyInt::from(b).into_ref(vm))
Ok(PyInt::from(b).into_ref(&vm.ctx))
}

#[pymethod]
Expand All @@ -773,7 +773,7 @@ mod mmap {

let remaining = self.len().saturating_sub(pos);
if remaining == 0 {
return Ok(PyBytes::from(vec![]).into_ref(vm));
return Ok(PyBytes::from(vec![]).into_ref(&vm.ctx));
}

let eof = match mmap.as_ref().unwrap() {
Expand All @@ -794,7 +794,7 @@ mod mmap {
MmapObj::Write(mmap) => mmap[pos..end_pos].to_vec(),
};

let result = PyBytes::from(bytes).into_ref(vm);
let result = PyBytes::from(bytes).into_ref(&vm.ctx);

self.advance_pos(end_pos - pos);

Expand Down Expand Up @@ -856,7 +856,7 @@ mod mmap {
Err(e) => return Err(vm.new_os_error(e.to_string())),
};

Ok(PyInt::from(file_len).into_ref(vm))
Ok(PyInt::from(file_len).into_ref(&vm.ctx))
}

#[pymethod]
Expand Down Expand Up @@ -884,7 +884,7 @@ mod mmap {

self.advance_pos(len);

Ok(PyInt::from(len).into_ref(vm))
Ok(PyInt::from(len).into_ref(&vm.ctx))
}

#[pymethod]
Expand Down Expand Up @@ -945,7 +945,7 @@ mod mmap {
MmapObj::Write(mmap) => mmap[i],
};

Ok(PyInt::from(b).into_ref(vm).into())
Ok(PyInt::from(b).into_ref(&vm.ctx).into())
}

fn getitem_by_slice(
Expand All @@ -958,13 +958,13 @@ mod mmap {
let mmap = self.check_valid(vm)?;

if slice_len == 0 {
return Ok(PyBytes::from(vec![]).into_ref(vm).into());
return Ok(PyBytes::from(vec![]).into_ref(&vm.ctx).into());
} else if step == 1 {
let bytes = match mmap.deref().as_ref().unwrap() {
MmapObj::Read(mmap) => &mmap[range],
MmapObj::Write(mmap) => &mmap[range],
};
return Ok(PyBytes::from(bytes.to_vec()).into_ref(vm).into());
return Ok(PyBytes::from(bytes.to_vec()).into_ref(&vm.ctx).into());
}

let mut result_buf = Vec::with_capacity(slice_len);
Expand All @@ -985,7 +985,7 @@ mod mmap {
result_buf.push(b);
}
}
Ok(PyBytes::from(result_buf).into_ref(vm).into())
Ok(PyBytes::from(result_buf).into_ref(&vm.ctx).into())
}

fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod _pyexpat {
entity_decl: MutableObject::new(vm.ctx.none()),
buffer_text: MutableObject::new(vm.ctx.new_bool(false).into()),
}
.into_ref(vm))
.into_ref(&vm.ctx))
}

#[extend_class]
Expand Down Expand Up @@ -118,15 +118,15 @@ mod _pyexpat {
.unwrap();
}

let name_str = PyStr::from(name.local_name).into_ref(vm);
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
invoke_handler(vm, &self.start_element, (name_str, dict));
}
Ok(XmlEvent::EndElement { name, .. }) => {
let name_str = PyStr::from(name.local_name).into_ref(vm);
let name_str = PyStr::from(name.local_name).into_ref(&vm.ctx);
invoke_handler(vm, &self.end_element, (name_str,));
}
Ok(XmlEvent::Characters(chars)) => {
let str = PyStr::from(chars).into_ref(vm);
let str = PyStr::from(chars).into_ref(&vm.ctx);
invoke_handler(vm, &self.character_data, (str,));
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) mod _struct {
b @ PyBytes => if b.is_ascii() {
Some(unsafe {
PyStr::new_ascii_unchecked(b.as_bytes().to_vec())
}.into_ref(vm))
}.into_ref(&vm.ctx))
} else {
None
},
Expand Down
46 changes: 25 additions & 21 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ mod _sqlite {
isolation_level: Option<PyStrRef>,
#[pyarg(any, default = "true")]
check_same_thread: bool,
#[pyarg(any, default = "Connection::class(vm).to_owned()")]
#[pyarg(any, default = "Connection::class(&vm.ctx).to_owned()")]
factory: PyTypeRef,
// TODO: cache statements
#[allow(dead_code)]
Expand Down Expand Up @@ -640,14 +640,14 @@ mod _sqlite {

#[pyfunction]
fn register_adapter(typ: PyTypeRef, adapter: ArgCallable, vm: &VirtualMachine) -> PyResult<()> {
if typ.is(PyInt::class(vm))
|| typ.is(PyFloat::class(vm))
|| typ.is(PyStr::class(vm))
|| typ.is(PyByteArray::class(vm))
if typ.is(PyInt::class(&vm.ctx))
|| typ.is(PyFloat::class(&vm.ctx))
|| typ.is(PyStr::class(&vm.ctx))
|| typ.is(PyByteArray::class(&vm.ctx))
{
let _ = BASE_TYPE_ADAPTED.set(());
}
let protocol = PrepareProtocol::class(vm).to_owned();
let protocol = PrepareProtocol::class(&vm.ctx).to_owned();
let key = vm.ctx.new_tuple(vec![typ.into(), protocol.into()]);
adapters().set_item(key.as_object(), adapter.into(), vm)
}
Expand Down Expand Up @@ -708,7 +708,7 @@ mod _sqlite {
// TODO: None proto
let proto = proto
.flatten()
.unwrap_or_else(|| PrepareProtocol::class(vm).to_owned());
.unwrap_or_else(|| PrepareProtocol::class(&vm.ctx).to_owned());

_adapt(
&obj,
Expand Down Expand Up @@ -818,7 +818,7 @@ mod _sqlite {

fn call(zelf: &Py<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult {
if let Some(stmt) = Statement::new(zelf, &args.0, vm)? {
Ok(stmt.into_ref(vm).into())
Ok(stmt.into_ref(&vm.ctx).into())
} else {
Ok(vm.ctx.none())
}
Expand All @@ -835,7 +835,7 @@ mod _sqlite {
if let Some(isolation_level) = &args.isolation_level {
begin_statement_ptr_from_isolation_level(isolation_level, vm)?;
}
let text_factory = PyStr::class(vm).to_owned().into_object();
let text_factory = PyStr::class(&vm.ctx).to_owned().into_object();

Ok(Self {
db: PyMutex::new(Some(db)),
Expand Down Expand Up @@ -884,7 +884,7 @@ mod _sqlite {
cursor
} else {
let row_factory = zelf.row_factory.to_owned();
Cursor::new(zelf, row_factory, vm).into_ref(vm)
Cursor::new(zelf, row_factory, vm).into_ref(&vm.ctx)
};
Ok(cursor)
}
Expand Down Expand Up @@ -921,7 +921,7 @@ mod _sqlite {
connection: zelf,
inner: PyMutex::new(Some(BlobInner { blob, offset: 0 })),
};
Ok(blob.into_ref(vm))
Ok(blob.into_ref(&vm.ctx))
}

#[pymethod]
Expand Down Expand Up @@ -954,7 +954,7 @@ mod _sqlite {
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
let row_factory = zelf.row_factory.to_owned();
let cursor = Cursor::new(zelf, row_factory, vm).into_ref(vm);
let cursor = Cursor::new(zelf, row_factory, vm).into_ref(&vm.ctx);
Cursor::execute(cursor, sql, parameters, vm)
}

Expand All @@ -966,7 +966,7 @@ mod _sqlite {
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
let row_factory = zelf.row_factory.to_owned();
let cursor = Cursor::new(zelf, row_factory, vm).into_ref(vm);
let cursor = Cursor::new(zelf, row_factory, vm).into_ref(&vm.ctx);
Cursor::executemany(cursor, sql, seq_of_params, vm)
}

Expand All @@ -977,7 +977,11 @@ mod _sqlite {
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
let row_factory = zelf.row_factory.to_owned();
Cursor::executescript(Cursor::new(zelf, row_factory, vm).into_ref(vm), script, vm)
Cursor::executescript(
Cursor::new(zelf, row_factory, vm).into_ref(&vm.ctx),
script,
vm,
)
}

#[pymethod]
Expand Down Expand Up @@ -1403,7 +1407,7 @@ mod _sqlite {
drop(inner);
return Ok(zelf);
};
let stmt = stmt.into_ref(vm);
let stmt = stmt.into_ref(&vm.ctx);

inner.rowcount = if stmt.is_dml { 0 } else { -1 };

Expand Down Expand Up @@ -1472,7 +1476,7 @@ mod _sqlite {
drop(inner);
return Ok(zelf);
};
let stmt = stmt.into_ref(vm);
let stmt = stmt.into_ref(&vm.ctx);

let st = stmt.lock();

Expand Down Expand Up @@ -1724,15 +1728,15 @@ mod _sqlite {

let text_factory = zelf.connection.text_factory.to_owned();

if text_factory.is(PyStr::class(vm)) {
if text_factory.is(PyStr::class(&vm.ctx)) {
let text = String::from_utf8(text).map_err(|_| {
new_operational_error(vm, "not valid UTF-8".to_owned())
})?;
vm.ctx.new_str(text).into()
} else if text_factory.is(PyBytes::class(vm)) {
} else if text_factory.is(PyBytes::class(&vm.ctx)) {
vm.ctx.new_bytes(text).into()
} else if text_factory.is(PyByteArray::class(vm)) {
PyByteArray::from(text).into_ref(vm).into()
} else if text_factory.is(PyByteArray::class(&vm.ctx)) {
PyByteArray::from(text).into_ref(&vm.ctx).into()
} else {
let bytes = vm.ctx.new_bytes(text);
text_factory.call((bytes,), vm)?
Expand Down Expand Up @@ -2531,7 +2535,7 @@ mod _sqlite {
let obj = if need_adapt(parameter, vm) {
adapted = _adapt(
parameter,
PrepareProtocol::class(vm).to_owned(),
PrepareProtocol::class(&vm.ctx).to_owned(),
|x| Ok(x.to_owned()),
vm,
)?;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ mod windows {
oids.into_iter().map(|oid| vm.ctx.new_str(oid).into()),
)
.unwrap()
.into_ref(vm)
.into_ref(&vm.ctx)
.into(),
};
Ok(vm.new_tuple((cert, enc_type, usage)).into())
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod syslog {
Some(value) => &argv[value..],
None => argv,
})
.into_ref(vm),
.into_ref(&vm.ctx),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let module = unicodedata::make_module(vm);

let ucd: PyObjectRef = unicodedata::Ucd::new(unic_ucd_age::UNICODE_VERSION)
.into_ref(vm)
.into_ref(&vm.ctx)
.into();

for attr in [
Expand Down Expand Up @@ -208,7 +208,7 @@ mod unicodedata {
micro: 0,
},
}
.into_ref(vm)
.into_ref(&vm.ctx)
}

#[pyattr]
Expand Down
Loading