Skip to content

Commit df2e964

Browse files
authored
Merge branch 'main' into exceptions
2 parents c861af5 + ae9b38f commit df2e964

File tree

7 files changed

+180
-95
lines changed

7 files changed

+180
-95
lines changed

Doc/using/windows.rst

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,22 +743,47 @@ command::
743743

744744
py -2
745745

746-
You should find the latest version of Python 3.x starts.
747-
748746
If you see the following error, you do not have the launcher installed::
749747

750748
'py' is not recognized as an internal or external command,
751749
operable program or batch file.
752750

753-
Per-user installations of Python do not add the launcher to :envvar:`PATH`
754-
unless the option was selected on installation.
755-
756751
The command::
757752

758753
py --list
759754

760755
displays the currently installed version(s) of Python.
761756

757+
The ``-x.y`` argument is the short form of the ``-V:Company/Tag`` argument,
758+
which allows selecting a specific Python runtime, including those that may have
759+
come from somewhere other than python.org. Any runtime registered by following
760+
:pep:`514` will be discoverable. The ``--list`` command lists all available
761+
runtimes using the ``-V:`` format.
762+
763+
When using the ``-V:`` argument, specifying the Company will limit selection to
764+
runtimes from that provider, while specifying only the Tag will select from all
765+
providers. Note that omitting the slash implies a tag::
766+
767+
# Select any '3.*' tagged runtime
768+
py -V:3
769+
770+
# Select any 'PythonCore' released runtime
771+
py -V:PythonCore/
772+
773+
# Select PythonCore's latest Python 3 runtime
774+
py -V:PythonCore/3
775+
776+
The short form of the argument (``-3``) only ever selects from core Python
777+
releases, and not other distributions. However, the longer form (``-V:3``) will
778+
select from any.
779+
780+
The Company is matched on the full string, case-insenitive. The Tag is matched
781+
oneither the full string, or a prefix, provided the next character is a dot or a
782+
hyphen. This allows ``-V:3.1`` to match ``3.1-32``, but not ``3.10``. Tags are
783+
sorted using numerical ordering (``3.10`` is newer than ``3.1``), but are
784+
compared using text (``-V:3.01`` does not match ``3.1``).
785+
786+
762787
Virtual environments
763788
^^^^^^^^^^^^^^^^^^^^
764789

Lib/test/test_launcher.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,17 @@
5656
None: sys.prefix,
5757
}
5858
},
59-
}
59+
},
60+
"PythonTestSuite1": {
61+
"DisplayName": "Python Test Suite Single",
62+
"3.100": {
63+
"DisplayName": "Single Interpreter",
64+
"InstallPath": {
65+
None: sys.prefix,
66+
"ExecutablePath": sys.executable,
67+
}
68+
}
69+
},
6070
}
6171

6272

@@ -206,6 +216,7 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=Non
206216
**{k.upper(): v for k, v in os.environ.items() if k.upper() not in ignore},
207217
"PYLAUNCHER_DEBUG": "1",
208218
"PYLAUNCHER_DRYRUN": "1",
219+
"PYLAUNCHER_LIMIT_TO_COMPANY": "",
209220
**{k.upper(): v for k, v in (env or {}).items()},
210221
}
211222
if not argv:
@@ -388,23 +399,33 @@ def test_filter_to_tag(self):
388399
self.assertEqual(company, data["env.company"])
389400
self.assertEqual("3.100", data["env.tag"])
390401

391-
data = self.run_py([f"-V:3.100-3"])
402+
data = self.run_py([f"-V:3.100-32"])
392403
self.assertEqual("X.Y-32.exe", data["LaunchCommand"])
393404
self.assertEqual(company, data["env.company"])
394405
self.assertEqual("3.100-32", data["env.tag"])
395406

396-
data = self.run_py([f"-V:3.100-a"])
407+
data = self.run_py([f"-V:3.100-arm64"])
397408
self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test", data["LaunchCommand"])
398409
self.assertEqual(company, data["env.company"])
399410
self.assertEqual("3.100-arm64", data["env.tag"])
400411

401412
def test_filter_to_company_and_tag(self):
402413
company = "PythonTestSuite"
403-
data = self.run_py([f"-V:{company}/3.1"])
414+
data = self.run_py([f"-V:{company}/3.1"], expect_returncode=103)
415+
416+
data = self.run_py([f"-V:{company}/3.100"])
404417
self.assertEqual("X.Y.exe", data["LaunchCommand"])
405418
self.assertEqual(company, data["env.company"])
406419
self.assertEqual("3.100", data["env.tag"])
407420

421+
def test_filter_with_single_install(self):
422+
company = "PythonTestSuite1"
423+
data = self.run_py(
424+
[f"-V:Nonexistent"],
425+
env={"PYLAUNCHER_LIMIT_TO_COMPANY": company},
426+
expect_returncode=103,
427+
)
428+
408429
def test_search_major_3(self):
409430
try:
410431
data = self.run_py(["-3"], allow_fail=True)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``py.exe`` launcher now correctly filters when only a single runtime is
2+
installed. It also correctly handles prefix matches on tags so that ``-3.1``
3+
does not match ``3.11``, but would still match ``3.1-32``.

PC/launcher2.c

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,30 @@ _startsWithArgument(const wchar_t *x, int xLen, const wchar_t *y, int yLen)
295295
}
296296

297297

298+
// Unlike regular startsWith, this function requires that the following
299+
// character is either NULL (that is, the entire string matches) or is one of
300+
// the characters in 'separators'.
301+
bool
302+
_startsWithSeparated(const wchar_t *x, int xLen, const wchar_t *y, int yLen, const wchar_t *separators)
303+
{
304+
if (!x || !y) {
305+
return false;
306+
}
307+
yLen = yLen < 0 ? (int)wcsnlen_s(y, MAXLEN) : yLen;
308+
xLen = xLen < 0 ? (int)wcsnlen_s(x, MAXLEN) : xLen;
309+
if (xLen < yLen) {
310+
return false;
311+
}
312+
if (xLen == yLen) {
313+
return 0 == _compare(x, xLen, y, yLen);
314+
}
315+
return separators &&
316+
0 == _compare(x, yLen, y, yLen) &&
317+
wcschr(separators, x[yLen]) != NULL;
318+
}
319+
320+
321+
298322
/******************************************************************************\
299323
*** HELP TEXT ***
300324
\******************************************************************************/
@@ -409,6 +433,9 @@ typedef struct {
409433
bool listPaths;
410434
// if true, display help message before contiuning
411435
bool help;
436+
// if set, limits search to registry keys with the specified Company
437+
// This is intended for debugging and testing only
438+
const wchar_t *limitToCompany;
412439
// dynamically allocated buffers to free later
413440
struct _SearchInfoBuffer *_buffer;
414441
} SearchInfo;
@@ -489,6 +516,7 @@ dumpSearchInfo(SearchInfo *search)
489516
DEBUG_BOOL(list);
490517
DEBUG_BOOL(listPaths);
491518
DEBUG_BOOL(help);
519+
DEBUG(limitToCompany);
492520
#undef DEBUG_BOOL
493521
#undef DEBUG_2
494522
#undef DEBUG
@@ -1606,6 +1634,10 @@ registrySearch(const SearchInfo *search, EnvironmentInfo **result, HKEY root, in
16061634
}
16071635
break;
16081636
}
1637+
if (search->limitToCompany && 0 != _compare(search->limitToCompany, -1, buffer, cchBuffer)) {
1638+
debug(L"# Skipping %s due to PYLAUNCHER_LIMIT_TO_COMPANY\n", buffer);
1639+
continue;
1640+
}
16091641
HKEY subkey;
16101642
if (ERROR_SUCCESS == RegOpenKeyExW(root, buffer, 0, KEY_READ, &subkey)) {
16111643
exitCode = _registrySearchTags(search, result, subkey, sortKey, buffer, fallbackArch);
@@ -1884,6 +1916,11 @@ collectEnvironments(const SearchInfo *search, EnvironmentInfo **result)
18841916
}
18851917
}
18861918

1919+
if (search->limitToCompany) {
1920+
debug(L"# Skipping APPX search due to PYLAUNCHER_LIMIT_TO_COMPANY\n");
1921+
return 0;
1922+
}
1923+
18871924
for (struct AppxSearchInfo *info = APPX_SEARCH; info->familyName; ++info) {
18881925
exitCode = appxSearch(search, result, info->familyName, info->tag, info->sortKey);
18891926
if (exitCode && exitCode != RC_NO_PYTHON) {
@@ -2053,12 +2090,15 @@ _companyMatches(const SearchInfo *search, const EnvironmentInfo *env)
20532090

20542091

20552092
bool
2056-
_tagMatches(const SearchInfo *search, const EnvironmentInfo *env)
2093+
_tagMatches(const SearchInfo *search, const EnvironmentInfo *env, int searchTagLength)
20572094
{
2058-
if (!search->tag || !search->tagLength) {
2095+
if (searchTagLength < 0) {
2096+
searchTagLength = search->tagLength;
2097+
}
2098+
if (!search->tag || !searchTagLength) {
20592099
return true;
20602100
}
2061-
return _startsWith(env->tag, -1, search->tag, search->tagLength);
2101+
return _startsWithSeparated(env->tag, -1, search->tag, searchTagLength, L".-");
20622102
}
20632103

20642104

@@ -2095,7 +2135,7 @@ _selectEnvironment(const SearchInfo *search, EnvironmentInfo *env, EnvironmentIn
20952135
}
20962136

20972137
if (!search->oldStyleTag) {
2098-
if (_companyMatches(search, env) && _tagMatches(search, env)) {
2138+
if (_companyMatches(search, env) && _tagMatches(search, env, -1)) {
20992139
// Because of how our sort tree is set up, we will walk up the
21002140
// "prev" side and implicitly select the "best" best. By
21012141
// returning straight after a match, we skip the entire "next"
@@ -2120,7 +2160,7 @@ _selectEnvironment(const SearchInfo *search, EnvironmentInfo *env, EnvironmentIn
21202160
}
21212161
}
21222162

2123-
if (_startsWith(env->tag, -1, search->tag, tagLength)) {
2163+
if (_tagMatches(search, env, tagLength)) {
21242164
if (exclude32Bit && _is32Bit(env)) {
21252165
debug(L"# Excluding %s/%s because it looks like 32bit\n", env->company, env->tag);
21262166
} else if (only32Bit && !_is32Bit(env)) {
@@ -2147,10 +2187,6 @@ selectEnvironment(const SearchInfo *search, EnvironmentInfo *root, EnvironmentIn
21472187
*best = NULL;
21482188
return RC_NO_PYTHON_AT_ALL;
21492189
}
2150-
if (!root->next && !root->prev) {
2151-
*best = root;
2152-
return 0;
2153-
}
21542190

21552191
EnvironmentInfo *result = NULL;
21562192
int exitCode = _selectEnvironment(search, root, &result);
@@ -2560,6 +2596,17 @@ process(int argc, wchar_t ** argv)
25602596
debug(L"argv0: %s\nversion: %S\n", argv[0], PY_VERSION);
25612597
}
25622598

2599+
DWORD len = GetEnvironmentVariableW(L"PYLAUNCHER_LIMIT_TO_COMPANY", NULL, 0);
2600+
if (len > 1) {
2601+
wchar_t *limitToCompany = allocSearchInfoBuffer(&search, len);
2602+
search.limitToCompany = limitToCompany;
2603+
if (0 == GetEnvironmentVariableW(L"PYLAUNCHER_LIMIT_TO_COMPANY", limitToCompany, len)) {
2604+
exitCode = RC_INTERNAL_ERROR;
2605+
winerror(0, L"Failed to read PYLAUNCHER_LIMIT_TO_COMPANY variable");
2606+
goto abort;
2607+
}
2608+
}
2609+
25632610
search.originalCmdLine = GetCommandLineW();
25642611

25652612
exitCode = performSearch(&search, &envs);

Python/bytecodes.c

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,13 @@ dummy_func(
10621062
}
10631063
}
10641064

1065-
// error: LOAD_GLOBAL has irregular stack effect
1066-
inst(LOAD_GLOBAL) {
1065+
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
1066+
LOAD_GLOBAL,
1067+
LOAD_GLOBAL_MODULE,
1068+
LOAD_GLOBAL_BUILTIN,
1069+
};
1070+
1071+
inst(LOAD_GLOBAL, (unused/1, unused/1, unused/2, unused/1 -- null if (oparg & 1), v)) {
10671072
#if ENABLE_SPECIALIZATION
10681073
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
10691074
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
@@ -1076,10 +1081,7 @@ dummy_func(
10761081
STAT_INC(LOAD_GLOBAL, deferred);
10771082
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
10781083
#endif /* ENABLE_SPECIALIZATION */
1079-
int push_null = oparg & 1;
1080-
PEEK(0) = NULL;
10811084
PyObject *name = GETITEM(names, oparg>>1);
1082-
PyObject *v;
10831085
if (PyDict_CheckExact(GLOBALS())
10841086
&& PyDict_CheckExact(BUILTINS()))
10851087
{
@@ -1093,7 +1095,7 @@ dummy_func(
10931095
format_exc_check_arg(tstate, PyExc_NameError,
10941096
NAME_ERROR_MSG, name);
10951097
}
1096-
goto error;
1098+
ERROR_IF(true, error);
10971099
}
10981100
Py_INCREF(v);
10991101
}
@@ -1103,9 +1105,7 @@ dummy_func(
11031105
/* namespace 1: globals */
11041106
v = PyObject_GetItem(GLOBALS(), name);
11051107
if (v == NULL) {
1106-
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1107-
goto error;
1108-
}
1108+
ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error);
11091109
_PyErr_Clear(tstate);
11101110

11111111
/* namespace 2: builtins */
@@ -1116,58 +1116,42 @@ dummy_func(
11161116
tstate, PyExc_NameError,
11171117
NAME_ERROR_MSG, name);
11181118
}
1119-
goto error;
1119+
ERROR_IF(true, error);
11201120
}
11211121
}
11221122
}
1123-
/* Skip over inline cache */
1124-
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
1125-
STACK_GROW(push_null);
1126-
PUSH(v);
1123+
null = NULL;
11271124
}
11281125

1129-
// error: LOAD_GLOBAL has irregular stack effect
1130-
inst(LOAD_GLOBAL_MODULE) {
1126+
inst(LOAD_GLOBAL_MODULE, (unused/1, index/1, version/2, unused/1 -- null if (oparg & 1), res)) {
11311127
assert(cframe.use_tracing == 0);
11321128
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
11331129
PyDictObject *dict = (PyDictObject *)GLOBALS();
1134-
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
1135-
uint32_t version = read_u32(cache->module_keys_version);
11361130
DEOPT_IF(dict->ma_keys->dk_version != version, LOAD_GLOBAL);
11371131
assert(DK_IS_UNICODE(dict->ma_keys));
11381132
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(dict->ma_keys);
1139-
PyObject *res = entries[cache->index].me_value;
1133+
res = entries[index].me_value;
11401134
DEOPT_IF(res == NULL, LOAD_GLOBAL);
1141-
int push_null = oparg & 1;
1142-
PEEK(0) = NULL;
1143-
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
1135+
Py_INCREF(res);
11441136
STAT_INC(LOAD_GLOBAL, hit);
1145-
STACK_GROW(push_null+1);
1146-
SET_TOP(Py_NewRef(res));
1137+
null = NULL;
11471138
}
11481139

1149-
// error: LOAD_GLOBAL has irregular stack effect
1150-
inst(LOAD_GLOBAL_BUILTIN) {
1140+
inst(LOAD_GLOBAL_BUILTIN, (unused/1, index/1, mod_version/2, bltn_version/1 -- null if (oparg & 1), res)) {
11511141
assert(cframe.use_tracing == 0);
11521142
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
11531143
DEOPT_IF(!PyDict_CheckExact(BUILTINS()), LOAD_GLOBAL);
11541144
PyDictObject *mdict = (PyDictObject *)GLOBALS();
11551145
PyDictObject *bdict = (PyDictObject *)BUILTINS();
1156-
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
1157-
uint32_t mod_version = read_u32(cache->module_keys_version);
1158-
uint16_t bltn_version = cache->builtin_keys_version;
11591146
DEOPT_IF(mdict->ma_keys->dk_version != mod_version, LOAD_GLOBAL);
11601147
DEOPT_IF(bdict->ma_keys->dk_version != bltn_version, LOAD_GLOBAL);
11611148
assert(DK_IS_UNICODE(bdict->ma_keys));
11621149
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(bdict->ma_keys);
1163-
PyObject *res = entries[cache->index].me_value;
1150+
res = entries[index].me_value;
11641151
DEOPT_IF(res == NULL, LOAD_GLOBAL);
1165-
int push_null = oparg & 1;
1166-
PEEK(0) = NULL;
1167-
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
1152+
Py_INCREF(res);
11681153
STAT_INC(LOAD_GLOBAL, hit);
1169-
STACK_GROW(push_null+1);
1170-
SET_TOP(Py_NewRef(res));
1154+
null = NULL;
11711155
}
11721156

11731157
inst(DELETE_FAST, (--)) {
@@ -3206,9 +3190,6 @@ family(call, INLINE_CACHE_ENTRIES_CALL) = {
32063190
family(for_iter, INLINE_CACHE_ENTRIES_FOR_ITER) = {
32073191
FOR_ITER, FOR_ITER_LIST,
32083192
FOR_ITER_RANGE };
3209-
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
3210-
LOAD_GLOBAL, LOAD_GLOBAL_BUILTIN,
3211-
LOAD_GLOBAL_MODULE };
32123193
family(store_fast) = { STORE_FAST, STORE_FAST__LOAD_FAST, STORE_FAST__STORE_FAST };
32133194
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
32143195
UNPACK_SEQUENCE, UNPACK_SEQUENCE_LIST,

0 commit comments

Comments
 (0)