Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement Number protocol for PyNone#3880

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
youknowone merged 3 commits intoRustPython:mainfromhy-kiera:none-pynumber
Jul 16, 2022

Conversation

hy-kiera
Copy link
Contributor

Implemented based on#3838

Snowapril and moreal reacted with thumbs up emoji
@hy-kierahy-kiera marked this pull request as draftJuly 14, 2022 04:01
@hy-kierahy-kiera marked this pull request as ready for reviewJuly 14, 2022 04:03
@@ -52,6 +54,13 @@ impl PyNone {
}
}

impl AsNumber for PyNone {
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|number, _vm| Ok(self::number_downcast(number).value.is_zero()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

is_zero? the value looking swapped

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

non_bool(), which is ofnone_as_number, returns 0 in cpythonObjects/object.c

static intnone_bool(PyObject *v){    return 0;}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

return zero means false in this case. As ourbool returns false as you know, we just need to return whatbool returns exactly.

hy-kiera reacted with eyes emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

oh, sorry. I was confused with bool.
as you said, it returns false(0). But this code is not simply returning false. make this line to do return false same to the code you shared.

hy-kiera reacted with eyes emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hi. I fixed my code. Could you please review the new commit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks good to me now 😊

@youknowoneyouknowone added the z-ca-2022Tag to track contrubution-academy 2022 labelJul 14, 2022
@@ -39,7 +41,7 @@ impl Constructor for PyNone {
}
}

#[pyimpl(with(Constructor))]
#[pyimpl(flags(BASETYPE),with(Constructor, AsNumber))]
Copy link
Contributor

@SnowaprilSnowaprilJul 14, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
#[pyimpl(flags(BASETYPE),with(Constructor,AsNumber))]
#[pyimpl(with(Constructor,AsNumber))]

It seemsPyNone does not haveBASETYPE flags in cpython. Is there any reason for adding it?

Copy link
Contributor

@SnowaprilSnowaprilJul 14, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

RustPython (by current change)

>>>>>Py_TPFLAGS_BASETYPE=1<<10>>>>>type(None).__flags__&Py_TPFLAGS_BASETYPE1024>>>>>

CPython 3.10

>>>Py_TPFLAGS_BASETYPE=1<<10>>>type(None).__flags__&Py_TPFLAGS_BASETYPE0

hy-kiera reacted with eyes emoji
@@ -39,7 +41,7 @@ impl Constructor for PyNone {
}
}

#[pyimpl(with(Constructor))]
#[pyimpl(flags(BASETYPE),with(Constructor, AsNumber))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is this flag required for PyNone or AsNumber? I don't thinkNone is a BASETYPE.

hy-kiera reacted with eyes emoji
@Snowapril
Copy link
Contributor

Please runcargo fmt --all 😊

@hy-kiera
Copy link
ContributorAuthor

Please runcargo fmt --all 😊

Okay. I'll run it.

@@ -52,6 +52,13 @@ impl PyNone {
}
}

impl AsNumber for PyNone {
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|number, _vm| Ok(Self::number_downcast(number).bool())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
boolean:Some(|number, _vm|Ok(Self::number_downcast(number).bool())),
boolean:Some(|number, _vm|Ok(false)),

None is singleton. I am pretty sure you can skip actual object value.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Good idea. BTW, what is the object's functions such asrepr() andbool() used be for?

Copy link
Contributor

@SnowaprilSnowaprilJul 15, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

They are exposed as python method by#[pymethod] macro attached. You can call them below way.

>>>>>None.__bool__()False>>>>>None.__repr__()'None'

hy-kiera reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

And those functions@Snowapril referred are used by facade functionsbool(None) andrepr(None)

Snowapril reacted with thumbs up emoji
Comment on lines 3 to 4
class::PyClassImpl, convert::ToPyObject,protocol::PyNumberMethods, types::AsNumber,
types::Constructor, Context, Py, PyObjectRef,PyPayload, PyResult, VirtualMachine,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

you can also group type traits

types::{AsNumber,Constructor}

fanninpm and hy-kiera reacted with thumbs up emoji
Copy link
Contributor

@qingshi163qingshi163 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks great

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@youknowoneyouknowoneyouknowone left review comments

@SnowaprilSnowaprilSnowapril left review comments

@qingshi163qingshi163qingshi163 approved these changes

Assignees
No one assigned
Labels
z-ca-2022Tag to track contrubution-academy 2022
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@hy-kiera@Snowapril@youknowone@qingshi163

[8]ページ先頭

©2009-2025 Movatter.jp