[issue43420] Optimize rational arithmetics

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> 1 loop, best of 11: 257 msec per loop
> $ ... from patched ...
> 10 loops, best of 11: 33.2 msec per loop

Looks worth it :-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41361] Converting collections.deque methods to Argument Clinic

2021-03-06 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

If the argument clinic is too disruptive, would it be okay to inline the 
equivalent code like this?

diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 90bafb0ea8..d75388abc8 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -880,9 +880,19 @@ deque_rotate(dequeobject *deque, PyObject *const *args, 
Py_ssize_t nargs)
 {
 Py_ssize_t n=1;

-if (!_PyArg_ParseStack(args, nargs, "|n:rotate", )) {
+if (!_PyArg_CheckPositional("deque.rotate", nargs, 0, 1)) {
 return NULL;
 }
+if (nargs) {
+PyObject *index = _PyNumber_Index(args[0]);
+if (index == NULL) {
+return NULL;
+}
+n = PyLong_AsSsize_t(index);
+if (n == -1 && PyErr_Occurred()) {
+return NULL;
+}
+}

 if (!_deque_rotate(deque, n))
 Py_RETURN_NONE;

Benchmarks for this change:

.\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate()"
Before: Mean +- std dev: 51.2 ns +- 0.9 ns
After:  Mean +- std dev: 39.3 ns +- 0.3 ns

.\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate(-1)"
Before: Mean +- std dev: 64.5 ns +- 0.3 ns
After:  Mean +- std dev: 46.2 ns +- 0.3 ns

.\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate(1)"
Before: Mean +- std dev: 64.4 ns +- 0.3 ns
After:  Mean +- std dev: 45.7 ns +- 0.2 ns

Similar changes could apply to deque.insert() and deque.index().

--
nosy: +Dennis Sweeney

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43405] DeprecationWarnings in test_unicode

2021-03-06 Thread Inada Naoki


Change by Inada Naoki :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43405] DeprecationWarnings in test_unicode

2021-03-06 Thread Inada Naoki


Inada Naoki  added the comment:


New changeset 8aabfa8550692a76d8a96e138c48faf5a7b2752c by Zackery Spytz in 
branch 'master':
bpo-43405: Fix DeprecationWarnings in test_unicode (GH-24754)
https://github.com/python/cpython/commit/8aabfa8550692a76d8a96e138c48faf5a7b2752c


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Aaron Meurer


Change by Aaron Meurer :


--
nosy: +asmeurer

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41282] Deprecate and remove distutils

2021-03-06 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I have created below issues where deprecation warning is emitted due to 
distutils usage in tests. Probably there are other places that need an update 
to setuptools like setup.py used by make that emits deprecation warning during 
building cpython.

https://bugs.python.org/issue43426
https://bugs.python.org/issue43425

 rg '(from|import) distutils' | rg -v 'Lib/distutils|rst'
Modules/_decimal/tests/formathelper.py:from distutils.spawn import 
find_executable
Doc/includes/setup.py:from distutils.core import setup, Extension
Doc/includes/test.py:from distutils.util import get_platform
setup.py:from distutils import log
setup.py:from distutils.command.build_ext import build_ext
setup.py:from distutils.command.build_scripts import build_scripts
setup.py:from distutils.command.install import install
setup.py:from distutils.command.install_lib import install_lib
setup.py:from distutils.core import Extension, setup
setup.py:from distutils.errors import CCompilerError, DistutilsError
setup.py:from distutils.spawn import find_executable
Lib/_osx_support.py:from distutils import log
Lib/_osx_support.py:Currently called from distutils.sysconfig
Lib/test/support/__init__.py:from distutils import ccompiler, sysconfig, 
spawn, errors
Lib/test/test_distutils.py:import distutils.tests
Lib/test/test_importlib/test_windows.py:from distutils.util import get_platform
Lib/test/test_peg_generator/test_c_parser.py:from distutils.tests.support 
import TempdirManager
Lib/test/test_sundry.py:import distutils.bcppcompiler
Lib/test/test_sundry.py:import distutils.ccompiler
Lib/test/test_sundry.py:import distutils.cygwinccompiler
Lib/test/test_sundry.py:import distutils.filelist
Lib/test/test_sundry.py:import distutils.text_file
Lib/test/test_sundry.py:import distutils.unixccompiler
Lib/test/test_sundry.py:import distutils.command.bdist_dumb
Lib/test/test_sundry.py:import distutils.command.bdist_msi
Lib/test/test_sundry.py:import distutils.command.bdist
Lib/test/test_sundry.py:import distutils.command.bdist_rpm
Lib/test/test_sundry.py:import distutils.command.build_clib
Lib/test/test_sundry.py:import distutils.command.build_ext
Lib/test/test_sundry.py:import distutils.command.build
Lib/test/test_sundry.py:import distutils.command.clean
Lib/test/test_sundry.py:import distutils.command.config
Lib/test/test_sundry.py:import distutils.command.install_data
Lib/test/test_sundry.py:import distutils.command.install_egg_info
Lib/test/test_sundry.py:import distutils.command.install_headers
Lib/test/test_sundry.py:import distutils.command.install_lib
Lib/test/test_sundry.py:import distutils.command.register
Lib/test/test_sundry.py:import distutils.command.sdist
Lib/test/test_sundry.py:import distutils.command.upload
Tools/peg_generator/pegen/build.py:import distutils.log
Tools/peg_generator/pegen/build.py:from distutils.core import Distribution, 
Extension
Tools/peg_generator/pegen/build.py:from distutils.command.clean import 
clean  # type: ignore
Tools/peg_generator/pegen/build.py:from distutils.command.build_ext import 
build_ext  # type: ignore
Tools/peg_generator/pegen/build.py:from distutils.tests.support import 
fixup_build_ext  # type: ignore
Tools/c-analyzer/c_parser/preprocessor/common.py:import distutils.ccompiler
Tools/c-analyzer/c_parser/preprocessor/__init__.py:import distutils.ccompiler
Tools/test2to3/setup.py:from distutils.core import setup
Tools/test2to3/setup.py:from distutils.command.build_py import 
build_py_2to3 as build_py
Tools/test2to3/setup.py:from distutils.command.build_py import build_py
Tools/test2to3/setup.py:from distutils.command.build_scripts import 
build_scripts_2to3 as build_scripts
Tools/test2to3/setup.py:from distutils.command.build_scripts import 
build_scripts
Tools/test2to3/test/runtests.py:from distutils.util import copydir_run_2to3
Misc/HISTORY:- Issue #5394: removed > 2.3 syntax from distutils.msvc9compiler.

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43426] test_importlib.test_windows emits deprecation warning over usage of distutils

2021-03-06 Thread Karthikeyan Singaravelan


New submission from Karthikeyan Singaravelan :

test_windows uses distutil which emits a deprecation warning due to distutils 
being deprecated. sysconfig.get_platform and distutils.util.get_host_platform 
seem to be identical though distutils.util.get_platform has an extra if clause 
for nt systems. This is related to https://bugs.python.org/issue41282

./python -Wall -m test test_importlib.test_windows
0:00:00 load avg: 0.00 Run tests sequentially
0:00:00 load avg: 0.00 [1/1] test_importlib.test_windows
/root/cpython/Lib/test/test_importlib/test_windows.py:10: DeprecationWarning: 
The distutils package is deprecated and slated for removal in Python 3.12. Use 
setuptools or check PEP 632 for potential alternatives
  from distutils.util import get_platform
test_importlib.test_windows skipped -- No module named 'winreg'
test_importlib.test_windows skipped

== Tests result: SUCCESS ==

1 test skipped:
test_importlib.test_windows

Total duration: 56 ms
Tests result: SUCCESS

--
components: Tests, Windows
messages: 388223
nosy: paul.moore, steve.dower, tim.golden, xtreak, zach.ware
priority: normal
severity: normal
status: open
title: test_importlib.test_windows emits deprecation warning over usage of 
distutils
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43425] test_peg_generator.test_c_parser emits DeprecationWarning due to distutils

2021-03-06 Thread Karthikeyan Singaravelan


New submission from Karthikeyan Singaravelan :

distutils was deprecated for removal in Python 3.10. It is used in 
test_peg_generator.test_c_parser which emits a deprecation warning. It also 
seems to be used in test_support for missing_compiler_executable that will emit 
a deprecation warning.

./python -Wall -m test test_peg_generator test_c_parser
0:00:00 load avg: 0.02 Run tests sequentially
0:00:00 load avg: 0.02 [1/2] test_peg_generator
/root/cpython/Lib/test/test_peg_generator/test_c_parser.py:4: 
DeprecationWarning: The distutils package is deprecated and slated for removal 
in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.tests.support import TempdirManager


Other places on grep : 

rg 'from distutils' | rg -v 'Lib/distutils|rst'   
Modules/_decimal/tests/formathelper.py:from distutils.spawn import 
find_executable
Doc/includes/setup.py:from distutils.core import setup, Extension
Doc/includes/test.py:from distutils.util import get_platform
setup.py:from distutils import log
setup.py:from distutils.command.build_ext import build_ext
setup.py:from distutils.command.build_scripts import build_scripts
setup.py:from distutils.command.install import install
setup.py:from distutils.command.install_lib import install_lib
setup.py:from distutils.core import Extension, setup
setup.py:from distutils.errors import CCompilerError, DistutilsError
setup.py:from distutils.spawn import find_executable
Lib/_osx_support.py:from distutils import log
Lib/_osx_support.py:Currently called from distutils.sysconfig
Lib/test/support/__init__.py:from distutils import ccompiler, sysconfig, 
spawn, errors
Lib/test/test_importlib/test_windows.py:from distutils.util import get_platform
Lib/test/test_peg_generator/test_c_parser.py:from distutils.tests.support 
import TempdirManager
Tools/peg_generator/pegen/build.py:from distutils.core import Distribution, 
Extension
Tools/peg_generator/pegen/build.py:from distutils.command.clean import 
clean  # type: ignore
Tools/peg_generator/pegen/build.py:from distutils.command.build_ext import 
build_ext  # type: ignore
Tools/peg_generator/pegen/build.py:from distutils.tests.support import 
fixup_build_ext  # type: ignore
Tools/test2to3/setup.py:from distutils.core import setup
Tools/test2to3/setup.py:from distutils.command.build_py import 
build_py_2to3 as build_py
Tools/test2to3/setup.py:from distutils.command.build_py import build_py
Tools/test2to3/setup.py:from distutils.command.build_scripts import 
build_scripts_2to3 as build_scripts
Tools/test2to3/setup.py:from distutils.command.build_scripts import 
build_scripts
Tools/test2to3/test/runtests.py:from distutils.util import copydir_run_2to3
Misc/HISTORY:- Issue #5394: removed > 2.3 syntax from distutils.msvc9compiler.

--
components: Tests
messages: 388222
nosy: pablogsal, xtreak
priority: normal
severity: normal
status: open
title: test_peg_generator.test_c_parser emits DeprecationWarning due to 
distutils
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Sergey B Kirpichev


Change by Sergey B Kirpichev :


--
pull_requests: +23544
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24779

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Sergey B Kirpichev


Sergey B Kirpichev  added the comment:

I have similar timings (for a draft version of PR, see f.patch) as for the last 
comment, though the small-dens overhead seems to be bigger(~20%):
$ python3.10 -m timeit -r11 -s 'from fractions import Fraction as F' -s 
'a=F(10,3)' -s 'b=F(6, 5)' 'a * b'
5 loops, best of 11: 9.09 usec per loop
$ python3.10 -m timeit -r11 -s 'from patched import Fraction as F' -s 
'a=F(10,3)' -s 'b=F(6, 5)' 'a * b'
2 loops, best of 11: 11.2 usec per loop

On another hand, here are timings for bigger denominators:
$ python3.10 -m timeit -r11 -s 'from fractions import Fraction as F' -s 'import 
random' -s 'n = [random.randint(1, 100) for _ in range(1000)]' -s 'd = 
[random.randint(1, 100) for _ in range(1000)]' -s 'a=list(map(lambda x: 
F(*x), zip(n, d)))' 'sum(a)'
1 loop, best of 11: 257 msec per loop
$ ... from patched ...
10 loops, best of 11: 33.2 msec per loop

It's not so clear what "are very large" does mean, that could be defined here.  
BTW, 10**6 denominators are (very!) small for mentioned above use case (CAS 
package).

--
keywords: +patch
Added file: https://bugs.python.org/file49854/f.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25024] Allow passing "delete=False" to TemporaryDirectory

2021-03-06 Thread C.A.M. Gerlach


C.A.M. Gerlach  added the comment:

To note, the proposal on [BPO-29982](https://bugs.python.org/issue29982) should 
hopefully address one of the two use-cases described by Anthony Sotittle, 
`ignore_errors=True`, in a cleaner fashion that takes advantage of the existing 
higher-level functionality rather than duplicating `mkdtemp`. Opinions and 
feedback welcome over there.

--
nosy: +CAM-Gerlach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29982] tempfile.TemporaryDirectory fails to delete itself

2021-03-06 Thread C.A.M. Gerlach


C.A.M. Gerlach  added the comment:

In addition to transient failures, this can also occur when, for example, files 
opened in the temporary directory (perhaps by library or application code not 
under direct control of the caller) haven't been properly cleaned up and their 
file handles don't get closed, resulting in permissions errors trying to delete 
them (particularly on platforms like Windows, that automatically lock files 
when opening them).

This case came up in e.g. [this recent 
PR](https://github.com/regebro/pyroma/pull/57) and rendered 
`tempfile.TemporaryDirectory` unusable for such use cases, forcing a reversion 
to the lower-level `tempfile.mkdtemp` without the cleaner, more robust and 
easier to interpret high-level interface that the former provides. Wrapping a 
`with` statement in a try-finally is syntactically ugly and semantically 
incongruous, and requires a second shutil.rmtree(..., ignore_errors=True)` call 
to clean up in a best-effort manner, while when manually calling `cleanup()` in 
a try-except, the finalizer still gets executed at a a non-deterministic later 
time when Python exits, raising an error.

Therefore, in the spirit of Guido's statements above in terms of providing a 
"best-effort" cleanup, I propose (and am willing to submit a PR to implement) 
adding an `ignore_errors` bool parameter (defaulting to False, of course, for 
backward compat--and should it be keyword only like `errors` to 
`TemporaryFile`?) to the `tempfile.TemporaryDirectory` constructor, which gets  
passed to `shutil.rmtree` on cleanup. This would not only address both cases 
here, but also one of the two discussed by Anthony Sotitle on 
[BPO-25024](https://bugs.python.org/issue25024), in a cleaner and simpler 
fashion that would take advantage of existing `tempfile.TemporaryDirectory` 
functionality and behavior.

Would a PR be accepted on this? If so, any specific guidance on tests and 
whether to mention it in What's New, etc., would be appreciated. Thanks!

--
nosy: +CAM-Gerlach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43424] Document the `controller.name` field in `webbrowser` module

2021-03-06 Thread Ilya Grigoriev


New submission from Ilya Grigoriev :

The object `webbrowser.get()` returns has, and had for a long time, a useful 
but undocumented field `name`. I wonder if it would be OK to document it as 
something like `a system-dependent name for the browser`. This would go here:

https://docs.python.org/3/library/webbrowser.html#browser-controller-objects

The reason I'd like this is so that I can write code like the following:

```python
# In Crostini Chrome OS Linux, the default browser is set to an
# intermediary called `garcon-url-handler`.
# It opens URLs in Chrome running outside the linux VM. This 
# browser does not have access to the Linux filesystem. Some references:
# 
https://chromium.googlesource.com/chromiumos/platform2/+/master/vm_tools/garcon/#opening-urls
# https://source.chromium.org/search?q=garcon-url-handler

if "garcon-url-handler" in webbrowser.get().name:
  webbrowser.open("http://external-url.com/docs.html;)
else:
  webbrowser.open("file:///usr/share/doc/docs.html")
```

This would work correctly, even if the user has installed a browser native to 
the Linux VM and put it into their `BROWSER` environment variable. I don't know 
a better way to achieve the same effect.

Some references to where the `name` field was introduced:

https://bugs.python.org/issue754022

https://github.com/python/cpython/commit/e8f244305ef4f257f6999b69601f4316b31faa5e

--
assignee: docs@python
components: Documentation, Library (Lib)
messages: 388218
nosy: docs@python, ilyagr
priority: normal
severity: normal
status: open
title: Document the `controller.name` field in `webbrowser` module
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40982] copytree example in shutil

2021-03-06 Thread Zackery Spytz


Change by Zackery Spytz :


--
keywords: +patch
nosy: +ZackerySpytz
nosy_count: 3.0 -> 4.0
pull_requests: +23543
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24778

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42246] Implement PEP 626 -- Precise line numbers for debugging

2021-03-06 Thread Neil Schemenauer


Neil Schemenauer  added the comment:


New changeset 87ec26b812e9c4095c017dc60f246eda37b83ab2 by Neil Schemenauer in 
branch 'master':
bpo-43372: Use _freeze_importlib for regen-frozen. (GH-24759)
https://github.com/python/cpython/commit/87ec26b812e9c4095c017dc60f246eda37b83ab2


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43372] ctypes: test_frozentable fails when make regen-frozen

2021-03-06 Thread Neil Schemenauer


Change by Neil Schemenauer :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43372] ctypes: test_frozentable fails when make regen-frozen

2021-03-06 Thread Neil Schemenauer


Neil Schemenauer  added the comment:


New changeset 87ec26b812e9c4095c017dc60f246eda37b83ab2 by Neil Schemenauer in 
branch 'master':
bpo-43372: Use _freeze_importlib for regen-frozen. (GH-24759)
https://github.com/python/cpython/commit/87ec26b812e9c4095c017dc60f246eda37b83ab2


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Without the guards the incremental cost drops to 10%.

$ python3.10 -m timeit -r11 -s 'from patched_noguards import Fraction as F' -s 
'a=F(10,3)' -s 'b=F(6, 5)' 'a * b'
10 loops, best of 11: 2.02 usec per loop

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The cost to the common case for small components is about 20%:

$ python3.10 -m timeit -r11 -s 'from fractions import Fraction as F' -s 
'a=F(10,3)' -s 'b=F(6, 5)' 'a * b'
20 loops, best of 11: 1.8 usec per loop

$ python3.10 -m timeit -r11 -s 'from patched import Fraction as F' -s 
'a=F(10,3)' -s 'b=F(6, 5)' 'a * b'
10 loops, best of 11: 2.14 usec per loop

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43216] Removal of @asyncio.coroutine in Python 3.10

2021-03-06 Thread Thomas Grainger


Change by Thomas Grainger :


--
nosy: +graingert

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43423] Subprocess IndexError possible in _communicate

2021-03-06 Thread Eryk Sun


Eryk Sun  added the comment:

The presumption I suppose is that these statements only execute if 
self.stdout_thread and/or self.stderr_thread completes successfully. I suppose 
that the read could fail or get canceled via CancelSynchronousIo(). Of course 
in that case you have a bigger problem than an IndexError.

On a related note, _communicate() needs significant changes in Windows. See 
bpo-43346 if you're interested.

--
components: +Windows
nosy: +eryksun, paul.moore, steve.dower, tim.golden, zach.ware

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Note that Fraction arithmetic has a huge administrative overhead. The cost of 
the underlying multiplications and divisions won't dominate the total time 
until the numerators and denominators are very large.

For the proposed optimization, this implies that cost for the extra Python 
steps to implement the optimization will be negligible.  The benefits of the 
optimization are similarly attenuated.

-- Update to experimentation code: add guards for the relatively prime case. --

class Henrici(Fraction):
'Reformulate _mul to reduce the size of intermediate products'
def _mul(a, b):
a_n, a_d = a.numerator, a.denominator
b_n, b_d = b.numerator, b.denominator
d1 = math.gcd(a_n, b_d)
if d1 > 1:
a_n //= d1
b_d //= d1
d2 = math.gcd(b_n, a_d)
if d2 > 1:
b_n //= d2
a_d //= d2
result = Fraction(a_n * b_n, a_d * b_d, _normalize=False)
assert math.gcd(a_n * b_n, a_d * b_d) == 1 and a_d * b_d >= 0
return result

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Mark Dickinson


Change by Mark Dickinson :


--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43423] Subprocess IndexError possible in _communicate

2021-03-06 Thread Chris Griffith


Change by Chris Griffith :


--
keywords: +patch
pull_requests: +23542
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24777

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43423] Subprocess IndexError possible in _communicate

2021-03-06 Thread Chris Griffith


New submission from Chris Griffith :

It is possible to run into an IndexError in the subprocess module's 
_communicate function.

```
return run(
  File "subprocess.py", line 491, in run
  File "subprocess.py", line 1024, in communicate
  File "subprocess.py", line 1418, in _communicate
IndexError: list index out of range
```

The lines in question are: 

```
if stdout is not None:
stdout = stdout[0]
if stderr is not None:
stderr = stderr[0]
```

I believe this is due to the fact there is no safety checking to make sure that 
self._stdout_buff and self._stderr_buff have any content in them after being 
set to empty lists. 

The fix I suggest is to change the checks from `if stdout is not None` to 
simply `if stdout` to make sure it is a populated list. 

Example fixed code: 

```
if stdout:
stdout = stdout[0]
if stderr:
stderr = stderr[0]
```

If a more stringent check is required, we could expand that out to check type 
and length, such as `isinstance(stdout, list) and len(stdout) > 0:` but that is 
more then necessary currently.

--
components: Library (Lib)
messages: 388211
nosy: cdgriffith
priority: normal
severity: normal
status: open
title: Subprocess IndexError possible in _communicate
type: crash
versions: Python 3.10, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43245] Add keyword argument support to ChainMap.new_child()

2021-03-06 Thread Kamil Turek


Change by Kamil Turek :


--
nosy: +kamilturek

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37771] No equivalent of `inspect.getcoroutinestate` for PyAsyncGenASend instances

2021-03-06 Thread Thomas Grainger


Change by Thomas Grainger :


--
nosy: +alex.gronholm, graingert

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Here's some code to try out:

from math import gcd
from fractions import Fraction
import operator
import math

class Henrici(Fraction):
'Reformulate _mul to reduce the size of intermediate products'

# Original has 2 multiplications, 1 gcd calls, and 2 divisions
# This one has 2 multiplications, 2 gcd calls, and 4 divisions  

def _mul(a, b):
a_n, a_d = a.numerator, a.denominator
b_n, b_d = b.numerator, b.denominator
d1 = math.gcd(a_n, b_d)
a_n //= d1
b_d //= d1
d2 = math.gcd(b_n, a_d)
b_n //= d2
a_d //= d2
result = Fraction(a_n * b_n, a_d * b_d, _normalize=False)
assert math.gcd(a_n * b_n, a_d * b_d) == 1 and a_d * b_d >= 0
return result

__mul__, __rmul__ = Fraction._operator_fallbacks(_mul, operator.mul)

assert Henrici(10, 3) * Henrici(6, 5) == Henrici(4, 1)

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16322] time.tzname on Python 3.3.0 for Windows is decoded by wrong encoding

2021-03-06 Thread Eryk Sun


Eryk Sun  added the comment:

The solution for bpo-36779 changed init_timezone() to get tzname directly from 
WinAPI GetTimeZoneInformation(). 

Unfortunately the implementer didn't think to also support time.tzset(), so the 
value may be stale with no way to refresh it, or possibly different from what 
time.strftime('%Z') returns, depending on when ucrt looks up the timezone. For 
example, start Python and import time. Then change the time zone and call 
time.strftime('%Z'). The value will be different from time.tzname.

--
resolution:  -> duplicate
stage: patch review -> resolved
status: open -> closed
superseder:  -> time.tzname returns empty string on Windows if default codepage 
is a Unicode codepage

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43422] Revert _decimal C API changes

2021-03-06 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

+1 fron me.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30295] msvcrt SetErrorMode not documented

2021-03-06 Thread Eryk Sun


Change by Eryk Sun :


--
components: +Library (Lib)
type:  -> enhancement
versions: +Python 3.10, Python 3.8, Python 3.9 -Python 3.4, Python 3.5, Python 
3.6, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14841] os.get_terminal_size() should check stdin as a fallback

2021-03-06 Thread Eryk Sun


Change by Eryk Sun :


--
components: +Extension Modules, Library (Lib)
versions: +Python 3.10, Python 3.9 -Python 3.4, Python 3.5, Python 3.6, Python 
3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43413] tuple subclasses allow kwargs

2021-03-06 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

> Hmm, but in my experience, tuple on Python 3.6 doesn't take keyword arguments 
> either:

They do.

>>> tuple(sequence='abc')
('a', 'b', 'c')

>>> list(sequence='abc')
['a', 'b', 'c']
>>> int(x='123')
123
>>> bool(x='123')
True
>>> float(x='123')
123.0

It was changed in bpo-29695.

But accepting arbitrary keyword arguments is another issue.

Object creation in Python can be customized be implementing special methods 
__new__ and __init__. They both are called sequentially with arguments passed 
to the constructor. If object is mutable, it is enough to implement __init__. 
If the object contains immutable data which should be initialized at creation 
time, it needs __new__, and __init__ is not necessary. So the list class has 
__init__, but the tuple and int classes have __new__. Usually class implements 
only one of __new__ or __init__, and inherits the other one from parent classes.

Since positional and keyword arguments are passed to both __new__ and __init__, 
they should accept same arguments. If __new__ and __init__ are inherited from 
parent class, they cannot know about arguments supported in the other method. 
Therefore object's __new__ and __init__ accept and ignore all arguments (if the 
other method is overridden).

Implementations of __new__ for most builtin classes which accept only 
positional arguments accept and ignore also arbitrary keyword arguments in 
subclasses. It makes easier to implement a subclass with non-trivial __init__. 
You just add additional keyword arguments which will be ignored in __new__. It 
is long tradition. Example:

if (type == _type && !_PyArg_NoKeywords("cycle()", kwds))
return NULL;

bpo-20186 just used this idiom for tuple and several other classes. It is a 
feature which makes subclassing these classes easier. And with converting more 
classes to Argument Clinic it is now used in more and more classes.

Now, perhaps it would be more correct to test `type->tp_init == 
cycle_type.tp_init` or `type->tp_init != PyBaseObject_Type.tp_init` instead of 
`type == _type`. It will ignore keyword arguments only if __init__ is 
overridden. If __init__ is overridden, it is now responsible for validating 
arguments.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41369] Update to libmpdec-2.5.1

2021-03-06 Thread Antoine Pitrou


Antoine Pitrou  added the comment:

Stefan wrote me that the changes which complete this update are here:
https://github.com/python/cpython/compare/master...skrah:libmpdec-2.5.1

I can turn this diff into a PR but first I want to gather feedback here.  Mark, 
Serhiy, Raymond, what do you say?

--
assignee: skrah -> 
nosy: +facundobatista, mark.dickinson, pitrou, rhettinger, serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43422] Revert _decimal C API changes

2021-03-06 Thread Antoine Pitrou


New submission from Antoine Pitrou :

Stefan Krah (who doesn't have access rights here, and is the author of the C 
_decimal module) asked me to transmit me this request:
"""
The capsule API does not meet my testing standards, since I've focused
on the upstream mpdecimal in the last couple of months.

Additionally, I'd like to refine the API, perhaps together with the
Arrow community.
"""

The relevant diff is here:
https://github.com/python/cpython/compare/master...skrah:revert_decimal_capsule_api

I can turn it into a PR but first I'd like to gather reactions here.

--
components: Extension Modules
messages: 388205
nosy: facundobatista, mark.dickinson, pitrou, rhettinger, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Revert _decimal C API changes
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43332] Make http.client._tunnel send one byte string over the network

2021-03-06 Thread Zveinn


Zveinn  added the comment:

Hey! 

First of all, thank you for not shitting all over me <3 

I have never really used python and the only reason I found this is because I 
was developing a tool that accepted a LOT of CONNECT requests for python and I 
just happened to stumble upon this little nugget.

Regarding a PR or a local path, it would have been too much work for me since 
I'm already swamped and I don't know anything about python or it's ecosystem :S

Anyways, I leave this in your hands now. 

Regards, Zveinn

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43406] Possible race condition between signal catching and signal.signal

2021-03-06 Thread Antoine Pitrou


Change by Antoine Pitrou :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43406] Possible race condition between signal catching and signal.signal

2021-03-06 Thread miss-islington


miss-islington  added the comment:


New changeset 1385f8355a036fd65aaf9c7e7ab48467ca922bcf by Miss Islington (bot) 
in branch '3.9':
[3.9] bpo-43406: Fix possible race condition where ``PyErr_CheckSignals`` tries 
to execute a non-Python signal handler (GH-24756) (GH-24761)
https://github.com/python/cpython/commit/1385f8355a036fd65aaf9c7e7ab48467ca922bcf


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43406] Possible race condition between signal catching and signal.signal

2021-03-06 Thread miss-islington


miss-islington  added the comment:


New changeset 4715be8a4384159e47fb09e5f3bd077734842655 by Antoine Pitrou in 
branch '3.8':
[3.8] bpo-43406: Fix possible race condition where ``PyErr_CheckSignals`` tries 
to execute a non-Python signal handler (GH-24756) (GH-24762)
https://github.com/python/cpython/commit/4715be8a4384159e47fb09e5f3bd077734842655


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43419] contextvars does not work properly in asyncio REPL.

2021-03-06 Thread Lanfon


Change by Lanfon :


--
keywords: +patch
pull_requests: +23541
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24773

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43421] os.device_encoding(fd) should support any console fd in Windows

2021-03-06 Thread Eryk Sun


New submission from Eryk Sun :

In Windows, os.device_encoding() is hard coded to map file descriptor 0 and 
descriptors 1 and 2 respectively to the console's input and output code page if 
isatty(fd) is true. But isatty() is true for any character device, such as 
"NUL". Also any fd might be a console, by way of dup(), dup2() -- or open() 
with the device names "CON", "CONIN$", and "CONOUT$".

The correct device encoding of a console file is needed for use with os.read() 
and os.write(). It's also necessary for io.TextIOWrapper() if 
PYTHONLEGACYWINDOWSSTDIO is set.

_Py_device_encoding() in Python/fileutils.c should use _get_osfhandle() to get 
the OS handle, and, if it's a character-device file, determine the code page to 
return, if any, depending on whether it's an input or output console file. For 
example:

PyObject *
_Py_device_encoding(int fd)
{
#if defined(MS_WINDOWS)
HANDLE handle;
DWORD temp;
UINT cp = 0;

_Py_BEGIN_SUPPRESS_IPH
handle = (HANDLE)_get_osfhandle(fd);
_Py_END_SUPPRESS_IPH
if (handle == INVALID_HANDLE_VALUE ||
GetFileType(handle) != FILE_TYPE_CHAR)
Py_RETURN_NONE;

Py_BEGIN_ALLOW_THREADS
/* GetConsoleMode requires a console handle. */
if (!GetConsoleMode(handle, )) {
/* Assume access denied implies output. */
if (GetLastError() == ERROR_ACCESS_DENIED)
cp = GetConsoleOutputCP();
} else {
if (GetNumberOfConsoleInputEvents(handle, )) {
cp = GetConsoleCP();
} else {
cp = GetConsoleOutputCP();
}
}
Py_END_ALLOW_THREADS

if (cp == CP_UTF8) {
return PyUnicode_FromString("UTF-8");
} else if (cp != 0) {
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
} else {
Py_RETURN_NONE;
}
#else
if (isatty(fd)) {
return _Py_GetLocaleEncodingObject();
} else {
Py_RETURN_NONE;
}
#endif  /* defined(MS_WINDOWS) */
}

--
components: Extension Modules, IO, Interpreter Core, Windows
messages: 388201
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: os.device_encoding(fd) should support any console fd in Windows
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43420] Optimize rational arithmetics

2021-03-06 Thread Sergey B Kirpichev


New submission from Sergey B Kirpichev :

fractions.py uses naive algorithms for doing arithmetics.

It may worth implementing less trivial versions for addtion/substraction
and multiplication (e.g. Henrici algorithm and so on), described here:
https://www.eecis.udel.edu/~saunders/courses/822/98f/collins-notes/rnarith.ps
as e.g gmplib does: https://gmplib.org/repo/gmp/file/tip/mpq/aors.c

Some projects (e.g. SymPy here: https://github.com/sympy/sympy/pull/12656) 
reinvent
the stdlib's Fraction just to add such simple improvements.  With big 
denominators (~10**6)
this really does matter, my local benchmarks suggest the order of magnitude 
difference for
summation of several such numbers.

--
components: Library (Lib)
messages: 388200
nosy: Sergey.Kirpichev
priority: normal
severity: normal
status: open
title: Optimize rational arithmetics
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43419] contextvars does not work properly in asyncio REPL.

2021-03-06 Thread Lanfon


New submission from Lanfon :

Demonstration (via python -m asyncio):

asyncio REPL 3.9.0 (default, Oct 18 2020, 00:21:26) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from contextvars import ContextVar
>>> ctx = ContextVar('ctx')
>>> ctx.set(1)
 at 0x1021bf800>
>>> ctx.get()
Traceback (most recent call last):
  File 
"/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/concurrent/futures/_base.py",
 line 440, in result
return self.__get_result()
  File 
"/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/concurrent/futures/_base.py",
 line 389, in __get_result
raise self._exception
  File "/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/asyncio/__main__.py", 
line 34, in callback
coro = func()
  File "", line 1, in 
LookupError: 
>>> exit()


It also got problem inside the functions when the context is referenced in 
global scope.

--
components: asyncio
messages: 388199
nosy: asvetlov, lanfon72, yselivanov
priority: normal
severity: normal
status: open
title: contextvars does not work properly in asyncio REPL.
type: behavior
versions: Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43418] FTPLib module crashes when server returns byte message instead of string

2021-03-06 Thread Hugo Chia


New submission from Hugo Chia :

https://github.com/cowrie/cowrie/issues/1394

https://github.com/cowrie/cowrie/pull/1396

Above are some of the links mentioning the issue with the FTPLib module. It 
happens when the FTP server returns a byte message instead of a string. Ftplib 
expects a string and does not account for receiving a byte message

--
components: Library (Lib)
messages: 388198
nosy: hugochiaxyz8
priority: normal
severity: normal
status: open
title: FTPLib module crashes when server returns byte message instead of string
type: crash
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43391] The comments have invalid license information (broken Python 2.4 URL for Python 3)

2021-03-06 Thread Marc-Andre Lemburg


Change by Marc-Andre Lemburg :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43417] ast.unparse: Simplify buffering logic

2021-03-06 Thread Batuhan Taskaya


Change by Batuhan Taskaya :


--
keywords: +patch
pull_requests: +23540
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24772

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43417] ast.unparse: Simplify buffering logic

2021-03-06 Thread Batuhan Taskaya


New submission from Batuhan Taskaya :

Currently, buffer is just an instance-level list that is used in various places 
to avoid directly writing stuff into the real source buffer, though the design 
is pretty complicated and hard to use.

There are various use cases (like omitting the empty space when unparsing 
argument-less lambdas, e.g: lambda : 2 + 2) we could've use this buffer system 
if it was offering a stackable version (like multiple levels of buffers).

What I think is we should probably do this with a proper context manager and in 
the context capture all writings into a list where we would return after the 
context is closed;

with self.buffered() as buffer:
self._write_fstring_inner(node)
return self._write_str_avoiding_backslashes("".join(buffer))

--
assignee: BTaskaya
components: Library (Lib)
messages: 388197
nosy: BTaskaya
priority: normal
severity: normal
status: open
title: ast.unparse: Simplify buffering logic
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43415] Typo

2021-03-06 Thread Eric V. Smith


Change by Eric V. Smith :


--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42128] Structural Pattern Matching (PEP 634)

2021-03-06 Thread Batuhan Taskaya


Change by Batuhan Taskaya :


--
pull_requests: +23539
pull_request: https://github.com/python/cpython/pull/24771

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43416] Add README files in Include/cpython and Include/internal

2021-03-06 Thread Serhiy Storchaka


New submission from Serhiy Storchaka :

I always hesitate in what subdirectory of Include/ to add a declaration of new 
private API. What is more "private"? It would be nice to add README files in 
these directories which describe what API should be declared here and what is 
the difference between Include/cpython and Include/internal.

--
assignee: docs@python
components: Documentation, Interpreter Core
messages: 388196
nosy: docs@python, serhiy.storchaka, vstinner
priority: normal
severity: normal
status: open
title: Add README files in Include/cpython and Include/internal
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com