Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 05f4c34

Browse files
author
Anselm Kruis
committed
Merge branch 3.8 into 3.8-slp
2 parents 7b612c2 + bdace21 commit 05f4c34

22 files changed

+100
-288
lines changed

Doc/c-api/init_config.rst

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,18 @@ PyPreConfig
194194
* Configure the LC_CTYPE locale
195195
* Set the UTF-8 mode
196196
197-
The :c:member:`struct_size` field must be explicitly initialized to
198-
``sizeof(PyPreConfig)``.
199-
200197
Function to initialize a preconfiguration:
201198
202-
.. c:function:: PyStatus PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
199+
.. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
203200
204201
Initialize the preconfiguration with :ref:`Python Configuration
205202
<init-python-config>`.
206203
207-
.. c:function:: PyStatus PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
204+
.. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
208205
209206
Initialize the preconfiguration with :ref:`Isolated Configuration
210207
<init-isolated-conf>`.
211208
212-
The caller of these functions is responsible to handle exceptions (error or
213-
exit) using :c:func:`PyStatus_Exception` and
214-
:c:func:`Py_ExitStatusException`.
215-
216209
Structure fields:
217210
218211
.. c:member:: int allocator
@@ -274,13 +267,6 @@ PyPreConfig
274267
same way the regular Python parses command line arguments: see
275268
:ref:`Command Line Arguments <using-on-cmdline>`.
276269
277-
.. c:member:: size_t struct_size
278-
279-
Size of the structure in bytes: must be initialized to
280-
``sizeof(PyPreConfig)``.
281-
282-
Field used for API and ABI compatibility.
283-
284270
.. c:member:: int use_environment
285271
286272
See :c:member:`PyConfig.use_environment`.
@@ -332,12 +318,7 @@ Example using the preinitialization to enable the UTF-8 Mode::
332318
333319
PyStatus status;
334320
PyPreConfig preconfig;
335-
preconfig.struct_size = sizeof(PyPreConfig);
336-
337-
status = PyPreConfig_InitPythonConfig(&preconfig);
338-
if (PyStatus_Exception(status)) {
339-
Py_ExitStatusException(status);
340-
}
321+
PyPreConfig_InitPythonConfig(&preconfig);
341322
342323
preconfig.utf8_mode = 1;
343324
@@ -360,9 +341,6 @@ PyConfig
360341
361342
Structure containing most parameters to configure Python.
362343
363-
The :c:member:`struct_size` field must be explicitly initialized to
364-
``sizeof(PyConfig)``.
365-
366344
Structure methods:
367345
368346
.. c:function:: PyStatus PyConfig_InitPythonConfig(PyConfig *config)
@@ -679,13 +657,6 @@ PyConfig
679657
Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
680658
:data:`sys.stderr`.
681659
682-
.. c:member:: size_t struct_size
683-
684-
Size of the structure in bytes: must be initialized to
685-
``sizeof(PyConfig)``.
686-
687-
Field used for API and ABI compatibility.
688-
689660
.. c:member:: int tracemalloc
690661
691662
If non-zero, call :func:`tracemalloc.start` at startup.
@@ -754,7 +725,6 @@ Example setting the program name::
754725
{
755726
PyStatus status;
756727
PyConfig config;
757-
config.struct_size = sizeof(PyConfig);
758728
759729
status = PyConfig_InitPythonConfig(&config);
760730
if (PyStatus_Exception(status)) {
@@ -787,7 +757,6 @@ configuration, and then override some parameters::
787757
{
788758
PyStatus status;
789759
PyConfig config;
790-
config.struct_size = sizeof(PyConfig);
791760
792761
status = PyConfig_InitPythonConfig(&config);
793762
if (PyStatus_Exception(status)) {
@@ -875,7 +844,6 @@ Example of customized Python always running in isolated mode::
875844
{
876845
PyStatus status;
877846
PyConfig config;
878-
config.struct_size = sizeof(PyConfig);
879847
880848
status = PyConfig_InitPythonConfig(&config);
881849
if (PyStatus_Exception(status)) {
@@ -1067,7 +1035,6 @@ phases::
10671035
{
10681036
PyStatus status;
10691037
PyConfig config;
1070-
config.struct_size = sizeof(PyConfig);
10711038
10721039
status = PyConfig_InitPythonConfig(&config);
10731040
if (PyStatus_Exception(status)) {

Doc/library/tarfile.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ be finalized; only the internally used file object will be closed. See the
290290

291291
*fileobj* is not closed, when :class:`TarFile` is closed.
292292

293-
*format* controls the archive format. It must be one of the constants
293+
*format* controls the archive format for writing. It must be one of the constants
294294
:const:`USTAR_FORMAT`, :const:`GNU_FORMAT` or :const:`PAX_FORMAT` that are
295-
defined at module level.
295+
defined at module level. When reading, format will be automatically detected, even
296+
if different formats are present in a single archive.
296297

297298
The *tarinfo* argument can be used to replace the default :class:`TarInfo` class
298299
with a different one.

Include/cpython/initconfig.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ PyAPI_FUNC(PyStatus) PyWideStringList_Insert(PyWideStringList *list,
4545
/* --- PyPreConfig ----------------------------------------------- */
4646

4747
typedef struct {
48-
/* Size of the structure in bytes: must be initialized to
49-
sizeof(PyPreConfig). Field used for API and ABI compatibility. */
50-
size_t struct_size;
51-
5248
int _config_init; /* _PyConfigInitEnum value */
5349

5450
/* Parse Py_PreInitializeFromBytesArgs() arguments?
@@ -124,17 +120,13 @@ typedef struct {
124120
int allocator;
125121
} PyPreConfig;
126122

127-
PyAPI_FUNC(PyStatus) PyPreConfig_InitPythonConfig(PyPreConfig *config);
128-
PyAPI_FUNC(PyStatus) PyPreConfig_InitIsolatedConfig(PyPreConfig *config);
123+
PyAPI_FUNC(void) PyPreConfig_InitPythonConfig(PyPreConfig *config);
124+
PyAPI_FUNC(void) PyPreConfig_InitIsolatedConfig(PyPreConfig *config);
129125

130126

131127
/* --- PyConfig ---------------------------------------------- */
132128

133129
typedef struct {
134-
/* Size of the structure in bytes: must be initialized to
135-
sizeof(PyConfig). Field used for API and ABI compatibility. */
136-
size_t struct_size;
137-
138130
int _config_init; /* _PyConfigInitEnum value */
139131

140132
int isolated; /* Isolated mode? see PyPreConfig.isolated */

Include/internal/pycore_initconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ extern PyStatus _PyPreCmdline_Read(_PyPreCmdline *cmdline,
120120

121121
/* --- PyPreConfig ----------------------------------------------- */
122122

123-
PyAPI_FUNC(PyStatus) _PyPreConfig_InitCompatConfig(PyPreConfig *preconfig);
124-
extern PyStatus _PyPreConfig_InitFromConfig(
123+
PyAPI_FUNC(void) _PyPreConfig_InitCompatConfig(PyPreConfig *preconfig);
124+
extern void _PyPreConfig_InitFromConfig(
125125
PyPreConfig *preconfig,
126126
const PyConfig *config);
127127
extern PyStatus _PyPreConfig_InitFromPreConfig(

Lib/test/support/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
except ImportError:
7575
resource = None
7676

77+
try:
78+
import _hashlib
79+
except ImportError:
80+
_hashlib = None
81+
7782
__all__ = [
7883
# globals
7984
"PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast",
@@ -91,8 +96,8 @@
9196
"create_empty_file", "can_symlink", "fs_is_case_insensitive",
9297
# unittest
9398
"is_resource_enabled", "requires", "requires_freebsd_version",
94-
"requires_linux_version", "requires_mac_ver", "check_syntax_error",
95-
"check_syntax_warning",
99+
"requires_linux_version", "requires_mac_ver", "requires_hashdigest",
100+
"check_syntax_error", "check_syntax_warning",
96101
"TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset",
97102
"transient_internet", "BasicTestRunner", "run_unittest", "run_doctest",
98103
"skip_unless_symlink", "requires_gzip", "requires_bz2", "requires_lzma",
@@ -652,20 +657,27 @@ def wrapper(*args, **kw):
652657
return decorator
653658

654659

655-
def requires_hashdigest(digestname):
660+
def requires_hashdigest(digestname, openssl=None):
656661
"""Decorator raising SkipTest if a hashing algorithm is not available
657662
658663
The hashing algorithm could be missing or blocked by a strict crypto
659664
policy.
660665
666+
If 'openssl' is True, then the decorator checks that OpenSSL provides
667+
the algorithm. Otherwise the check falls back to built-in
668+
implementations.
669+
661670
ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS
662671
ValueError: unsupported hash type md4
663672
"""
664673
def decorator(func):
665674
@functools.wraps(func)
666675
def wrapper(*args, **kwargs):
667676
try:
668-
hashlib.new(digestname)
677+
if openssl and _hashlib is not None:
678+
_hashlib.new(digestname)
679+
else:
680+
hashlib.new(digestname)
669681
except ValueError:
670682
raise unittest.SkipTest(
671683
f"hash digest '{digestname}' is not available."

Lib/test/test_hashlib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import array
1010
from binascii import unhexlify
11+
import functools
1112
import hashlib
1213
import importlib
1314
import itertools
@@ -18,6 +19,7 @@
1819
import warnings
1920
from test import support
2021
from test.support import _4G, bigmemtest, import_fresh_module
22+
from test.support import requires_hashdigest
2123
from http.client import HTTPException
2224

2325
# Were we compiled --with-pydebug or with #define Py_DEBUG?
@@ -119,6 +121,7 @@ def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
119121
constructors.add(_test_algorithm_via_hashlib_new)
120122

121123
_hashlib = self._conditional_import_module('_hashlib')
124+
self._hashlib = _hashlib
122125
if _hashlib:
123126
# These two algorithms should always be present when this module
124127
# is compiled. If not, something was compiled wrong.
@@ -127,7 +130,13 @@ def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
127130
for algorithm, constructors in self.constructors_to_test.items():
128131
constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
129132
if constructor:
130-
constructors.add(constructor)
133+
try:
134+
constructor()
135+
except ValueError:
136+
# default constructor blocked by crypto policy
137+
pass
138+
else:
139+
constructors.add(constructor)
131140

132141
def add_builtin_constructor(name):
133142
constructor = getattr(hashlib, "__get_builtin_constructor")(name)

Lib/test/test_hmac.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def wrapper(*args, **kwargs):
2121

2222
class TestVectorsTestCase(unittest.TestCase):
2323

24-
@requires_hashdigest('md5')
24+
@requires_hashdigest('md5', openssl=True)
2525
def test_md5_vectors(self):
2626
# Test the HMAC module against test vectors from the RFC.
2727

@@ -79,7 +79,7 @@ def md5test(key, data, digest):
7979
b"and Larger Than One Block-Size Data"),
8080
"6f630fad67cda0ee1fb1f562db3aa53e")
8181

82-
@requires_hashdigest('sha1')
82+
@requires_hashdigest('sha1', openssl=True)
8383
def test_sha_vectors(self):
8484
def shatest(key, data, digest):
8585
h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
@@ -272,19 +272,19 @@ def hmactest(key, data, hexdigests):
272272
'134676fb6de0446065c97440fa8c6a58',
273273
})
274274

275-
@requires_hashdigest('sha224')
275+
@requires_hashdigest('sha224', openssl=True)
276276
def test_sha224_rfc4231(self):
277277
self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64)
278278

279-
@requires_hashdigest('sha256')
279+
@requires_hashdigest('sha256', openssl=True)
280280
def test_sha256_rfc4231(self):
281281
self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64)
282282

283-
@requires_hashdigest('sha384')
283+
@requires_hashdigest('sha384', openssl=True)
284284
def test_sha384_rfc4231(self):
285285
self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128)
286286

287-
@requires_hashdigest('sha512')
287+
@requires_hashdigest('sha512', openssl=True)
288288
def test_sha512_rfc4231(self):
289289
self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128)
290290

Misc/NEWS.d/next/C API/2019-09-28-03-43-27.bpo-38304.RqHAwd.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug due to the interaction of weakrefs and the cyclic garbage
2+
collector. We must clear any weakrefs in garbage in order to prevent their
3+
callbacks from executing and causing a crash.

Modules/_hashopenssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ EVPnew(const EVP_MD *digest,
556556
if ((self = newEVPobject()) == NULL)
557557
return NULL;
558558

559-
if (!EVP_DigestInit(self->ctx, digest)) {
559+
if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
560560
_setException(PyExc_ValueError);
561561
Py_DECREF(self);
562562
return NULL;

Modules/gcmodule.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,21 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
678678
op = FROM_GC(gc);
679679
next = GC_NEXT(gc);
680680

681+
if (PyWeakref_Check(op)) {
682+
/* A weakref inside the unreachable set must be cleared. If we
683+
* allow its callback to execute inside delete_garbage(), it
684+
* could expose objects that have tp_clear already called on
685+
* them. Or, it could resurrect unreachable objects. One way
686+
* this can happen is if some container objects do not implement
687+
* tp_traverse. Then, wr_object can be outside the unreachable
688+
* set but can be deallocated as a result of breaking the
689+
* reference cycle. If we don't clear the weakref, the callback
690+
* will run and potentially cause a crash. See bpo-38006 for
691+
* one example.
692+
*/
693+
_PyWeakref_ClearRef((PyWeakReference *)op);
694+
}
695+
681696
if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
682697
continue;
683698

@@ -733,6 +748,8 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
733748
* is moved to wrcb_to_call in this case.
734749
*/
735750
if (gc_is_collecting(AS_GC(wr))) {
751+
/* it should already have been cleared above */
752+
assert(wr->wr_object == Py_None);
736753
continue;
737754
}
738755

Modules/main.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,15 @@ pymain_init(const _PyArgv *args)
5353
#endif
5454

5555
PyPreConfig preconfig;
56-
preconfig.struct_size = sizeof(PyPreConfig);
57-
58-
status = PyPreConfig_InitPythonConfig(&preconfig);
59-
if (_PyStatus_EXCEPTION(status)) {
60-
return status;
61-
}
56+
PyPreConfig_InitPythonConfig(&preconfig);
6257

6358
status = _Py_PreInitializeFromPyArgv(&preconfig, args);
6459
if (_PyStatus_EXCEPTION(status)) {
6560
return status;
6661
}
6762

6863
PyConfig config;
69-
config.struct_size = sizeof(PyConfig);
64+
7065
status = PyConfig_InitPythonConfig(&config);
7166
if (_PyStatus_EXCEPTION(status)) {
7267
goto done;

PC/getpathp.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,13 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
315315
'prefix' is null terminated in bounds. join() ensures
316316
'landmark' can not overflow prefix if too long. */
317317
static int
318-
gotlandmark(wchar_t *prefix, const wchar_t *landmark)
318+
gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
319319
{
320-
int ok;
321-
Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
322-
323-
join(prefix, landmark);
324-
ok = ismodule(prefix, FALSE);
325-
prefix[n] = '\0';
326-
return ok;
320+
wchar_t filename[MAXPATHLEN+1];
321+
memset(filename, 0, sizeof(filename));
322+
wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
323+
join(filename, landmark);
324+
return ismodule(filename, FALSE);
327325
}
328326

329327

0 commit comments

Comments
 (0)