Skip to content
Merged
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
6 changes: 5 additions & 1 deletion Lib/_ast_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,11 @@ def visit_Attribute(self, node):
# Special case: 3.__abs__() is a syntax error, so if node.value
# is an integer literal then we need to either parenthesize
# it or add an extra space to get 3 .__abs__().
if isinstance(node.value, Constant) and isinstance(node.value.value, int):
# bool is a subclass of int, but True.real and False.real are
# valid without a space.
if (isinstance(node.value, Constant)
and isinstance(node.value.value, int)
and not isinstance(node.value.value, bool)):
self.write(" ")
self.write(".")
self.write(node.attr)
Expand Down
20 changes: 5 additions & 15 deletions Lib/test/test_cext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
os.path.join(SOURCE_DIR, 'extension.cpp'),
os.path.join(SOURCE_DIR, 'setup.py'),
]
RUNTESTS_PY = os.path.join(SOURCE_DIR, 'runtests.py')
MSVC = support.MS_WINDOWS


Expand Down Expand Up @@ -99,21 +100,10 @@ def run_cmd(operation, cmd):
cmd.append('-v')
run_cmd('Install', cmd)

# Do a reference run. Until we test that running python
# doesn't leak references (gh-94755), run it so one can manually check
# -X showrefcount results against this baseline.
cmd = [python_exe,
'-X', 'dev',
'-X', 'showrefcount',
'-c', 'pass']
run_cmd('Reference run', cmd)

# Import the C/C++ extension
cmd = [python_exe,
'-X', 'dev',
'-X', 'showrefcount',
'-c', f"import {extension_name}"]
run_cmd('Import', cmd)
# Import the extension module and run tests.
# On a debug build, check also for reference leaks.
cmd = [python_exe, '-X', 'dev', RUNTESTS_PY, extension_name]
run_cmd('Tests', cmd)


class TestPublicC(BaseTests, unittest.TestCase):
Expand Down
41 changes: 14 additions & 27 deletions Lib/test/test_cext/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ class VirtualPyObject : public PyObject {
public:
VirtualPyObject();
virtual ~VirtualPyObject() {
PyTypeObject *type = Py_TYPE(this);
delete [] internal_data;
--instance_count;
// Do not call type->tp_free(this), C++ manages the memory
Py_DECREF(type);
}
virtual void set_internal_data() {
internal_data[0] = 1;
Expand Down Expand Up @@ -295,7 +298,7 @@ _Py_COMP_DIAG_PUSH
#endif

PyType_Slot VirtualPyObject_Slots[] = {
{Py_tp_free, (void*)VirtualPyObject::dealloc},
{Py_tp_dealloc, (void*)VirtualPyObject::dealloc},
{0, _Py_NULL},
};

Expand Down Expand Up @@ -333,6 +336,10 @@ test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
"instance_count should be 0, got %d",
VirtualPyObject::instance_count);
}

// Force a garbage collection to delete the temporary heap type
// used by this test
PyGC_Collect();
Py_RETURN_NONE;
}
#endif // __cplusplus && !Py_TARGET_ABI3T
Expand All @@ -356,8 +363,6 @@ static PyMethodDef module_methods[] = {
static int
module_exec(PyObject *module)
{
PyObject *result;

#ifdef __STDC_VERSION__
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
return -1;
Expand All @@ -368,31 +373,13 @@ module_exec(PyObject *module)
return -1;
}
#endif

result = PyObject_CallMethod(module, "test_macros", "");
if (!result) return -1;
Py_DECREF(result);

result = PyObject_CallMethod(module, "test_datetime", "");
if (!result) return -1;
Py_DECREF(result);

result = PyObject_CallMethod(module, "test_unicode", "");
if (!result) return -1;
Py_DECREF(result);

#ifdef __cplusplus
result = PyObject_CallMethod(module, "test_api_casts", "");
if (!result) return -1;
Py_DECREF(result);
#endif

#if defined(__cplusplus) && !defined(Py_TARGET_ABI3T)
result = PyObject_CallMethod(module, "test_virtual_object", "");
if (!result) return -1;
Py_DECREF(result);
#ifdef _MSVC_LANG
if (PyModule_AddIntMacro(module, _MSVC_LANG) < 0) {
return -1;
}
#endif

// Ignore "unused argument" warning when none of these macros is defined
(void)module;
return 0;
}

Expand Down
63 changes: 63 additions & 0 deletions Lib/test/test_cext/runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import array
import gc
import importlib
import sys


def run_tests(testmod, verbose=True):
for name in dir(testmod):
if not name.startswith('test'):
continue
func = getattr(testmod, name)
print(f"{name}()")
func()

print("add()")
if testmod.add(11, 23) != 34:
raise AssertionError("add() failed badly")

print(flush=True)


def main():
if len(sys.argv) < 2:
print("usage: python runtests.py TEST_MODULE_NAME")
sys.exit(1)
module_name = sys.argv[1]

testmod = importlib.import_module(module_name)

newline = False
for name in ('__STDC_VERSION__', '__cplusplus', '_MSVC_LANG'):
try:
value = getattr(testmod, name)
except AttributeError:
pass
else:
print(f'{name}: {value}')
newline = True
if newline:
print()

if hasattr(sys, 'gettotalrefcount'):
# First run to warm up Python. For example, test_datetime() imports
# the datetime module.
run_tests(testmod, verbose=False)

refcount = array.array('q', [0, 0])
gc.collect()

# Check for reference leak
refcount[0] = sys.gettotalrefcount()
run_tests(testmod)
refcount[1] = sys.gettotalrefcount()

diff = refcount[1] - refcount[0]
if diff >= 1:
raise AssertionError(f'Tests leaked {diff} references')
else:
run_tests(testmod)


if __name__ == "__main__":
main()
8 changes: 8 additions & 0 deletions Lib/test/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ def test_unary_parens(self):
def test_integer_parens(self):
self.check_ast_roundtrip("3 .__abs__()")

def test_attribute_on_bool(self):
# gh-158237: True.real should not gain a space
self.check_src_roundtrip("x = True.real")
self.check_src_roundtrip("x = False.__class__")

def test_attribute_on_int_still_spaced(self):
self.check_src_roundtrip("x = 3 .__abs__()")

def test_huge_float(self):
self.check_ast_roundtrip("1e1000")
self.check_ast_roundtrip("-1e1000")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`ast.unparse` to not insert a space before attribute
access on bool constants (e.g. ``True.real`` instead of
``True .real``).
Loading
Loading