Thanks to visit codestin.com
Credit goes to github.com

Skip to content

__new__ doesn't raise TypeError #3692

Open
@rebunto

Description

@rebunto

Feature

<built-in type>.__new__(<other built-in type>) raise TypeError.
(Maybe 'built-in type' is not a exact definition because object.__new__(_frozen_importlib.BuiltinImporter) is valid in CPython.)

CPython:

>>> a = object.__new__(dict)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object.__new__(dict) is not safe, use dict.__new__()

RustPython:

>>>>> a = object.__new__(dict) # This doen't raise TypeError
>>>>> repr(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected payload 'dict' but 'dict' found
>>>>> str(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected payload 'dict' but 'dict' found

Most of cases are above,
but when bool is passed as argument, it is panicked.

>>>>> a = object.__new__(bool)
>>>>> a
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', vm\src\builtins\bool.rs:188:29
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `C:\Users\user\RustPython\target\release\rustpython.exe` (exit code: 101)

Python Documentation

https://github.com/python/cpython/blob/85354ed78c0edb6d81a2bd53cabc85e547b8b26e/Objects/typeobject.c#L7120

Relevant code in RustPython:

pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-stdlibC-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions