mirror of
https://github.com/python/cpython.git
synced 2025-10-21 22:22:48 +00:00
Made dir() more robust.
Added hex() and oct().
This commit is contained in:
parent
9eb4f535aa
commit
006bcd42ac
1 changed files with 57 additions and 6 deletions
|
@ -37,6 +37,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include "ceval.h"
|
#include "ceval.h"
|
||||||
#include "modsupport.h"
|
#include "modsupport.h"
|
||||||
|
|
||||||
|
/* Should be in longobject.h */
|
||||||
|
extern stringobject *long_format PROTO((object *, int));
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_abs(self, v)
|
builtin_abs(self, v)
|
||||||
object *self;
|
object *self;
|
||||||
|
@ -58,7 +61,7 @@ builtin_chr(self, v)
|
||||||
long x;
|
long x;
|
||||||
char s[1];
|
char s[1];
|
||||||
if (v == NULL || !is_intobject(v)) {
|
if (v == NULL || !is_intobject(v)) {
|
||||||
err_setstr(TypeError, "chr() must have int argument");
|
err_setstr(TypeError, "chr() requires int argument");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
x = getintvalue(v);
|
x = getintvalue(v);
|
||||||
|
@ -84,20 +87,20 @@ builtin_dir(self, v)
|
||||||
d = getattr(v, "__dict__");
|
d = getattr(v, "__dict__");
|
||||||
if (d == NULL) {
|
if (d == NULL) {
|
||||||
err_setstr(TypeError,
|
err_setstr(TypeError,
|
||||||
"dir() argument has no variable attributes");
|
"dir() argument must have __dict__ attribute");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!is_dictobject(d)) { /* Assume it is None */
|
if (is_dictobject(d)) {
|
||||||
v = newlistobject(0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
v = getdictkeys(d);
|
v = getdictkeys(d);
|
||||||
if (sortlist(v) != 0) {
|
if (sortlist(v) != 0) {
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
v = NULL;
|
v = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
v = newlistobject(0);
|
||||||
|
}
|
||||||
DECREF(d);
|
DECREF(d);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
@ -194,6 +197,29 @@ builtin_float(self, v)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
builtin_hex(self, v)
|
||||||
|
object *self;
|
||||||
|
object *v;
|
||||||
|
{
|
||||||
|
if (v != NULL) {
|
||||||
|
if (is_intobject(v)) {
|
||||||
|
char buf[20];
|
||||||
|
long x = getintvalue(v);
|
||||||
|
if (x >= 0)
|
||||||
|
sprintf(buf, "0x%lx", x);
|
||||||
|
else
|
||||||
|
sprintf(buf, "-0x%lx", -x);
|
||||||
|
return newstringobject(buf);
|
||||||
|
}
|
||||||
|
if (is_longobject(v)) {
|
||||||
|
return (object *) long_format(v, 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err_setstr(TypeError, "hex() requires int/long argument");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_input(self, v)
|
builtin_input(self, v)
|
||||||
object *self;
|
object *self;
|
||||||
|
@ -342,6 +368,29 @@ builtin_max(self, v)
|
||||||
return min_max(v, 1);
|
return min_max(v, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
builtin_oct(self, v)
|
||||||
|
object *self;
|
||||||
|
object *v;
|
||||||
|
{
|
||||||
|
if (v != NULL) {
|
||||||
|
if (is_intobject(v)) {
|
||||||
|
char buf[20];
|
||||||
|
long x = getintvalue(v);
|
||||||
|
if (x >= 0)
|
||||||
|
sprintf(buf, "0%lo", x);
|
||||||
|
else
|
||||||
|
sprintf(buf, "-0%lo", -x);
|
||||||
|
return newstringobject(buf);
|
||||||
|
}
|
||||||
|
if (is_longobject(v)) {
|
||||||
|
return (object *) long_format(v, 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err_setstr(TypeError, "oct() requires int/long argument");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
builtin_open(self, v)
|
builtin_open(self, v)
|
||||||
object *self;
|
object *self;
|
||||||
|
@ -508,12 +557,14 @@ static struct methodlist builtin_methods[] = {
|
||||||
{"eval", builtin_eval},
|
{"eval", builtin_eval},
|
||||||
{"exec", builtin_exec},
|
{"exec", builtin_exec},
|
||||||
{"float", builtin_float},
|
{"float", builtin_float},
|
||||||
|
{"hex", builtin_hex},
|
||||||
{"input", builtin_input},
|
{"input", builtin_input},
|
||||||
{"int", builtin_int},
|
{"int", builtin_int},
|
||||||
{"len", builtin_len},
|
{"len", builtin_len},
|
||||||
{"long", builtin_long},
|
{"long", builtin_long},
|
||||||
{"max", builtin_max},
|
{"max", builtin_max},
|
||||||
{"min", builtin_min},
|
{"min", builtin_min},
|
||||||
|
{"oct", builtin_oct},
|
||||||
{"open", builtin_open}, /* XXX move to OS module */
|
{"open", builtin_open}, /* XXX move to OS module */
|
||||||
{"ord", builtin_ord},
|
{"ord", builtin_ord},
|
||||||
{"pow", builtin_pow},
|
{"pow", builtin_pow},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue