Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/c-api/concrete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Container Objects

dict.rst
set.rst
simplenamespace.rst


.. _otherobjects:
Expand Down
15 changes: 15 additions & 0 deletions Doc/c-api/simplenamespace.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.. highlight:: c

SimpleNamespace Objects
-----------------------

.. c:function:: PyObject* PySimpleNamespace_New(PyObject *attrs)

Create a new simple namespace object.

*attrs* must be a mapping object with non-empty string keys.
It can be ``NULL``.

Return ``NULL`` and raise an exception on failure.

.. versionadded:: 3.11
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ New Features
* Add a new :c:func:`PyType_GetQualName` function to get type's qualified name.
(Contributed by Hai Shi in :issue:`42035`.)

* Add a new :c:func:`PySimpleNamespace_New` function to create a simple
namespace: C API of the :class:`types.SimpleNamespace` type.
(Contributed by Victor Stinner in :issue:`45482`.)

Porting to Python 3.11
----------------------

Expand Down
2 changes: 1 addition & 1 deletion Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#include "warnings.h"
#include "weakrefobject.h"
#include "structseq.h"
#include "namespaceobject.h"
#include "cpython/namespaceobject.h"
#include "cpython/picklebufobject.h"
#include "cpython/pytime.h"
#include "codecs.h"
Expand Down
18 changes: 18 additions & 0 deletions Include/cpython/namespaceobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Simple namespace object interface

#ifndef Py_LIMITED_API
#ifndef Py_NAMESPACEOBJECT_H
#define Py_NAMESPACEOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

PyAPI_DATA(PyTypeObject) _PySimpleNamespace_Type;

PyAPI_FUNC(PyObject *) PySimpleNamespace_New(PyObject *attrs);

#ifdef __cplusplus
}
#endif
#endif // !Py_NAMESPACEOBJECT_H
#endif // !Py_LIMITED_API
19 changes: 0 additions & 19 deletions Include/namespaceobject.h

This file was deleted.

49 changes: 45 additions & 4 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Python test set -- part 6, built-in types

from test.support import run_with_locale, cpython_only
import collections.abc
from collections import namedtuple
import copy
import gc
import inspect
import pickle
import locale
import pickle
import sys
import types
import typing
import unittest.mock
import weakref
import typing

from collections import namedtuple
from test.support import run_with_locale, cpython_only
from test.support import import_helper


T = typing.TypeVar("T")
Expand Down Expand Up @@ -1733,6 +1735,45 @@ class FakeSimpleNamespace(str):
with self.assertRaises(TypeError):
types.SimpleNamespace() >= FakeSimpleNamespace()

def test_capi(self):
_testcapi = import_helper.import_module('_testcapi')
PySimpleNamespace_New = _testcapi.PySimpleNamespace_New

def list_attributes(obj):
return [name for name in dir(obj) if not name.startswith('_')]

# two attributes
value1 = 'value'
value2 = 2
ns = PySimpleNamespace_New({'attr1': value1, 'attr2': value2})
self.assertIsInstance(ns, types.SimpleNamespace)
self.assertEqual(list_attributes(ns), ['attr1', 'attr2'])
self.assertIs(ns.attr1, value1)
self.assertIs(ns.attr2, value2)

# it's possible to modify a namespace
ns.attr1 = 1
self.assertEqual(ns.attr1, 1)
ns.attr3 = 3
self.assertEqual(list_attributes(ns), ['attr1', 'attr2', 'attr3'])

# empty namespace
ns = PySimpleNamespace_New({})
self.assertIsInstance(ns, types.SimpleNamespace)
self.assertEqual(list_attributes(ns), [])

# invalid arguments
for invalid_arg in (
123,
'string',
[('key', 'value')],
# non-string key
{123: 'value'},
):
with (self.subTest(invalid_arg=invalid_arg),
self.assertRaises(TypeError)):
PySimpleNamespace_New(invalid_arg)


class CoroutineTests(unittest.TestCase):
def test_wrong_args(self):
Expand Down
2 changes: 1 addition & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/methodobject.h \
$(srcdir)/Include/modsupport.h \
$(srcdir)/Include/moduleobject.h \
$(srcdir)/Include/namespaceobject.h \
$(srcdir)/Include/object.h \
$(srcdir)/Include/objimpl.h \
$(srcdir)/Include/opcode.h \
Expand Down Expand Up @@ -1215,6 +1214,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/cpython/interpreteridobject.h \
$(srcdir)/Include/cpython/listobject.h \
$(srcdir)/Include/cpython/methodobject.h \
$(srcdir)/Include/cpython/namespaceobject.h \
$(srcdir)/Include/cpython/object.h \
$(srcdir)/Include/cpython/objimpl.h \
$(srcdir)/Include/cpython/odictobject.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a new :c:func:`PySimpleNamespace_New` function to create a simple
namespace: C API of the :class:`types.SimpleNamespace` type. Patch by Victor
Stinner.
15 changes: 14 additions & 1 deletion Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "int") == 0);
Py_DECREF(tp_qualname);

tp_qualname = PyType_GetQualName(&_PyNamespace_Type);
tp_qualname = PyType_GetQualName(&_PySimpleNamespace_Type);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "SimpleNamespace") == 0);
Py_DECREF(tp_qualname);

Expand Down Expand Up @@ -5674,6 +5674,18 @@ type_get_version(PyObject *self, PyObject *type)
}


static PyObject *
test_simplenamespace(PyObject *self, PyObject *args)
{
PyObject *items = NULL;
if (!PyArg_ParseTuple(args, "|O", &items)) {
return NULL;
}

return PySimpleNamespace_New(items);
}


static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);

Expand Down Expand Up @@ -5957,6 +5969,7 @@ static PyMethodDef TestMethods[] = {
{"fatal_error", test_fatal_error, METH_VARARGS,
PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")},
{"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")},
{"PySimpleNamespace_New", test_simplenamespace, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
PyDict_SetItemString(dct, "three", three);
Py_DECREF(three);

ns = _PyNamespace_New(dct);
ns = PySimpleNamespace_New(dct);
Py_DECREF(dct);
return ns;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ time_get_clock_info(PyObject *self, PyObject *args)
}
Py_CLEAR(obj);

ns = _PyNamespace_New(dict);
ns = PySimpleNamespace_New(dict);
Py_DECREF(dict);
return ns;

Expand Down
44 changes: 31 additions & 13 deletions Objects/namespaceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ namespace_repr(PyObject *ns)
PyObject *separator, *pairsrepr, *repr = NULL;
const char * name;

name = Py_IS_TYPE(ns, &_PyNamespace_Type) ? "namespace"
: Py_TYPE(ns)->tp_name;
if (Py_IS_TYPE(ns, &_PySimpleNamespace_Type)) {
name = "namespace";
}
else {
name = Py_TYPE(ns)->tp_name;
}

i = Py_ReprEnter(ns);
if (i != 0) {
Expand Down Expand Up @@ -163,8 +167,8 @@ namespace_clear(_PyNamespaceObject *ns)
static PyObject *
namespace_richcompare(PyObject *self, PyObject *other, int op)
{
if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
PyObject_TypeCheck(other, &_PyNamespace_Type))
if (PyObject_TypeCheck(self, &_PySimpleNamespace_Type) &&
PyObject_TypeCheck(other, &_PySimpleNamespace_Type))
return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
((_PyNamespaceObject *)other)->ns_dict, op);
Py_RETURN_NOTIMPLEMENTED;
Expand Down Expand Up @@ -199,7 +203,7 @@ PyDoc_STRVAR(namespace_doc,
\n\
SimpleNamespace(**kwargs)");

PyTypeObject _PyNamespace_Type = {
PyTypeObject _PySimpleNamespace_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"types.SimpleNamespace", /* tp_name */
sizeof(_PyNamespaceObject), /* tp_basicsize */
Expand Down Expand Up @@ -244,18 +248,32 @@ PyTypeObject _PyNamespace_Type = {


PyObject *
_PyNamespace_New(PyObject *kwds)
PySimpleNamespace_New(PyObject *attrs)
{
PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
if (ns == NULL)
if (attrs != NULL && !PyDict_Check(attrs)) {
PyErr_Format(PyExc_TypeError, "expected dict, got %s",
Py_TYPE(attrs)->tp_name);
return NULL;
}

if (kwds == NULL)
return ns;
if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
Py_DECREF(ns);
PyObject *ns = namespace_new(&_PySimpleNamespace_Type, NULL, NULL);
if (ns == NULL) {
return NULL;
}

return (PyObject *)ns;
if (attrs == NULL) {
return ns;
}

if (!PyArg_ValidateKeywordArguments(attrs)) {
goto error;
}
if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, attrs) < 0) {
goto error;
}
return ns;

error:
Py_DECREF(ns);
return NULL;
}
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@ _PyTypes_Init(void)
INIT_TYPE(_PyInterpreterID_Type);
INIT_TYPE(_PyManagedBuffer_Type);
INIT_TYPE(_PyMethodWrapper_Type);
INIT_TYPE(_PyNamespace_Type);
INIT_TYPE(_PySimpleNamespace_Type);
INIT_TYPE(_PyNone_Type);
INIT_TYPE(_PyNotImplemented_Type);
INIT_TYPE(_PyWeakref_CallableProxyType);
Expand Down
2 changes: 1 addition & 1 deletion PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<ClInclude Include="..\Include\cpython\interpreteridobject.h" />
<ClInclude Include="..\Include\cpython\listobject.h" />
<ClInclude Include="..\Include\cpython\methodobject.h" />
<ClInclude Include="..\Include\cpython\namespaceobject.h" />
<ClInclude Include="..\Include\cpython\object.h" />
<ClInclude Include="..\Include\cpython\objimpl.h" />
<ClInclude Include="..\Include\cpython\odictobject.h" />
Expand Down Expand Up @@ -231,7 +232,6 @@
<ClInclude Include="..\Include\methodobject.h" />
<ClInclude Include="..\Include\modsupport.h" />
<ClInclude Include="..\Include\moduleobject.h" />
<ClInclude Include="..\Include\namespaceobject.h" />
<ClInclude Include="..\Include\object.h" />
<ClInclude Include="..\Include\objimpl.h" />
<ClInclude Include="..\Include\opcode.h" />
Expand Down
10 changes: 5 additions & 5 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,6 @@
<ClInclude Include="..\Include\pyhash.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\namespaceobject.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\interpreteridobject.h">
<Filter>Include</Filter>
</ClInclude>
Expand Down Expand Up @@ -408,12 +405,15 @@
<ClInclude Include="..\Include\cpython\methodobject.h">
<Filter>Include\cpython</Filter>
</ClInclude>
<ClInclude Include="..\Include\cpython\objimpl.h">
<Filter>Include\cpython</Filter>
<ClInclude Include="..\Include\cpython\namespaceobject.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Include\cpython\object.h">
<Filter>Include\cpython</Filter>
</ClInclude>
<ClInclude Include="..\Include\cpython\objimpl.h">
<Filter>Include\cpython</Filter>
</ClInclude>
<ClInclude Include="..\Include\cpython\parser_interface.h">
<Filter>Include</Filter>
</ClInclude>
Expand Down
2 changes: 1 addition & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@ _PyImport_BootstrapImp(PyThreadState *tstate)
if (attrs == NULL) {
goto error;
}
PyObject *spec = _PyNamespace_New(attrs);
PyObject *spec = PySimpleNamespace_New(attrs);
Py_DECREF(attrs);
if (spec == NULL) {
goto error;
Expand Down
4 changes: 2 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Data members:
#include "pycore_pylifecycle.h" // _PyErr_WriteUnraisableDefaultHook()
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_structseq.h" // PyStructSequence_InitType()
#include "pycore_structseq.h" // _PyStructSequence_InitType()
#include "pycore_tuple.h" // _PyTuple_FromArray()

#include "code.h"
Expand Down Expand Up @@ -2717,7 +2717,7 @@ make_impl_info(PyObject *version_info)

/* dict ready */

ns = _PyNamespace_New(impl_info);
ns = PySimpleNamespace_New(impl_info);
Py_DECREF(impl_info);
return ns;

Expand Down