is_number = PyNumber_Check(file);
if (is_number) {
- path_or_fd = file;
- Py_INCREF(path_or_fd);
+ path_or_fd = Py_NewRef(file);
} else {
path_or_fd = PyOS_FSPath(file);
if (path_or_fd == NULL) {
encoding = &_Py_ID(locale);
}
}
- Py_INCREF(encoding);
- return encoding;
+ return Py_NewRef(encoding);
}
"UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
if (state->unsupported_operation == NULL)
goto fail;
- Py_INCREF(state->unsupported_operation);
if (PyModule_AddObject(m, "UnsupportedOperation",
- state->unsupported_operation) < 0)
+ Py_NewRef(state->unsupported_operation)) < 0)
goto fail;
/* BlockingIOError, for compatibility */
if (r < 0)
goto end;
if (r > 0) {
- res = Py_None;
- Py_INCREF(res);
+ res = Py_NewRef(Py_None);
goto end;
}
break;
if (n < 0) {
if (n == -2) {
- Py_INCREF(Py_None);
- res = Py_None;
+ res = Py_NewRef(Py_None);
}
goto end;
}
if (_PyIOBase_check_readable(raw, Py_True) == NULL)
return -1;
- Py_INCREF(raw);
- Py_XSETREF(self->raw, raw);
+ Py_XSETREF(self->raw, Py_NewRef(raw));
self->buffer_size = buffer_size;
self->readable = 1;
self->writable = 0;
buf = (bytesiobuf *) type->tp_alloc(type, 0);
if (buf == NULL)
return NULL;
- Py_INCREF(self);
- buf->source = self;
+ buf->source = (bytesio*)Py_NewRef(self);
view = PyMemoryView_FromObject((PyObject *) buf);
Py_DECREF(buf);
return view;
return NULL;
}
}
- Py_INCREF(self->buf);
- return self->buf;
+ return Py_NewRef(self->buf);
}
/*[clinic input]
self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
self->exports == 0) {
self->pos += size;
- Py_INCREF(self->buf);
- return self->buf;
+ return Py_NewRef(self->buf);
}
output = PyBytes_AS_STRING(self->buf) + self->pos;
if (initvalue == NULL)
return NULL;
if (self->dict == NULL) {
- Py_INCREF(Py_None);
- dict = Py_None;
+ dict = Py_NewRef(Py_None);
}
else {
dict = PyDict_Copy(self->dict);
return NULL;
}
else {
- Py_INCREF(dict);
- self->dict = dict;
+ self->dict = Py_NewRef(dict);
}
}
}
if (initvalue && initvalue != Py_None) {
if (PyBytes_CheckExact(initvalue)) {
- Py_INCREF(initvalue);
- Py_XSETREF(self->buf, initvalue);
+ Py_XSETREF(self->buf, Py_NewRef(initvalue));
self->string_size = PyBytes_GET_SIZE(initvalue);
}
else {
if (iobase_check_closed(self))
return NULL;
- Py_INCREF(self);
- return self;
+ return Py_NewRef(self);
}
static PyObject *
if (iobase_check_closed(self))
return NULL;
- Py_INCREF(self);
- return self;
+ return Py_NewRef(self);
}
static PyObject *
self->decoder, obj, 1 /* always final */);
}
else {
- decoded = obj;
- Py_INCREF(decoded);
+ decoded = Py_NewRef(obj);
}
if (self->writenl) {
PyObject *translated = PyUnicode_Replace(
is pointless for StringIO)
*/
if (newline != NULL && newline[0] == '\r') {
- self->writenl = self->readnl;
- Py_INCREF(self->writenl);
+ self->writenl = Py_NewRef(self->readnl);
}
if (self->readuniversal) {
if (initvalue == NULL)
return NULL;
if (self->dict == NULL) {
- Py_INCREF(Py_None);
- dict = Py_None;
+ dict = Py_NewRef(Py_None);
}
else {
dict = PyDict_Copy(self->dict);
return NULL;
}
else {
- Py_INCREF(dict);
- self->dict = dict;
+ self->dict = Py_NewRef(dict);
}
}
PyObject *errors)
/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
{
- self->decoder = decoder;
- Py_INCREF(decoder);
+ self->decoder = Py_NewRef(decoder);
if (errors == NULL) {
- self->errors = &_Py_ID(strict);
+ self->errors = Py_NewRef(&_Py_ID(strict));
}
else {
- self->errors = errors;
+ self->errors = Py_NewRef(errors);
}
- Py_INCREF(self->errors);
self->translate = translate ? 1 : 0;
self->seennl = 0;
&_Py_ID(decode), input, final ? Py_True : Py_False, NULL);
}
else {
- output = input;
- Py_INCREF(output);
+ output = Py_NewRef(input);
}
if (check_decoded(output) < 0)
* of the partially constructed object (like self->encoding)
*/
- Py_INCREF(errors);
- self->errors = errors;
+ self->errors = Py_NewRef(errors);
self->chunk_size = 8192;
self->line_buffering = line_buffering;
self->write_through = write_through;
goto error;
}
- self->buffer = buffer;
- Py_INCREF(buffer);
+ self->buffer = Py_NewRef(buffer);
/* Build the decoder object */
if (_textiowrapper_set_decoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0)
}
Py_DECREF(codec_info);
- Py_INCREF(errors);
Py_SETREF(self->encoding, encoding);
- Py_SETREF(self->errors, errors);
+ Py_SETREF(self->errors, Py_NewRef(errors));
return _textiowrapper_fix_encoder_state(self);
}
PyObject *b;
if (PyBytes_Check(pending)) {
- b = pending;
- Py_INCREF(b);
+ b = Py_NewRef(pending);
}
else if (PyUnicode_Check(pending)) {
assert(PyUnicode_IS_ASCII(pending));
// See bpo-43260
PyUnicode_GET_LENGTH(text) <= self->chunk_size &&
is_asciicompat_encoding(self->encodefunc)) {
- b = text;
- Py_INCREF(b);
+ b = Py_NewRef(text);
}
else {
b = (*self->encodefunc)((PyObject *) self, text);
return NULL;
}
else {
- chars = self->decoded_chars;
- Py_INCREF(chars);
+ chars = Py_NewRef(self->decoded_chars);
}
self->decoded_chars_used += n;
}
if (remaining == NULL) {
- line = self->decoded_chars;
+ line = Py_NewRef(self->decoded_chars);
start = self->decoded_chars_used;
offset_to_buffer = 0;
- Py_INCREF(line);
}
else {
assert(self->decoded_chars_used == 0);
textiowrapper_errors_get(textio *self, void *context)
{
CHECK_INITIALIZED(self);
- Py_INCREF(self->errors);
- return self->errors;
+ return Py_NewRef(self->errors);
}
static PyObject *
if (s == NULL)
return NULL;
- s->markers = markers;
- s->defaultfn = defaultfn;
- s->encoder = encoder;
- s->indent = indent;
- s->key_separator = key_separator;
- s->item_separator = item_separator;
+ s->markers = Py_NewRef(markers);
+ s->defaultfn = Py_NewRef(defaultfn);
+ s->encoder = Py_NewRef(encoder);
+ s->indent = Py_NewRef(indent);
+ s->key_separator = Py_NewRef(key_separator);
+ s->item_separator = Py_NewRef(item_separator);
s->sort_keys = sort_keys;
s->skipkeys = skipkeys;
s->allow_nan = allow_nan;
s->fast_encode = NULL;
+
if (PyCFunction_Check(s->encoder)) {
PyCFunction f = PyCFunction_GetFunction(s->encoder);
if (f == (PyCFunction)py_encode_basestring_ascii ||
}
}
- Py_INCREF(s->markers);
- Py_INCREF(s->defaultfn);
- Py_INCREF(s->encoder);
- Py_INCREF(s->indent);
- Py_INCREF(s->key_separator);
- Py_INCREF(s->item_separator);
return (PyObject *)s;
}
PyObject *encoded;
if (PyUnicode_Check(key)) {
- Py_INCREF(key);
- keystr = key;
+ keystr = Py_NewRef(key);
}
else if (PyFloat_Check(key)) {
keystr = encoder_encode_float(s, key);
{
PyCFunctionObject *fn;
if (!PyCFunction_Check(obj)) {
- Py_INCREF(obj);
- return obj;
+ return Py_NewRef(obj);
}
/* Replace built-in function objects with a descriptive string
because of built-in methods -- keeping a reference to
PyObject *modname = NULL;
if (mod != NULL) {
if (PyUnicode_Check(mod)) {
- modname = mod;
- Py_INCREF(modname);
+ modname = Py_NewRef(mod);
}
else if (PyModule_Check(mod)) {
modname = PyModule_GetNameObject(mod);
}
}
else {
- Py_INCREF(Py_None);
- collect->sublist = Py_None;
+ collect->sublist = Py_NewRef(Py_None);
}
info = PyObject_CallFunction((PyObject*) collect->state->stats_entry_type,
if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
return -1;
pObj->externalTimerUnit = timeunit;
- Py_XINCREF(timer);
- Py_XSETREF(pObj->externalTimer, timer);
+ Py_XSETREF(pObj->externalTimer, Py_XNewRef(timer));
return 0;
}
{
PyObject *result;
result = (a != b) ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
+ return Py_NewRef(result);
}
/* compare_digest **********************************************************/
return NULL;
}
- Py_INCREF(item);
- ig->item = item;
+ ig->item = Py_NewRef(item);
ig->nitems = nitems;
ig->index = -1;
if (PyLong_CheckExact(item)) {
&& ig->index < PyTuple_GET_SIZE(obj))
{
result = PyTuple_GET_ITEM(obj, ig->index);
- Py_INCREF(result);
- return result;
+ return Py_NewRef(result);
}
return PyObject_GetItem(obj, ig->item);
}
}
return PyUnicode_Join(*attrsep, attr);
} else {
- Py_INCREF(attr);
- return attr;
+ return Py_NewRef(attr);
}
}
PyUnicode_InternInPlace(&name);
mc->name = name;
- Py_XINCREF(kwds);
- mc->kwds = kwds;
+ mc->kwds = Py_XNewRef(kwds);
mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
if (mc->args == NULL) {
newargs = PyTuple_New(1 + callargcount);
if (newargs == NULL)
return NULL;
- Py_INCREF(mc->name);
- PyTuple_SET_ITEM(newargs, 0, mc->name);
+ PyTuple_SET_ITEM(newargs, 0, Py_NewRef(mc->name));
for (i = 0; i < callargcount; ++i) {
PyObject *arg = PyTuple_GET_ITEM(mc->args, i);
- Py_INCREF(arg);
- PyTuple_SET_ITEM(newargs, i + 1, arg);
+ PyTuple_SET_ITEM(newargs, i + 1, Py_NewRef(arg));
}
return Py_BuildValue("ON", Py_TYPE(mc), newargs);
}
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
/* Deconstruct a bound Python method */
- func2 = PyMethod_GET_FUNCTION(func);
- Py_INCREF(func2);
*method_self = self; /* borrowed */
- Py_XSETREF(*method_func, func2);
+ func2 = PyMethod_GET_FUNCTION(func);
+ Py_XSETREF(*method_func, Py_NewRef(func2));
Py_DECREF(func);
return 0;
}
return PyMethod_New(func, self);
}
else {
- Py_INCREF(func);
- return func;
+ return Py_NewRef(func);
}
}
entry->me_value = value;
return 0;
}
- Py_INCREF(key);
- entry->me_key = key;
+ entry->me_key = Py_NewRef(key);
entry->me_value = value;
self->mt_used++;
return -1;
}
- Py_XINCREF(buffer_callback);
- self->buffer_callback = buffer_callback;
+ self->buffer_callback = Py_XNewRef(buffer_callback);
return 0;
}
return -1;
assert(idx < self->memo_size);
}
- Py_INCREF(value);
old_item = self->memo[idx];
- self->memo[idx] = value;
+ self->memo[idx] = Py_NewRef(value);
if (old_item != NULL) {
Py_DECREF(old_item);
}
i = 0;
while (PyDict_Next(modules, &i, &module_name, &module)) {
if (_checkmodule(module_name, module, global, dotted_path) == 0) {
- Py_INCREF(module_name);
- return module_name;
+ return Py_NewRef(module_name);
}
if (PyErr_Occurred()) {
return NULL;
/* If no module is found, use __main__. */
module_name = &_Py_ID(__main__);
- Py_INCREF(module_name);
- return module_name;
+ return Py_NewRef(module_name);
}
/* fast_save_enter() and fast_save_leave() are guards against recursive
Py_CLEAR(*module_name);
Py_CLEAR(*global_name);
- Py_INCREF(fixed_module_name);
- Py_INCREF(fixed_global_name);
- *module_name = fixed_module_name;
- *global_name = fixed_global_name;
+ *module_name = Py_NewRef(fixed_module_name);
+ *global_name = Py_NewRef(fixed_global_name);
return 0;
}
else if (PyErr_Occurred()) {
Py_TYPE(item)->tp_name);
return -1;
}
- Py_INCREF(item);
- Py_XSETREF(*module_name, item);
+ Py_XSETREF(*module_name, Py_NewRef(item));
}
else if (PyErr_Occurred()) {
return -1;
const char global_op = GLOBAL;
if (name) {
- Py_INCREF(name);
- global_name = name;
+ global_name = Py_NewRef(name);
}
else {
if (_PyObject_LookupAttr(obj, &_Py_ID(__qualname__), &global_name) < 0)
obj, module_name);
goto error;
}
- lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
- Py_INCREF(lastname);
+ lastname = Py_NewRef(PyList_GET_ITEM(dotted_path,
+ PyList_GET_SIZE(dotted_path) - 1));
cls = get_deep_attribute(module, dotted_path, &parent);
Py_CLEAR(dotted_path);
if (cls == NULL) {
PyObject *cls;
if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &cls) == 0) {
- cls = (PyObject *) Py_TYPE(obj);
- Py_INCREF(cls);
+ cls = Py_NewRef(Py_TYPE(obj));
}
return cls;
}
return -1;
}
PyTuple_SET_ITEM(newargs, 0, cls_new);
- Py_INCREF(cls);
- PyTuple_SET_ITEM(newargs, 1, cls);
+ PyTuple_SET_ITEM(newargs, 1, Py_NewRef(cls));
for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
PyObject *item = PyTuple_GET_ITEM(args, i);
- Py_INCREF(item);
- PyTuple_SET_ITEM(newargs, i + 2, item);
+ PyTuple_SET_ITEM(newargs, i + 2, Py_NewRef(item));
}
callable = PyObject_Call(st->partial, newargs, kwargs);
}
}
if (reduce_func != NULL) {
- Py_INCREF(obj);
- reduce_value = _Pickle_FastCall(reduce_func, obj);
+ reduce_value = _Pickle_FastCall(reduce_func, Py_NewRef(obj));
}
else if (PyType_IsSubtype(type, &PyType_Type)) {
status = save_global(self, obj, NULL);
return NULL;
}
PyTuple_SET_ITEM(dict_args, 0, contents);
- Py_INCREF((PyObject *)&PyDict_Type);
- PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
+ PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
PyTuple_SET_ITEM(reduce_value, 1, dict_args);
return reduce_value;
}
self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
if (self == NULL)
return NULL;
- Py_INCREF(pickler);
- self->pickler = pickler;
+ self->pickler = (PicklerObject*)Py_NewRef(pickler);
PyObject_GC_Track(self);
return (PyObject *)self;
}
}
self->pers_func_self = NULL;
- Py_INCREF(value);
- Py_XSETREF(self->pers_func, value);
+ Py_XSETREF(self->pers_func, Py_NewRef(value));
return 0;
}
return NULL;
}
PyTuple_SET_ITEM(constructor_args, 0, contents);
- Py_INCREF((PyObject *)&PyDict_Type);
- PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
+ PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
return reduce_value;
}
&UnpicklerMemoProxyType);
if (self == NULL)
return NULL;
- Py_INCREF(unpickler);
- self->unpickler = unpickler;
+ self->unpickler = (UnpicklerObject*)Py_NewRef(unpickler);
PyObject_GC_Track(self);
return (PyObject *)self;
}
return -1;
for (size_t i = 0; i < new_memo_size; i++) {
- Py_XINCREF(unpickler->memo[i]);
- new_memo[i] = unpickler->memo[i];
+ new_memo[i] = Py_XNewRef(unpickler->memo[i]);
}
}
else if (PyDict_Check(obj)) {
}
self->pers_func_self = NULL;
- Py_INCREF(value);
- Py_XSETREF(self->pers_func, value);
+ Py_XSETREF(self->pers_func, Py_NewRef(value));
return 0;
}
m = PyState_FindModule(&_picklemodule);
if (m) {
- Py_INCREF(m);
- return m;
+ return Py_NewRef(m);
}
if (PyType_Ready(&Pdata_Type) < 0)