GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43753: Add Py_Is() and Py_IsNone() functions by vstinner · Pull Request #25227 · python/cpython

bpo-43753: Add Py_Is() and Py_IsNone() functions #25227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
vstinner merged 1 commit into python:master from vstinner:py_is
Apr 10, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-43753: Add Py_Is() and Py_IsNone() functions #25227

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
e0fea7d
bpo-43753: Add Py_Is() and Py_IsNone() functions
vstinner Apr 6, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

31 changes: 31 additions & 0 deletions 31 Doc/c-api/structures.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ the definition of all other Python objects.
See documentation of :c:type:`PyVarObject` above.


.. c:function:: int Py_Is(const PyObject *x, const PyObject *y)
Comment thread
vstinner marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

Test if the *x* object is the *y* object, the same as ``x is y`` in Python.
.. versionadded:: 3.10
.. c:function:: int Py_IsNone(const PyObject *x)
Test if an object is the ``None`` singleton,
the same as ``x is None`` in Python.
.. versionadded:: 3.10
.. c:function:: int Py_IsTrue(const PyObject *x)
Test if an object is the ``True`` singleton,
the same as ``x is True`` in Python.
.. versionadded:: 3.10
.. c:function:: int Py_IsFalse(const PyObject *x)
Test if an object is the ``False`` singleton,
the same as ``x is False`` in Python.
.. versionadded:: 3.10
.. c:function:: PyTypeObject* Py_TYPE(const PyObject *o)
Get the type of the Python object *o*.
Expand Down
4 changes: 4 additions & 0 deletions 4 Doc/data/stable_abi.dat
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,11 @@ Py_HasFileSystemDefaultEncoding
Py_IncRef
Py_Initialize
Py_InitializeEx
Py_Is
Py_IsFalse
Py_IsInitialized
Py_IsNone
Py_IsTrue
Py_LeaveRecursiveCall
Py_Main
Py_MakePendingCalls
Expand Down
7 changes: 7 additions & 0 deletions 7 Doc/whatsnew/3.10.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,13 @@ New Features
build (``Py_TRACE_REFS`` macro).
(Contributed by Victor Stinner in :issue:`43688`.)
* Add the :c:func:`Py_Is(x, y) <Py_Is>` function to test if the *x* object is
the *y* object, the same as ``x is y`` in Python. Add also the
:c:func:`Py_IsNone`, :c:func:`Py_IsTrue`, :c:func:`Py_IsFalse` functions to
test if an object is, respectively, the ``None`` singleton, the ``True``
singleton or the ``False`` singleton.
(Contributed by Victor Stinner in :issue:`43753`.)
Porting to Python 3.10
----------------------
Expand Down
8 changes: 8 additions & 0 deletions 8 Include/boolobject.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;
#define Py_False ((PyObject *) &_Py_FalseStruct)
#define Py_True ((PyObject *) &_Py_TrueStruct)

// Test if an object is the True singleton, the same as "x is True" in Python.
PyAPI_FUNC(int) Py_IsTrue(PyObject *x);
#define Py_IsTrue(x) Py_Is((x), Py_True)

// Test if an object is the False singleton, the same as "x is False" in Python.
PyAPI_FUNC(int) Py_IsFalse(PyObject *x);
#define Py_IsFalse(x) Py_Is((x), Py_False)

/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_NewRef(Py_True)
#define Py_RETURN_FALSE return Py_NewRef(Py_False)
Expand Down
9 changes: 9 additions & 0 deletions 9 Include/object.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ typedef struct {
#define _PyVarObject_CAST_CONST(op) ((const PyVarObject*)(op))


// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
#define Py_Is(x, y) ((x) == (y))


static inline Py_ssize_t _Py_REFCNT(const PyObject *ob) {
return ob->ob_refcnt;
}
Expand Down Expand Up @@ -586,6 +591,10 @@ Don't forget to apply Py_INCREF() when returning this value!!!
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
#define Py_None (&_Py_NoneStruct)

// Test if an object is the None singleton, the same as "x is None" in Python.
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
#define Py_IsNone(x) Py_Is((x), Py_None)

/* Macro for returning Py_None from a function */
#define Py_RETURN_NONE return Py_NewRef(Py_None)

Expand Down
6 changes: 6 additions & 0 deletions 6 Misc/NEWS.d/next/C API/2021-04-06-20-52-44.bpo-43753.xUsHp1.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Add the :c:func:`Py_Is(x, y) <Py_Is>` function to test if the *x* object is the
*y* object, the same as ``x is y`` in Python. Add also the :c:func:`Py_IsNone`,
:c:func:`Py_IsTrue`, :c:func:`Py_IsFalse` functions to test if an object is,
respectively, the ``None`` singleton, the ``True`` singleton or the ``False``
singleton.
Patch by Victor Stinner.
111 changes: 90 additions & 21 deletions 111 Modules/_testcapimodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5401,32 +5401,98 @@ test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
}


// Test Py_NewRef() and Py_XNewRef() functions
#define TEST_REFCOUNT() \
do { \
PyObject *obj = PyList_New(0); \
if (obj == NULL) { \
return NULL; \
} \
assert(Py_REFCNT(obj) == 1); \
\
/* test Py_NewRef() */ \
PyObject *ref = Py_NewRef(obj); \
assert(ref == obj); \
assert(Py_REFCNT(obj) == 2); \
Py_DECREF(ref); \
\
/* test Py_XNewRef() */ \
PyObject *xref = Py_XNewRef(obj); \
assert(xref == obj); \
assert(Py_REFCNT(obj) == 2); \
Py_DECREF(xref); \
\
assert(Py_XNewRef(NULL) == NULL); \
\
Py_DECREF(obj); \
Py_RETURN_NONE; \
} while (0) \


// Test Py_NewRef() and Py_XNewRef() macros
static PyObject*
test_refcount(PyObject *self, PyObject *Py_UNUSED(ignored))
test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *obj = PyList_New(0);
if (obj == NULL) {
return NULL;
}
assert(Py_REFCNT(obj) == 1);
TEST_REFCOUNT();
}

#undef Py_NewRef
#undef Py_XNewRef

// Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
static PyObject*
test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
{
TEST_REFCOUNT();
}

// Test Py_NewRef()
PyObject *ref = Py_NewRef(obj);
assert(ref == obj);
assert(Py_REFCNT(obj) == 2);
Py_DECREF(ref);

// Test Py_XNewRef()
PyObject *xref = Py_XNewRef(obj);
assert(xref == obj);
assert(Py_REFCNT(obj) == 2);
Py_DECREF(xref);
// Test Py_Is() function
#define TEST_PY_IS() \
do { \
PyObject *o_none = Py_None; \
PyObject *o_true = Py_True; \
PyObject *o_false = Py_False; \
PyObject *obj = PyList_New(0); \
if (obj == NULL) { \
return NULL; \
} \
\
/* test Py_Is() */ \
assert(Py_Is(obj, obj)); \
assert(!Py_Is(obj, o_none)); \
\
/* test Py_None */ \
assert(Py_Is(o_none, o_none)); \
assert(!Py_Is(obj, o_none)); \
\
/* test Py_True */ \
assert(Py_Is(o_true, o_true)); \
assert(!Py_Is(o_false, o_true)); \
assert(!Py_Is(obj, o_true)); \
\
/* test Py_False */ \
assert(Py_Is(o_false, o_false)); \
assert(!Py_Is(o_true, o_false)); \
assert(!Py_Is(obj, o_false)); \
\
Py_DECREF(obj); \
Py_RETURN_NONE; \
} while (0)

// Test Py_Is() macro
static PyObject*
test_py_is_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
{
TEST_PY_IS();
}

assert(Py_XNewRef(NULL) == NULL);
#undef Py_Is

Py_DECREF(obj);
Py_RETURN_NONE;
// Test Py_Is() function, after undefining its macro.
static PyObject*
test_py_is_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
{
TEST_PY_IS();
}


Expand Down Expand Up @@ -5716,7 +5782,10 @@ static PyMethodDef TestMethods[] = {
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
{"without_gc", without_gc, METH_O},
{"test_set_type_size", test_set_type_size, METH_NOARGS},
{"test_refcount", test_refcount, METH_NOARGS},
{"test_refcount_macros", test_refcount_macros, METH_NOARGS},
{"test_refcount_funcs", test_refcount_funcs, METH_NOARGS},
{"test_py_is_macros", test_py_is_macros, METH_NOARGS},
{"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
{"fatal_error", test_fatal_error, METH_VARARGS,
PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")},
{NULL, NULL} /* sentinel */
Expand Down
27 changes: 27 additions & 0 deletions 27 Objects/object.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,33 @@ Py_XNewRef(PyObject *obj)
return _Py_XNewRef(obj);
}

#undef Py_Is
#undef Py_IsNone
#undef Py_IsTrue
#undef Py_IsFalse

// Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
Comment thread Outdated

@pablogsal pablogsal Apr 10, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not super conviced. Why would people would like to use Py_Is if is going to be slower than just pointer comparison?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Apr 10, 2021

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is correctness if you care about other Python implelentations than CPython. If you don't care, well, don't use it :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
// for the stable ABI.
int Py_Is(PyObject *x, PyObject *y)
{
return (x == y);
}

int Py_IsNone(PyObject *x)
{
return Py_Is(x, Py_None);
}

int Py_IsTrue(PyObject *x)
{
return Py_Is(x, Py_True);
}

int Py_IsFalse(PyObject *x)
{
return Py_Is(x, Py_False);
}

#ifdef __cplusplus
}
#endif
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.