Skip to content

Implement _json.encode_basestring{,_ascii} #1957

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 4 commits into from
Jun 6, 2020
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
2 changes: 0 additions & 2 deletions Lib/test/test_json/test_speedups.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def test_scanstring(self):
self.assertEqual(self.json.decoder.scanstring.__module__, "_json")
self.assertIs(self.json.decoder.scanstring, self.json.decoder.c_scanstring)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_encode_basestring_ascii(self):
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
"_json")
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ impl<O: OutputStream> Compiler<O> {
{
return Err(self.error_loc(
CompileErrorType::AsyncReturnValue,
statement.location.clone(),
statement.location,
));
}
self.compile_expression(v)?;
Expand Down
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ libz-sys = "1.0"
winreg = "0.7"
schannel = "0.1"

[target."cfg(windows)".dependencies.winapi]
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["winsock2", "handleapi", "ws2def", "std", "winbase", "wincrypt", "fileapi"]

Expand Down
22 changes: 21 additions & 1 deletion vm/src/dictdatatype.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::obj::objstr::PyString;
use crate::obj::objstr::{PyString, PyStringRef};
use crate::pyhash;
use crate::pyobject::{IdProtocol, IntoPyObject, PyObjectRef, PyResult};
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -438,6 +438,26 @@ impl DictKey for &PyObjectRef {
}
}

impl DictKey for &PyStringRef {
fn do_hash(self, _vm: &VirtualMachine) -> PyResult<HashValue> {
Ok(self.hash())
}

fn do_is(self, other: &PyObjectRef) -> bool {
self.is(other)
}

fn do_eq(self, vm: &VirtualMachine, other_key: &PyObjectRef) -> PyResult<bool> {
if self.is(other_key) {
Ok(true)
} else if let Some(py_str_value) = other_key.payload::<PyString>() {
Ok(py_str_value.as_str() == self.as_str())
} else {
vm.bool_eq(self.clone().into_object(), other_key.clone())
}
}
}

/// Implement trait for the str type, so that we can use strings
/// to index dictionaries.
impl DictKey for &str {
Expand Down
14 changes: 14 additions & 0 deletions vm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,17 @@ macro_rules! class_or_notimplemented {
}
};
}

#[macro_export]
macro_rules! named_function {
($ctx:expr, $module:ident, $func:ident) => {{
paste::expr! {
$crate::pyobject::PyContext::new_function_named(
&$ctx,
[<$module _ $func>],
stringify!($module).to_owned(),
stringify!($func).to_owned(),
)
}
}};
}
45 changes: 40 additions & 5 deletions vm/src/obj/objbuiltinfunc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use crate::function::{OptionalArg, PyFuncArgs, PyNativeFunc};
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol,
Expand All @@ -11,12 +12,15 @@ use crate::vm::VirtualMachine;
#[pyclass]
pub struct PyBuiltinFunction {
value: PyNativeFunc,
module: Option<PyStringRef>,
name: Option<PyStringRef>,
}

impl PyValue for PyBuiltinFunction {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.builtin_function_or_method_type()
}
const HAVE_DICT: bool = true;
}

impl fmt::Debug for PyBuiltinFunction {
Expand All @@ -27,7 +31,19 @@ impl fmt::Debug for PyBuiltinFunction {

impl PyBuiltinFunction {
pub fn new(value: PyNativeFunc) -> Self {
Self { value }
Self {
value,
module: None,
name: None,
}
}

pub fn new_with_name(value: PyNativeFunc, module: PyStringRef, name: PyStringRef) -> Self {
Self {
value,
module: Some(module),
name: Some(name),
}
}

pub fn as_func(&self) -> &PyNativeFunc {
Expand All @@ -42,7 +58,16 @@ impl SlotCall for PyBuiltinFunction {
}

#[pyimpl(with(SlotCall))]
impl PyBuiltinFunction {}
impl PyBuiltinFunction {
#[pyproperty(magic)]
fn module(&self) -> Option<PyStringRef> {
self.module.clone()
}
#[pyproperty(magic)]
fn name(&self) -> Option<PyStringRef> {
self.name.clone()
}
}

#[pyclass]
pub struct PyBuiltinMethod {
Expand All @@ -64,7 +89,12 @@ impl fmt::Debug for PyBuiltinMethod {
impl PyBuiltinMethod {
pub fn new(value: PyNativeFunc) -> Self {
Self {
function: PyBuiltinFunction { value },
function: PyBuiltinFunction::new(value),
}
}
pub fn new_with_name(value: PyNativeFunc, module: PyStringRef, name: PyStringRef) -> Self {
Self {
function: PyBuiltinFunction::new_with_name(value, module, name),
}
}

Expand Down Expand Up @@ -100,9 +130,14 @@ impl SlotCall for PyBuiltinMethod {

#[pyimpl(with(SlotDescriptor, SlotCall))]
impl PyBuiltinMethod {
// TODO: give builtin functions names
#[pyproperty(magic)]
fn name(&self) {}
fn module(&self) -> Option<PyStringRef> {
self.function.module.clone()
}
#[pyproperty(magic)]
fn name(&self) -> Option<PyStringRef> {
self.function.name.clone()
}
}

pub fn init(context: &PyContext) {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl PyString {
}

#[pymethod(name = "__hash__")]
fn hash(&self) -> pyhash::PyHash {
pub(crate) fn hash(&self) -> pyhash::PyHash {
self.hash.load().unwrap_or_else(|| {
let hash = pyhash::hash_value(&self.value);
self.hash.store(Some(hash));
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl PyClassRef {
// Search the bases for the proper metatype to deal with this:
let winner = calculate_meta_class(metatype.clone(), &bases, vm)?;
let metatype = if !winner.is(&metatype) {
#[allow(clippy::redundant_clone)] // false positive
if let Some(ref tp_new) = winner.clone().slots.read().unwrap().new {
// Pass it to the winner

Expand Down
41 changes: 32 additions & 9 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl PyContext {
let exceptions = exceptions::ExceptionZoo::new(&types.type_type, &types.object_type);

fn create_object<T: PyObjectPayload + PyValue>(payload: T, cls: &PyClassRef) -> PyRef<T> {
PyRef::new_ref_unchecked(PyObject::new(payload, cls.clone(), None))
PyRef::from_obj_unchecked(PyObject::new(payload, cls.clone(), None))
}

let none_type = create_type("NoneType", &types.type_type, &types.object_type);
Expand Down Expand Up @@ -492,6 +492,18 @@ impl PyContext {
)
}

pub fn new_function_named<F, T, R, VM>(&self, f: F, module: String, name: String) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R, VM>,
{
let stringref = |s| PyRef::new_ref(objstr::PyString::from(s), self.str_type(), None);
PyObject::new(
PyBuiltinFunction::new_with_name(f.into_func(), stringref(module), stringref(name)),
self.builtin_function_or_method_type(),
None,
)
}

pub fn new_method<F, T, R, VM>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R, VM>,
Expand Down Expand Up @@ -587,7 +599,7 @@ impl PyContext {
bytecode::Constant::Complex { ref value } => self.new_complex(*value),
bytecode::Constant::String { ref value } => self.new_str(value.clone()),
bytecode::Constant::Bytes { ref value } => self.new_bytes(value.clone()),
bytecode::Constant::Boolean { ref value } => self.new_bool(value.clone()),
bytecode::Constant::Boolean { value } => self.new_bool(value),
bytecode::Constant::Code { ref code } => {
self.new_code_object(*code.clone()).into_object()
}
Expand Down Expand Up @@ -695,9 +707,14 @@ impl<T> Clone for PyRef<T> {
}

impl<T: PyValue> PyRef<T> {
fn new_ref(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
#[allow(clippy::new_ret_no_self)]
pub fn new_ref(payload: T, typ: PyClassRef, dict: Option<PyDictRef>) -> Self {
Self::from_obj_unchecked(PyObject::new(payload, typ, dict))
}

fn from_obj(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
if obj.payload_is::<T>() {
Ok(Self::new_ref_unchecked(obj))
Ok(Self::from_obj_unchecked(obj))
} else {
Err(vm.new_runtime_error(format!(
"Unexpected payload for type {:?}",
Expand All @@ -706,7 +723,7 @@ impl<T: PyValue> PyRef<T> {
}
}

pub(crate) fn new_ref_unchecked(obj: PyObjectRef) -> Self {
pub(crate) fn from_obj_unchecked(obj: PyObjectRef) -> Self {
PyRef {
obj,
_payload: PhantomData,
Expand Down Expand Up @@ -747,7 +764,7 @@ where
{
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
if objtype::isinstance(&obj, &T::class(vm)) {
PyRef::new_ref(obj, vm)
PyRef::from_obj(obj, vm)
} else {
let class = T::class(vm);
let expected_type = vm.to_pystr(&class)?;
Expand Down Expand Up @@ -1084,6 +1101,12 @@ impl<T> IntoPyObject for PyRef<T> {
}
}

impl<T> IntoPyObject for &PyRef<T> {
fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult {
Ok(self.obj.clone())
}
}

impl IntoPyObject for PyCallable {
fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult {
Ok(self.into_object())
Expand Down Expand Up @@ -1145,7 +1168,7 @@ where
where
T: PyValue,
{
PyRef::new_ref_unchecked(self as PyObjectRef)
PyRef::from_obj_unchecked(self as PyObjectRef)
}
}

Expand Down Expand Up @@ -1210,7 +1233,7 @@ pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
} else {
Some(vm.ctx.new_dict())
};
PyRef::new_ref(PyObject::new(self, cls, dict), vm)
PyRef::from_obj(PyObject::new(self, cls, dict), vm)
} else {
let subtype = vm.to_str(&cls.obj)?;
let basetype = vm.to_str(&class.obj)?;
Expand All @@ -1219,7 +1242,7 @@ pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
}

fn into_ref_with_type_unchecked(self, cls: PyClassRef, dict: Option<PyDictRef>) -> PyRef<Self> {
PyRef::new_ref_unchecked(PyObject::new(self, cls, dict))
PyRef::from_obj_unchecked(PyObject::new(self, cls, dict))
}
}

Expand Down
1 change: 0 additions & 1 deletion vm/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl Scope {
) -> Scope {
if !globals.contains_key("__builtins__", vm) {
globals
.clone()
.set_item("__builtins__", vm.builtins.clone(), vm)
.unwrap();
}
Expand Down
21 changes: 21 additions & 0 deletions vm/src/stdlib/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::VirtualMachine;
use num_bigint::BigInt;
use std::str::FromStr;

mod machinery;

#[pyclass(name = "Scanner")]
#[derive(Debug)]
struct JsonScanner {
Expand Down Expand Up @@ -209,11 +211,30 @@ impl JsonScanner {
}
}

fn encode_string(s: &str, ascii_only: bool) -> String {
let mut buf = Vec::<u8>::with_capacity(s.len() + 2);
machinery::write_json_string(s, ascii_only, &mut buf)
// writing to a vec can't fail
.unwrap_or_else(|_| unsafe { std::hint::unreachable_unchecked() });
// TODO: verify that the implementation is correct enough to use `from_utf8_unchecked`
String::from_utf8(buf).expect("invalid utf-8 in json output")
}

fn _json_encode_basestring(s: PyStringRef) -> String {
encode_string(s.as_str(), false)
}

fn _json_encode_basestring_ascii(s: PyStringRef) -> String {
encode_string(s.as_str(), true)
}

pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
let scanner_cls = JsonScanner::make_class(ctx);
scanner_cls.set_str_attr("__module__", vm.new_str("_json".to_owned()));
py_module!(vm, "_json", {
"make_scanner" => scanner_cls,
"encode_basestring" => named_function!(ctx, _json, encode_basestring),
"encode_basestring_ascii" => named_function!(ctx, _json, encode_basestring_ascii),
})
}
Loading