Skip to content

Conversation

@wetor
Copy link
Contributor

@wetorwetor commented Jun 3, 2023

all: support filter builtin feature and fix iterable object

Python-3.4.9/Doc/c-api/iterator.rst

Python provides two general-purpose iterator objects. The first, a sequence
iterator, works with an arbitrary sequence supporting the :meth:__getitem__
method. The second works with a callable object and a sentinel value, calling
the callable for each item in the sequence, and ending the iteration when the
sentinel value is returned.

Python-3.4.9/Lib/test/test_builtin.py

classSquares: def__init__(self, max): self.max=maxself.sofar= [] def__len__(self): returnlen(self.sofar) def__getitem__(self, i): ifnot0<=i<self.max: raiseIndexErrorn=len(self.sofar) whilen<=i: self.sofar.append(n*n) n+=1returnself.sofar[i]

In CPython, Squares (5) is an iterable object, and to support this type of iterable object, I made modifications to Iterator. go


Iterator. go

typeIteratorstruct{PosintSeqObject } // ...func (it*Iterator) M__next__() (resObject, errerror){iftuple, ok:=it.Seq.(Tuple); ok{ifit.Pos>=len(tuple){returnnil, StopIteration } res=tuple[it.Pos] it.Pos++returnres, nil } index:=Int(it.Pos) ifI, ok:=it.Seq.(I__getitem__); ok{res, err=I.M__getitem__(index) } elseifres, ok, err=TypeCall1(it.Seq, "__getitem__", index); !ok{returnnil, ExceptionNewf(TypeError, "'%s' object is not iterable", it.Type().Name) } iferr!=nil{ifIsException(IndexError, err){returnnil, StopIteration } returnnil, err } it.Pos++returnres, nil }

Corresponding CPython code
Python-3.4.9/Objects/iterobject.c

staticPyObject*iter_iternext(PyObject*iterator){seqiterobject*it; PyObject*seq; PyObject*result; assert(PySeqIter_Check(iterator)); it= (seqiterobject*)iterator; seq=it->it_seq; if (seq==NULL) returnNULL; if (it->it_index==PY_SSIZE_T_MAX){PyErr_SetString(PyExc_OverflowError, "iter index too large"); returnNULL} result=PySequence_GetItem(seq, it->it_index); if (result!=NULL){it->it_index++; returnresult} if (PyErr_ExceptionMatches(PyExc_IndexError) ||PyErr_ExceptionMatches(PyExc_StopIteration)){PyErr_Clear(); Py_DECREF(seq); it->it_seq=NULL} returnNULL}

and Iter()

funcIter(selfObject) (resObject, errerror){ifI, ok:=self.(I__iter__); ok{returnI.M__iter__() } elseifres, ok, err=TypeCall0(self, "__iter__"); ok{returnres, err } ifObjectIsSequence(self){returnNewIterator(self), nil } returnnil, ExceptionNewf(TypeError, "'%s' object is not iterable", self.Type().Name) }

Corresponding CPython code
Python-3.4.9/Objects/abstract.c

PyObject*PyObject_GetIter(PyObject*o){PyTypeObject*t=o->ob_type; getiterfuncf=NULL; f=t->tp_iter; if (f==NULL){if (PySequence_Check(o)) returnPySeqIter_New(o); returntype_error("'%.200s' object is not iterable", o)} else{PyObject*res= (*f)(o); if (res!=NULL&& !PyIter_Check(res)){PyErr_Format(PyExc_TypeError, "iter() returned non-iterator ""of type '%.100s'", res->ob_type->tp_name); Py_DECREF(res); res=NULL} returnres} }

Finally, filter and map methods were fully implemented

py/tests/filter.py is a copy of Python-3.4.9/Lib/test/test_builtin.py:BuiltinTest.test_filter()
and
py/tests/map.py is a copy of Python-3.4.9/Lib/test/test_builtin.py:BuiltinTest.test_map()

builtin: Implement oct and optimise hex

Through benchmark testing, the speed of hex has indeed been improved

My English is not very good, so forgive me for not providing sufficient explanations. Welcome to ask questions here

@codecov
Copy link

codecovbot commented Jun 3, 2023

Codecov Report

Patch coverage: 83.13% and project coverage change: +0.10 🎉

Comparison is base (acd458b) 74.42% compared to head (04a9c5b) 74.52%.

Additional details and impacted files
@@ Coverage Diff @@## main #222 +/- ## ========================================== + Coverage 74.42% 74.52% +0.10%  ========================================== Files 76 78 +2 Lines 12675 12804 +129 ========================================== + Hits 9433 9542 +109 - Misses 2567 2583 +16 - Partials 675 679 +4 
Impacted FilesCoverage Δ
py/arithmetic.go61.04% <ø> (-0.18%)⬇️
py/list.go64.16% <60.00%> (+0.03%)⬆️
stdlib/builtin/builtin.go78.21% <63.79%> (-1.86%)⬇️
py/iterator.go86.95% <88.88%> (-3.96%)⬇️
py/filter.go92.59% <92.59%> (ø)
py/object.go89.18% <93.93%> (+22.52%)⬆️
py/dict.go82.01% <100.00%> (ø)
py/exception.go64.00% <100.00%> (+3.79%)⬆️
py/internal.go57.14% <100.00%> (+1.58%)⬆️
py/map.go100.00% <100.00%> (ø)
... and 1 more

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@sbinetsbinet left a comment

Choose a reason for hiding this comment

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

thanks for the PR.

I have mostly cosmetics comments, see below.

@wetorwetorforce-pushed the main branch 3 times, most recently from 4979c19 to b184a43CompareJune 6, 2023 03:34
Copy link
Member

@sbinetsbinet left a comment

Choose a reason for hiding this comment

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

LGTM

could you split the commits between implementing filter, implementing map, implementing oct and optimising hex ?
(it's good practice and brings easier to understand git-history)
if not, I'll do it, once @ncw chimed in about ObjectIsTrue.

@wetor
Copy link
ContributorAuthor

wetor commented Jun 7, 2023

Of course, I will split commit later

@wetorwetorforce-pushed the main branch 3 times, most recently from f364ef9 to 8681126CompareJune 7, 2023 11:52
@wetor
Copy link
ContributorAuthor

wetor commented Jun 8, 2023

I found that the basic types (int, string, etc.) did not run Ready() correctly, resulting some attribute was not registered correctly and was fixed by the way
b4b7ef2

Copy link
Member

@sbinetsbinet left a comment

Choose a reason for hiding this comment

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

LGTM, modulo a very minor nit.

thanks again.

Copy link
Member

@sbinetsbinet left a comment

Choose a reason for hiding this comment

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

LGTM.

thanks again.

@sbinetsbinet merged commit 7102b79 into go-python:mainJun 8, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@wetor@sbinet