GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — all: fix iterable object, implement `filter`, `map`, `oct` and optimise `hex` by wetor · Pull Request #222 · go-python/gpython Conversation
Contributor
all: support filter builtin feature and fix iterable objectPython-3.4.9/Doc/c-api/iterator.rst
class Squares: def __init__(self, max): self.max = max self.sofar = [] def __len__(self): return len(self.sofar) def __getitem__(self, i): if not 0 <= i < self.max: raise IndexError n = len(self.sofar) while n <= i: self.sofar.append(n*n) n += 1 return self.sofar[i] In CPython, Iterator. go type Iterator struct { Pos int Seq Object } // ... func (it *Iterator) M__next__() (res Object, err error) { if tuple, ok := it.Seq.(Tuple); ok { if it.Pos >= len(tuple) { return nil, StopIteration } res = tuple[it.Pos] it.Pos++ return res, nil } index := Int(it.Pos) if I, ok := it.Seq.(I__getitem__); ok { res, err = I.M__getitem__(index) } else if res, ok, err = TypeCall1(it.Seq, "__getitem__", index); !ok { return nil, ExceptionNewf(TypeError, "'%s' object is not iterable", it.Type().Name) } if err != nil { if IsException(IndexError, err) { return nil, StopIteration } return nil, err } it.Pos++ return res, nil } Corresponding CPython code static PyObject * iter_iternext(PyObject *iterator) { seqiterobject *it; PyObject *seq; PyObject *result; assert(PySeqIter_Check(iterator)); it = (seqiterobject *)iterator; seq = it->it_seq; if (seq == NULL) return NULL; if (it->it_index == PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_OverflowError, "iter index too large"); return NULL; } result = PySequence_GetItem(seq, it->it_index); if (result != NULL) { it->it_index++; return result; } if (PyErr_ExceptionMatches(PyExc_IndexError) || PyErr_ExceptionMatches(PyExc_StopIteration)) { PyErr_Clear(); Py_DECREF(seq); it->it_seq = NULL; } return NULL; } and func Iter(self Object) (res Object, err error) { if I, ok := self.(I__iter__); ok { return I.M__iter__() } else if res, ok, err = TypeCall0(self, "__iter__"); ok { return res, err } if ObjectIsSequence(self) { return NewIterator(self), nil } return nil, ExceptionNewf(TypeError, "'%s' object is not iterable", self.Type().Name) } Corresponding CPython code PyObject * PyObject_GetIter(PyObject *o) { PyTypeObject *t = o->ob_type; getiterfunc f = NULL; f = t->tp_iter; if (f == NULL) { if (PySequence_Check(o)) return PySeqIter_New(o); return type_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; } return res; } } Finally,
builtin: Implement oct and optimise hexThrough benchmark testing, the speed of My English is not very good, so forgive me for not providing sufficient explanations. Welcome to ask questions here
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
Codecov ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
4979c19 to
b184a43
Compare
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Of course, I will split commit later |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f364ef9 to
8681126
Compare
There was an error while loading. Please reload this page.
|
I found that the basic types (int, string, etc.) did not run |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM, modulo a very minor nit.
thanks again.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
thanks again.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.