PyObject * PyNumber_Int(PyObject *o) { PyNumberMethods *m; const char *buffer; int buffer_len; if (o == NULL) return null_error(); if (PyInt_Check(o)) { Py_INCREF(o); return o; } if (PyString_Check(o)) return int_from_string(PyString_AS_STRING(o), PyString_GET_SIZE(o)); if (PyUnicode_Check(o)) return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o), PyUnicode_GET_SIZE(o), 10); m = o->ob_type->tp_as_number; if (m && m->nb_int) return m->nb_int(o); if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return int_from_string((char*)buffer, buffer_len); return type_error("object can't be converted to int"); }
static PyObject * int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *x = NULL; int base = -909; static char *kwlist[] = {"x", "base", 0}; if (type != &PyInt_Type) return int_subtype_new(type, args, kwds); /* Wimp out */ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, &x, &base)) return NULL; if (x == NULL) return PyInt_FromLong(0L); if (base == -909) return PyNumber_Int(x); if (PyString_Check(x)) return PyInt_FromString(PyString_AS_STRING(x), NULL, base); #ifdef Py_USING_UNICODE if (PyUnicode_Check(x)) return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), PyUnicode_GET_SIZE(x), base); #endif PyErr_SetString(PyExc_TypeError, "int() can't convert non-string with explicit base"); return NULL; }
static Box* _intNew(Box* val, Box* base) { if (isSubclass(val->cls, int_cls)) { RELEASE_ASSERT(!base, ""); BoxedInt* n = static_cast<BoxedInt*>(val); if (val->cls == int_cls) return n; return new BoxedInt(n->n); } else if (isSubclass(val->cls, str_cls)) { int base_n; if (!base) base_n = 10; else { RELEASE_ASSERT(base->cls == int_cls, ""); base_n = static_cast<BoxedInt*>(base)->n; } BoxedString* s = static_cast<BoxedString*>(val); RELEASE_ASSERT(s->size() == strlen(s->data()), ""); Box* r = PyInt_FromString(s->data(), NULL, base_n); if (!r) throwCAPIException(); return r; } else if (isSubclass(val->cls, unicode_cls)) { int base_n; if (!base) base_n = 10; else { RELEASE_ASSERT(base->cls == int_cls, ""); base_n = static_cast<BoxedInt*>(base)->n; } Box* r = PyInt_FromUnicode(PyUnicode_AS_UNICODE(val), PyUnicode_GET_SIZE(val), base_n); if (!r) throwCAPIException(); return r; } else if (val->cls == float_cls) { RELEASE_ASSERT(!base, ""); double d = static_cast<BoxedFloat*>(val)->d; return new BoxedInt(d); } else { RELEASE_ASSERT(!base, ""); static const std::string int_str("__int__"); Box* r = callattr(val, &int_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }), ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); if (!r) { fprintf(stderr, "TypeError: int() argument must be a string or a number, not '%s'\n", getTypeName(val)); raiseExcHelper(TypeError, ""); } if (!isSubclass(r->cls, int_cls) && !isSubclass(r->cls, long_cls)) { raiseExcHelper(TypeError, "__int__ returned non-int (type %s)", r->cls->tp_name); } return r; }
static PyObject *next(PyObject *self) { ligolw_Tokenizer *tokenizer = (ligolw_Tokenizer *) self; PyObject *type; PyObject *token; Py_UNICODE *start, *end; /* * Identify the start and end of the next token. */ do { type = next_token(tokenizer, &start, &end); if(!type) return NULL; } while(type == Py_None); /* * Extract token as desired type. */ if(start == NULL) { /* * unquoted zero-length string == None */ Py_INCREF(Py_None); token = Py_None; } else if(type == (PyObject *) &PyFloat_Type) { char ascii_buffer[end - start + 1]; char *ascii_end; if(PyUnicode_EncodeDecimal(start, end - start, ascii_buffer, NULL)) return NULL; token = PyFloat_FromDouble(strtod(ascii_buffer, &ascii_end)); if(ascii_end == ascii_buffer || *ascii_end != 0) { /* * strtod() couldn't convert the token, emulate * float()'s error message */ Py_XDECREF(token); PyErr_Format(PyExc_ValueError, "invalid literal for float(): '%s'", ascii_buffer); token = NULL; } } else if(type == (PyObject *) &PyUnicode_Type) { token = PyUnicode_FromUnicode(start, end - start); } else if(type == (PyObject *) &PyString_Type) { token = PyUnicode_Encode(start, end - start, NULL, NULL); } else if(type == (PyObject *) &PyInt_Type) { token = PyInt_FromUnicode(start, end - start, 0); } else if(type == (PyObject *) &PyLong_Type) { token = PyLong_FromUnicode(start, end - start, 0); } else { token = PyObject_CallFunction(type, "u#", start, end - start); } /* * Done. */ return token; }