Skip to content

Commit 0e6f07d

Browse files
committed
Sync boolobject.c with CPython
1 parent b716d5f commit 0e6f07d

File tree

2 files changed

+205
-44
lines changed

2 files changed

+205
-44
lines changed
Lines changed: 204 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,214 @@
1-
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
3-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1+
/* Copyright (c) 2024, Oracle and/or its affiliates.
2+
* Copyright (C) 1996-2024 Python Software Foundation
43
*
5-
* The Universal Permissive License (UPL), Version 1.0
6-
*
7-
* Subject to the condition set forth below, permission is hereby granted to any
8-
* person obtaining a copy of this software, associated documentation and/or
9-
* data (collectively the "Software"), free of charge and under any and all
10-
* copyright rights in the Software, and any and all patent rights owned or
11-
* freely licensable by each licensor hereunder covering either (i) the
12-
* unmodified Software as contributed to or provided by such licensor, or (ii)
13-
* the Larger Works (as defined below), to deal in both
14-
*
15-
* (a) the Software, and
16-
*
17-
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18-
* one is included with the Software each a "Larger Work" to which the Software
19-
* is contributed by such licensors),
20-
*
21-
* without restriction, including without limitation the rights to copy, create
22-
* derivative works of, display, perform, and distribute the Software and make,
23-
* use, sell, offer for sale, import, export, have made, and have sold the
24-
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25-
* either these or other terms.
26-
*
27-
* This license is subject to the following condition:
28-
*
29-
* The above copyright notice and either this complete permission notice or at a
30-
* minimum a reference to the UPL must be included in all copies or substantial
31-
* portions of the Software.
32-
*
33-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39-
* SOFTWARE.
4+
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
405
*/
41-
#include "capi.h"
6+
#include "capi.h" // GraalPy change
7+
#include "Python.h"
8+
#if 0 // GraalPy change
9+
#include "pycore_object.h" // _Py_FatalRefcountError()
10+
#include "pycore_runtime.h" // _Py_ID()
11+
12+
/* We define bool_repr to return "False" or "True" */
13+
14+
static PyObject *
15+
bool_repr(PyObject *self)
16+
{
17+
PyObject *res = self == Py_True ? &_Py_ID(True) : &_Py_ID(False);
18+
return Py_NewRef(res);
19+
}
20+
#endif // GraalPy change
21+
22+
/* Function to return a bool from a C long */
4223

43-
// taken from CPython "Python/Objects/boolobject.c"
44-
PyObject *PyBool_FromLong(long ok) {
24+
PyObject *PyBool_FromLong(long ok)
25+
{
4526
PyObject *result;
4627

47-
if (ok) {
28+
if (ok)
4829
result = Py_True;
49-
} else {
30+
else
5031
result = Py_False;
51-
}
5232
Py_INCREF(result);
5333
return result;
5434
}
35+
36+
#if 0 // GraalPy change
37+
/* We define bool_new to always return either Py_True or Py_False */
38+
39+
static PyObject *
40+
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
41+
{
42+
PyObject *x = Py_False;
43+
long ok;
44+
45+
if (!_PyArg_NoKeywords("bool", kwds))
46+
return NULL;
47+
if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x))
48+
return NULL;
49+
ok = PyObject_IsTrue(x);
50+
if (ok < 0)
51+
return NULL;
52+
return PyBool_FromLong(ok);
53+
}
54+
55+
static PyObject *
56+
bool_vectorcall(PyObject *type, PyObject * const*args,
57+
size_t nargsf, PyObject *kwnames)
58+
{
59+
long ok = 0;
60+
if (!_PyArg_NoKwnames("bool", kwnames)) {
61+
return NULL;
62+
}
63+
64+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
65+
if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
66+
return NULL;
67+
}
68+
69+
assert(PyType_Check(type));
70+
if (nargs) {
71+
ok = PyObject_IsTrue(args[0]);
72+
if (ok < 0) {
73+
return NULL;
74+
}
75+
}
76+
return PyBool_FromLong(ok);
77+
}
78+
79+
/* Arithmetic operations redefined to return bool if both args are bool. */
80+
81+
static PyObject *
82+
bool_and(PyObject *a, PyObject *b)
83+
{
84+
if (!PyBool_Check(a) || !PyBool_Check(b))
85+
return PyLong_Type.tp_as_number->nb_and(a, b);
86+
return PyBool_FromLong((a == Py_True) & (b == Py_True));
87+
}
88+
89+
static PyObject *
90+
bool_or(PyObject *a, PyObject *b)
91+
{
92+
if (!PyBool_Check(a) || !PyBool_Check(b))
93+
return PyLong_Type.tp_as_number->nb_or(a, b);
94+
return PyBool_FromLong((a == Py_True) | (b == Py_True));
95+
}
96+
97+
static PyObject *
98+
bool_xor(PyObject *a, PyObject *b)
99+
{
100+
if (!PyBool_Check(a) || !PyBool_Check(b))
101+
return PyLong_Type.tp_as_number->nb_xor(a, b);
102+
return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
103+
}
104+
105+
/* Doc string */
106+
107+
PyDoc_STRVAR(bool_doc,
108+
"bool(x) -> bool\n\
109+
\n\
110+
Returns True when the argument x is true, False otherwise.\n\
111+
The builtins True and False are the only two instances of the class bool.\n\
112+
The class bool is a subclass of the class int, and cannot be subclassed.");
113+
114+
/* Arithmetic methods -- only so we can override &, |, ^. */
115+
116+
static PyNumberMethods bool_as_number = {
117+
0, /* nb_add */
118+
0, /* nb_subtract */
119+
0, /* nb_multiply */
120+
0, /* nb_remainder */
121+
0, /* nb_divmod */
122+
0, /* nb_power */
123+
0, /* nb_negative */
124+
0, /* nb_positive */
125+
0, /* nb_absolute */
126+
0, /* nb_bool */
127+
0, /* nb_invert */
128+
0, /* nb_lshift */
129+
0, /* nb_rshift */
130+
bool_and, /* nb_and */
131+
bool_xor, /* nb_xor */
132+
bool_or, /* nb_or */
133+
0, /* nb_int */
134+
0, /* nb_reserved */
135+
0, /* nb_float */
136+
0, /* nb_inplace_add */
137+
0, /* nb_inplace_subtract */
138+
0, /* nb_inplace_multiply */
139+
0, /* nb_inplace_remainder */
140+
0, /* nb_inplace_power */
141+
0, /* nb_inplace_lshift */
142+
0, /* nb_inplace_rshift */
143+
0, /* nb_inplace_and */
144+
0, /* nb_inplace_xor */
145+
0, /* nb_inplace_or */
146+
0, /* nb_floor_divide */
147+
0, /* nb_true_divide */
148+
0, /* nb_inplace_floor_divide */
149+
0, /* nb_inplace_true_divide */
150+
0, /* nb_index */
151+
};
152+
153+
static void _Py_NO_RETURN
154+
bool_dealloc(PyObject* Py_UNUSED(ignore))
155+
{
156+
_Py_FatalRefcountError("deallocating True or False");
157+
}
158+
159+
/* The type object for bool. Note that this cannot be subclassed! */
160+
161+
PyTypeObject PyBool_Type = {
162+
PyVarObject_HEAD_INIT(&PyType_Type, 0)
163+
"bool",
164+
sizeof(struct _longobject),
165+
0,
166+
bool_dealloc, /* tp_dealloc */
167+
0, /* tp_vectorcall_offset */
168+
0, /* tp_getattr */
169+
0, /* tp_setattr */
170+
0, /* tp_as_async */
171+
bool_repr, /* tp_repr */
172+
&bool_as_number, /* tp_as_number */
173+
0, /* tp_as_sequence */
174+
0, /* tp_as_mapping */
175+
0, /* tp_hash */
176+
0, /* tp_call */
177+
0, /* tp_str */
178+
0, /* tp_getattro */
179+
0, /* tp_setattro */
180+
0, /* tp_as_buffer */
181+
Py_TPFLAGS_DEFAULT, /* tp_flags */
182+
bool_doc, /* tp_doc */
183+
0, /* tp_traverse */
184+
0, /* tp_clear */
185+
0, /* tp_richcompare */
186+
0, /* tp_weaklistoffset */
187+
0, /* tp_iter */
188+
0, /* tp_iternext */
189+
0, /* tp_methods */
190+
0, /* tp_members */
191+
0, /* tp_getset */
192+
&PyLong_Type, /* tp_base */
193+
0, /* tp_dict */
194+
0, /* tp_descr_get */
195+
0, /* tp_descr_set */
196+
0, /* tp_dictoffset */
197+
0, /* tp_init */
198+
0, /* tp_alloc */
199+
bool_new, /* tp_new */
200+
.tp_vectorcall = bool_vectorcall,
201+
};
202+
203+
/* The objects representing bool values False and True */
204+
205+
struct _longobject _Py_FalseStruct = {
206+
PyVarObject_HEAD_INIT(&PyBool_Type, 0)
207+
{ 0 }
208+
};
209+
210+
struct _longobject _Py_TrueStruct = {
211+
PyVarObject_HEAD_INIT(&PyBool_Type, 1)
212+
{ 1 }
213+
};
214+
#endif // GraalPy change

mx.graalpython/copyrights/overrides

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ graalpython/com.oracle.graal.python.cext/modules/testcapi_long.h,python.copyrigh
280280
graalpython/com.oracle.graal.python.cext/modules/unicodedata_db.h,python.copyright
281281
graalpython/com.oracle.graal.python.cext/modules/unicodename_db.h,python.copyright
282282
graalpython/com.oracle.graal.python.cext/src/abstract.c,python.copyright
283+
graalpython/com.oracle.graal.python.cext/src/boolobject.c,python.copyright
283284
graalpython/com.oracle.graal.python.cext/src/bytesobject.c,python.copyright
284285
graalpython/com.oracle.graal.python.cext/src/bytearrayobject.c,python.copyright
285286
graalpython/com.oracle.graal.python.cext/src/call.c,python.copyright

0 commit comments

Comments
 (0)