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
2 changes: 1 addition & 1 deletion Lib/multiprocessing/dummy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class DummyProcess(threading.Thread):

def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
threading.Thread.__init__(self, group, target, name, args, kwargs)
self._pid = None
self._children = weakref.WeakKeyDictionary()
Expand Down
4 changes: 2 additions & 2 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BaseProcess(object):
def _Popen(self):
raise NotImplementedError

def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None,
*, daemon=None):
assert group is None, 'group argument must be None for now'
count = next(_process_counter)
Expand All @@ -89,7 +89,7 @@ def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
self._closed = False
self._target = target
self._args = tuple(args)
self._kwargs = dict(kwargs)
self._kwargs = dict(kwargs) if kwargs else {}
self._name = name or type(self).__name__ + '-' + \
':'.join(str(i) for i in self._identity)
if daemon is not None:
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5278,6 +5278,23 @@ def test_invalid_handles(self):
multiprocessing.connection.Connection, -1)


#
# Regression tests for BaseProcess kwargs handling
#

class TestBaseProcessKwargs(unittest.TestCase):
def test_default_kwargs_not_shared_between_instances(self):
# Creating multiple Process instances without passing kwargs
# must create independent empty dicts (no shared state).
p1 = multiprocessing.Process(target=lambda: None)
p2 = multiprocessing.Process(target=lambda: None)
self.assertIsInstance(p1._kwargs, dict)
self.assertIsInstance(p2._kwargs, dict)
self.assertIsNot(p1._kwargs, p2._kwargs)
# Mutating one should not affect the other
p1._kwargs['x'] = 1
self.assertNotIn('x', p2._kwargs)


@hashlib_helper.requires_hashdigest('sha256')
class OtherTest(unittest.TestCase):
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,9 @@ def __rmul__(self, other):

# Error cases that arose during development
args = ((-5, -5, 10), (1.5, 4611686018427387904, 2305843009213693952))
self.assertEqual(sumprod(*args), 0.0)
self.assertEqual(sumprod(*args), -7.5)
self.assertEqual(sumprod([-0.01, 1, -1, 0.01], [1, 1, 1, 1]), 0.0)
self.assertEqual(sumprod([1, 1, 1, 1], [-0.01, 1, -1, 0.01], ), 0.0)

@requires_IEEE_754
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
Expand Down
46 changes: 46 additions & 0 deletions Lib/test/test_peg_generator/test_pegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,3 +1106,49 @@ def test_deep_nested_rule(self) -> None:
)

self.assertEqual(output, expected_output)

def test_rule_flags(self) -> None:
"""Test the new rule flags syntax that accepts arbitrary lists of flags."""
# Test grammar with various flag combinations
grammar_source = """
start: simple_rule

simple_rule (memo):
| "hello"

multi_flag_rule (memo, custom, test):
| "world"

single_custom_flag (custom):
| "test"

no_flags_rule:
| "plain"
"""

grammar: Grammar = parse_string(grammar_source, GrammarParser)
rules = grammar.rules

# Test memo-only rule
simple_rule = rules['simple_rule']
self.assertTrue(simple_rule.memo, "simple_rule should have memo=True")
self.assertEqual(simple_rule.flags, frozenset(['memo']),
f"simple_rule flags should be {'memo'}, got {simple_rule.flags}")

# Test multi-flag rule
multi_flag_rule = rules['multi_flag_rule']
self.assertTrue(multi_flag_rule.memo, "multi_flag_rule should have memo=True")
self.assertEqual(multi_flag_rule.flags, frozenset({'memo', 'custom', 'test'}),
f"multi_flag_rule flags should contain memo, custom, test, got {multi_flag_rule.flags}")

# Test single custom flag rule
single_custom_rule = rules['single_custom_flag']
self.assertFalse(single_custom_rule.memo, "single_custom_flag should have memo=False")
self.assertEqual(single_custom_rule.flags, frozenset(['custom']),
f"single_custom_flag flags should be {'custom'}, got {single_custom_rule.flags}")

# Test no flags rule
no_flags_rule = rules['no_flags_rule']
self.assertFalse(no_flags_rule.memo, "no_flags_rule should have memo=False")
self.assertEqual(no_flags_rule.flags, [],
f"no_flags_rule flags should be the empty set, got {no_flags_rule.flags}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a missing case in :func:`math.sumprod` where a low precision path was
taken when an int/int input pair followed a float input.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`!multiprocessing.BaseProcess` defaults ``kwargs`` to ``None`` instead of a shared dictionary.
29 changes: 14 additions & 15 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2937,32 +2937,31 @@ math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q)

if (!finished) {
double flt_p, flt_q;
bool p_type_float = PyFloat_CheckExact(p_i);
bool q_type_float = PyFloat_CheckExact(q_i);
if (p_type_float && q_type_float) {
flt_p = PyFloat_AS_DOUBLE(p_i);
flt_q = PyFloat_AS_DOUBLE(q_i);
} else if (p_type_float && (PyLong_CheckExact(q_i) || PyBool_Check(q_i))) {
/* We care about float/int pairs and int/float pairs because
they arise naturally in several use cases such as price
times quantity, measurements with integer weights, or
data selected by a vector of bools. */

if (PyFloat_CheckExact(p_i)) {
flt_p = PyFloat_AS_DOUBLE(p_i);
flt_q = PyLong_AsDouble(q_i);
if (flt_q == -1.0 && PyErr_Occurred()) {
} else if (PyLong_CheckExact(p_i) || PyBool_Check(p_i)) {
flt_p = PyLong_AsDouble(p_i);
if (flt_p == -1.0 && PyErr_Occurred()) {
PyErr_Clear();
goto finalize_flt_path;
}
} else if (q_type_float && (PyLong_CheckExact(p_i) || PyBool_Check(p_i))) {
} else {
goto finalize_flt_path;
}

if (PyFloat_CheckExact(q_i)) {
flt_q = PyFloat_AS_DOUBLE(q_i);
flt_p = PyLong_AsDouble(p_i);
if (flt_p == -1.0 && PyErr_Occurred()) {
} else if (PyLong_CheckExact(q_i) || PyBool_Check(q_i)) {
flt_q = PyLong_AsDouble(q_i);
if (flt_q == -1.0 && PyErr_Occurred()) {
PyErr_Clear();
goto finalize_flt_path;
}
} else {
goto finalize_flt_path;
}

TripleLength new_flt_total = tl_fma(flt_p, flt_q, flt_total);
if (isfinite(new_flt_total.hi)) {
flt_total = new_flt_total;
Expand Down
2 changes: 1 addition & 1 deletion Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def _set_up_rule_memoization(self, node: Rule, result_type: str) -> None:
self.print(f"{node.name}_raw(Parser *p)")

def _should_memoize(self, node: Rule) -> bool:
return node.memo and not node.left_recursive
return "memo" in node.flags and not node.left_recursive

def _handle_default_rule_body(self, node: Rule, rhs: Rhs, result_type: str) -> None:
memoize = self._should_memoize(node)
Expand Down
6 changes: 2 additions & 4 deletions Tools/peg_generator/pegen/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def __iter__(self) -> Iterator[Rule]:


class Rule:
def __init__(self, name: str, type: str | None, rhs: Rhs, memo: object | None = None):
def __init__(self, name: str, type: str | None, rhs: Rhs, flags: frozenset[str] | None = None):
self.name = name
self.type = type
self.rhs = rhs
self.memo = bool(memo)
self.flags = flags or frozenset()
self.left_recursive = False
self.leader = False

Expand Down Expand Up @@ -135,7 +135,6 @@ def __repr__(self) -> str:
class Rhs:
def __init__(self, alts: list[Alt]):
self.alts = alts
self.memo: tuple[str | None, str] | None = None

def __str__(self) -> str:
return " | ".join(str(alt) for alt in self.alts)
Expand Down Expand Up @@ -263,7 +262,6 @@ class Repeat:

def __init__(self, node: Plain):
self.node = node
self.memo: tuple[str | None, str] | None = None

def __iter__(self) -> Iterator[Plain]:
yield self.node
Expand Down
67 changes: 54 additions & 13 deletions Tools/peg_generator/pegen/grammar_parser.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions Tools/peg_generator/pegen/metagrammar.gram
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@ rules[RuleList]:
| rule { [rule] }

rule[Rule]:
| rulename memoflag? ":" alts NEWLINE INDENT more_alts DEDENT {
Rule(rulename[0], rulename[1], Rhs(alts.alts + more_alts.alts), memo=opt) }
| rulename memoflag? ":" NEWLINE INDENT more_alts DEDENT {
Rule(rulename[0], rulename[1], more_alts, memo=opt) }
| rulename memoflag? ":" alts NEWLINE { Rule(rulename[0], rulename[1], alts, memo=opt) }
| rulename flags=flags? ":" alts NEWLINE INDENT more_alts DEDENT {
Rule(rulename[0], rulename[1], Rhs(alts.alts + more_alts.alts), flags=flags) }
| rulename flags=flags? ":" NEWLINE INDENT more_alts DEDENT {
Rule(rulename[0], rulename[1], more_alts, flags=flags) }
| rulename flags=flags? ":" alts NEWLINE { Rule(rulename[0], rulename[1], alts, flags=flags) }

rulename[RuleName]:
| NAME annotation { (name.string, annotation) }
| NAME { (name.string, None) }

# In the future this may return something more complicated
memoflag[str]:
| '(' "memo" ')' { "memo" }
flags[frozenset[str]]:
| '(' a=','.flag+ ')' { frozenset(a) }

flag[str]:
| NAME { name.string }

alts[Rhs]:
| alt "|" alts { Rhs([alt] + alts.alts)}
Expand Down
Loading