Skip to content

Commit

Permalink
pythongh-107526: Revert "pythongh-100357: Convert several functions i…
Browse files Browse the repository at this point in the history
…n bltinsmodule to AC"
  • Loading branch information
sobolevn committed Aug 1, 2023
1 parent 6ef8f8c commit db35ad9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 304 deletions.
208 changes: 99 additions & 109 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,33 +842,31 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
return result;
}

/*[clinic input]
dir as builtin_dir
arg: object = NULL
/
Show attributes of an object.
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
[clinic start generated code]*/

/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_dir_impl(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=24f2c7a52c1e3b08 input=ed6d6ccb13d52251]*/
builtin_dir(PyObject *self, PyObject *args)
{
PyObject *arg = NULL;

if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
return NULL;
return PyObject_Dir(arg);
}

PyDoc_STRVAR(dir_doc,
"dir([object]) -> list of strings\n"
"\n"
"If called without an argument, return the names in the current scope.\n"
"Else, return an alphabetized list of names comprising (some of) the attributes\n"
"of the given object, and of attributes reachable from it.\n"
"If the object supplies a method named __dir__, it will be used; otherwise\n"
"the default dir() logic is used and returns:\n"
" for a module object: the module's attributes.\n"
" for a class object: its attributes, and recursively the attributes\n"
" of its bases.\n"
" for any other object: its attributes, its class's attributes, and\n"
" recursively the attributes of its class's base classes.");

/*[clinic input]
divmod as builtin_divmod
Expand Down Expand Up @@ -1138,39 +1136,36 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
}


/*[clinic input]
getattr as builtin_getattr
object: object
name: object
default: object = NULL
/
Get a named attribute from an object.
getattr(x, 'y') is equivalent to x.y
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
[clinic start generated code]*/

/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_getattr_impl(PyObject *module, PyObject *object, PyObject *name,
PyObject *default_value)
/*[clinic end generated code: output=74ad0e225e3f701c input=d7562cd4c3556171]*/
builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *result;
PyObject *v, *name, *result;

if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
return NULL;

if (default_value != NULL) {
if (PyObject_GetOptionalAttr(object, name, &result) == 0) {
return Py_NewRef(default_value);
v = args[0];
name = args[1];
if (nargs > 2) {
if (PyObject_GetOptionalAttr(v, name, &result) == 0) {
PyObject *dflt = args[2];
return Py_NewRef(dflt);
}
}
else {
result = PyObject_GetAttr(object, name);
result = PyObject_GetAttr(v, name);
}
return result;
}

PyDoc_STRVAR(getattr_doc,
"getattr(object, name[, default]) -> value\n\
\n\
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
When a default argument is given, it is returned when the attribute doesn't\n\
exist; without it, an exception is raised in that case.");


/*[clinic input]
globals as builtin_globals
Expand Down Expand Up @@ -1482,43 +1477,34 @@ PyTypeObject PyMap_Type = {
};


/*[clinic input]
next as builtin_next
iterator: object
default: object = NULL
/
Return the next item from the iterator.
If default is given and the iterator is exhausted,
it is returned instead of raising StopIteration.
[clinic start generated code]*/

/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_next_impl(PyObject *module, PyObject *iterator,
PyObject *default_value)
/*[clinic end generated code: output=a38a94eeb447fef9 input=180f9984f182020f]*/
builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *res;
PyObject *it, *res;

if (!_PyArg_CheckPositional("next", nargs, 1, 2))
return NULL;

if (!PyIter_Check(iterator)) {
it = args[0];
if (!PyIter_Check(it)) {
PyErr_Format(PyExc_TypeError,
"'%.200s' object is not an iterator",
Py_TYPE(iterator)->tp_name);
Py_TYPE(it)->tp_name);
return NULL;
}

res = (*Py_TYPE(iterator)->tp_iternext)(iterator);
res = (*Py_TYPE(it)->tp_iternext)(it);
if (res != NULL) {
return res;
} else if (default_value != NULL) {
} else if (nargs > 1) {
PyObject *def = args[1];
if (PyErr_Occurred()) {
if(!PyErr_ExceptionMatches(PyExc_StopIteration))
return NULL;
PyErr_Clear();
}
return Py_NewRef(default_value);
return Py_NewRef(def);
} else if (PyErr_Occurred()) {
return NULL;
} else {
Expand All @@ -1527,6 +1513,12 @@ builtin_next_impl(PyObject *module, PyObject *iterator,
}
}

PyDoc_STRVAR(next_doc,
"next(iterator[, default])\n\
\n\
Return the next item from the iterator. If default is given and the iterator\n\
is exhausted, it is returned instead of raising StopIteration.");


/*[clinic input]
setattr as builtin_setattr
Expand Down Expand Up @@ -1620,33 +1612,34 @@ builtin_hex(PyObject *module, PyObject *number)
}


/*[clinic input]
iter as builtin_iter
object: object
sentinel: object = NULL
/
Get an iterator from an object.
In the first form, the argument must supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
[clinic start generated code]*/

/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_iter_impl(PyObject *module, PyObject *object, PyObject *sentinel)
/*[clinic end generated code: output=12cf64203c195a94 input=a5d64d9d81880ba6]*/
builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
if (sentinel == NULL)
return PyObject_GetIter(object);
if (!PyCallable_Check(object)) {
PyObject *v;

if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
return NULL;
v = args[0];
if (nargs == 1)
return PyObject_GetIter(v);
if (!PyCallable_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"iter(object, sentinel): object must be callable");
"iter(v, w): v must be callable");
return NULL;
}
return PyCallIter_New(object, sentinel);
PyObject *sentinel = args[1];
return PyCallIter_New(v, sentinel);
}

PyDoc_STRVAR(iter_doc,
"iter(iterable) -> iterator\n\
iter(callable, sentinel) -> iterator\n\
\n\
Get an iterator from an object. In the first form, the argument must\n\
supply its own iterator, or be a sequence.\n\
In the second form, the callable is called until it returns the sentinel.");


/*[clinic input]
aiter as builtin_aiter
Expand Down Expand Up @@ -2444,36 +2437,33 @@ builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
}


/*[clinic input]
vars as builtin_vars
object: object = NULL
/
Show vars.
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
[clinic start generated code]*/

/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_vars_impl(PyObject *module, PyObject *object)
/*[clinic end generated code: output=840a7f64007a3e0a input=80cbdef9182c4ba3]*/
builtin_vars(PyObject *self, PyObject *args)
{
PyObject *v = NULL;
PyObject *d;

if (object == NULL) {
d = _PyEval_GetFrameLocals();
if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
return NULL;
if (v == NULL) {
d = Py_XNewRef(PyEval_GetLocals());
}
else {
if (PyObject_GetOptionalAttr(object, &_Py_ID(__dict__), &d) == 0) {
if (PyObject_GetOptionalAttr(v, &_Py_ID(__dict__), &d) == 0) {
PyErr_SetString(PyExc_TypeError,
"vars() argument must have __dict__ attribute");
}
}
return d;
}

PyDoc_STRVAR(vars_doc,
"vars([object]) -> dictionary\n\
\n\
Without arguments, equivalent to locals().\n\
With an argument, equivalent to object.__dict__.");


/*[clinic input]
sum as builtin_sum
Expand Down Expand Up @@ -3022,12 +3012,12 @@ static PyMethodDef builtin_methods[] = {
BUILTIN_CHR_METHODDEF
BUILTIN_COMPILE_METHODDEF
BUILTIN_DELATTR_METHODDEF
BUILTIN_DIR_METHODDEF
{"dir", builtin_dir, METH_VARARGS, dir_doc},
BUILTIN_DIVMOD_METHODDEF
BUILTIN_EVAL_METHODDEF
BUILTIN_EXEC_METHODDEF
BUILTIN_FORMAT_METHODDEF
BUILTIN_GETATTR_METHODDEF
{"getattr", _PyCFunction_CAST(builtin_getattr), METH_FASTCALL, getattr_doc},
BUILTIN_GLOBALS_METHODDEF
BUILTIN_HASATTR_METHODDEF
BUILTIN_HASH_METHODDEF
Expand All @@ -3036,13 +3026,13 @@ static PyMethodDef builtin_methods[] = {
BUILTIN_INPUT_METHODDEF
BUILTIN_ISINSTANCE_METHODDEF
BUILTIN_ISSUBCLASS_METHODDEF
BUILTIN_ITER_METHODDEF
{"iter", _PyCFunction_CAST(builtin_iter), METH_FASTCALL, iter_doc},
BUILTIN_AITER_METHODDEF
BUILTIN_LEN_METHODDEF
BUILTIN_LOCALS_METHODDEF
{"max", _PyCFunction_CAST(builtin_max), METH_VARARGS | METH_KEYWORDS, max_doc},
{"min", _PyCFunction_CAST(builtin_min), METH_VARARGS | METH_KEYWORDS, min_doc},
BUILTIN_NEXT_METHODDEF
{"next", _PyCFunction_CAST(builtin_next), METH_FASTCALL, next_doc},
BUILTIN_ANEXT_METHODDEF
BUILTIN_OCT_METHODDEF
BUILTIN_ORD_METHODDEF
Expand All @@ -3053,7 +3043,7 @@ static PyMethodDef builtin_methods[] = {
BUILTIN_SETATTR_METHODDEF
BUILTIN_SORTED_METHODDEF
BUILTIN_SUM_METHODDEF
BUILTIN_VARS_METHODDEF
{"vars", builtin_vars, METH_VARARGS, vars_doc},
{NULL, NULL},
};

Expand Down
Loading

0 comments on commit db35ad9

Please sign in to comment.