mercurial/exewrapper.c
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 08 Dec 2022 21:45:47 -0500
branchstable
changeset 49778 48e38b179106
parent 48304 2ec5fbe26659
permissions -rw-r--r--
demandimport: fix a crash in LazyFinder.__delattr__ I was tinkering with `with hgdemandimport.deactivated()` wrapped around loading the keyring module, and got spew that seemed to be confirmed by PyCharm. But I can't believe we haven't seen this before (and phabricator uses the same pattern): ** Unknown exception encountered with possibly-broken third-party extension "mercurial_keyring" 1.4.3 (keyring 23.11.0, backend unknown) ** which supports versions unknown of Mercurial. ** Please disable "mercurial_keyring" and try your action again. ** If that fixes the bug please report it to https://foss.heptapod.net/mercurial/mercurial_keyring/issues ** Python 3.9.15 (main, Oct 13 2022, 04:28:25) [GCC 7.5.0] ** Mercurial Distributed SCM (version 6.3.1) ** Extensions loaded: absorb, attorc 20220315, blackbox, eol, extdiff, fastannotate, lfs, mercurial_keyring 1.4.3 (keyring 23.11.0, backend unknown), phabblocker 20220315, phabricator 20220315, purge, rebase, schemes, share, show, strip, uncommit Traceback (most recent call last): File "/usr/local/bin/hg", line 59, in <module> dispatch.run() File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 143, in run status = dispatch(req) File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 232, in dispatch status = _rundispatch(req) File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 276, in _rundispatch ret = _runcatch(req) or 0 File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 451, in _runcatch return _callcatch(ui, _runcatchfunc) File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 461, in _callcatch return scmutil.callcatch(ui, func) File "/usr/local/lib/python3.9/site-packages/mercurial/scmutil.py", line 153, in callcatch return func() File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 441, in _runcatchfunc return _dispatch(req) File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 1265, in _dispatch return runcommand( File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 899, in runcommand ret = _runcommand(ui, options, cmd, d) File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 1277, in _runcommand return cmdfunc() File "/usr/local/lib/python3.9/site-packages/mercurial/dispatch.py", line 1263, in <lambda> d = lambda: util.checksignature(func)(ui, *args, **strcmdopt) File "/usr/local/lib/python3.9/site-packages/mercurial/util.py", line 1880, in check return func(*args, **kwargs) File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 962, in cmd_keyring_check user, pwd, source, final_url = handler.get_credentials( File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 497, in get_credentials keyring_pwd = password_store.get_http_password(keyring_url, actual_user) File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 287, in get_http_password return self._read_password_from_keyring( File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 335, in _read_password_from_keyring keyring = import_keyring() >> `with hgdemandimport.deactivated()` inserted here File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 120, in import_keyring return _import_keyring() File "/root/mercurial_keyring/mercurial_keyring/mercurial_keyring.py", line 133, in _import_keyring mod, was_imported_now = meu.direct_import_ext( File "/usr/lib/python3.9/site-packages/mercurial_extension_utils.py", line 1381, in direct_import_ext __import__(module_name) File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 46, in exec_module self.loader.exec_module(module) File "/usr/lib/python3.9/site-packages/keyring/__init__.py", line 1, in <module> from .core import ( File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 46, in exec_module self.loader.exec_module(module) File "/usr/lib/python3.9/site-packages/keyring/core.py", line 11, in <module> from . import backend, credentials File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 46, in exec_module self.loader.exec_module(module) File "/usr/lib/python3.9/site-packages/keyring/backend.py", line 13, in <module> from .py312compat import metadata File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 46, in exec_module self.loader.exec_module(module) File "/usr/lib/python3.9/site-packages/keyring/py312compat.py", line 10, in <module> import importlib_metadata as metadata # type: ignore File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 46, in exec_module self.loader.exec_module(module) File "/usr/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 715, in <module> class MetadataPathFinder(NullFinder, DistributionFinder): File "/usr/lib/python3.9/site-packages/importlib_metadata/_compat.py", line 24, in install disable_stdlib_finder() File "/usr/lib/python3.9/site-packages/importlib_metadata/_compat.py", line 43, in disable_stdlib_finder del finder.find_distributions File "/usr/local/lib/python3.9/site-packages/hgdemandimport/demandimportpy3.py", line 88, in __delattr__ return delattr(object.__getattribute__(self, "_finder")) TypeError: delattr expected 2 arguments, got 1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     1
/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     2
 exewrapper.c - wrapper for calling a python script on Windows
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     3
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     4
 Copyright 2012 Adrian Buehlmann <adrian@cadifra.com> and others
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     5
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     6
 This software may be used and distributed according to the terms of the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     7
 GNU General Public License version 2 or any later version.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     8
*/
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     9
40976
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40416
diff changeset
    10
#include <Python.h>
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    11
#include <stdio.h>
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    12
#include <tchar.h>
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    13
#include <windows.h>
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    14
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    15
#include "hgpythonlib.h"
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    16
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    17
#ifdef __GNUC__
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    18
int strcat_s(char *d, size_t n, const char *s)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    19
{
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    20
	return !strncat(d, s, n);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    21
}
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    22
int strcpy_s(char *d, size_t n, const char *s)
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    23
{
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    24
	return !strncpy(d, s, n);
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    25
}
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    26
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    27
#define _tcscpy_s strcpy_s
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    28
#define _tcscat_s strcat_s
40416
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    29
#define _countof(array) (sizeof(array) / sizeof(array[0]))
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    30
#endif
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    31
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    32
static TCHAR pyscript[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    33
static TCHAR pyhome[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    34
static TCHAR pydllfile[MAX_PATH + 10];
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    35
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    36
int _tmain(int argc, TCHAR *argv[])
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    37
{
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    38
	TCHAR *p;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    39
	int ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    40
	int i;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    41
	int n;
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    42
	TCHAR **pyargv;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    43
	WIN32_FIND_DATA fdata;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    44
	HANDLE hfind;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    45
	const char *err;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    46
	HMODULE pydll;
40416
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    47
	void(__cdecl * Py_SetPythonHome)(TCHAR * home);
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    48
	int(__cdecl * Py_Main)(int argc, TCHAR *argv[]);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    49
40976
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40416
diff changeset
    50
#if PY_MAJOR_VERSION >= 3
47315
825d5a5907b4 exewrapper: avoid directly linking against python3X.dll
Matt Harbison <matt_harbison@yahoo.com>
parents: 40976
diff changeset
    51
	_wputenv(L"PYTHONLEGACYWINDOWSSTDIO=1");
40976
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40416
diff changeset
    52
#endif
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40416
diff changeset
    53
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    54
	if (GetModuleFileName(NULL, pyscript, _countof(pyscript)) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    55
		err = "GetModuleFileName failed";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    56
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    57
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    58
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    59
	p = _tcsrchr(pyscript, '.');
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    60
	if (p == NULL) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    61
		err = "malformed module filename";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    62
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    63
	}
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    64
	*p = 0; /* cut trailing ".exe" */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    65
	_tcscpy_s(pyhome, _countof(pyhome), pyscript);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    66
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    67
	hfind = FindFirstFile(pyscript, &fdata);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    68
	if (hfind != INVALID_HANDLE_VALUE) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    69
		/* pyscript exists, close handle */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    70
		FindClose(hfind);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    71
	} else {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    72
		/* file pyscript isn't there, take <pyscript>exe.py */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    73
		_tcscat_s(pyscript, _countof(pyscript), _T("exe.py"));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    74
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    75
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    76
	pydll = NULL;
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    77
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    78
	p = _tcsrchr(pyhome, _T('\\'));
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    79
	if (p == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    80
		err = "can't find backslash in module filename";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    81
		goto bail;
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    82
	}
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    83
	*p = 0; /* cut at directory */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    84
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    85
	/* check for private Python of HackableMercurial */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    86
	_tcscat_s(pyhome, _countof(pyhome), _T("\\hg-python"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    87
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    88
	hfind = FindFirstFile(pyhome, &fdata);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    89
	if (hfind != INVALID_HANDLE_VALUE) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    90
		/* Path .\hg-python exists. We are probably in HackableMercurial
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    91
		scenario, so let's load python dll from this dir. */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    92
		FindClose(hfind);
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    93
		_tcscpy_s(pydllfile, _countof(pydllfile), pyhome);
40416
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    94
		_tcscat_s(pydllfile, _countof(pydllfile),
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    95
		          _T("\\") _T(HGPYTHONLIB) _T(".dll"));
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    96
		pydll = LoadLibrary(pydllfile);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    97
		if (pydll == NULL) {
40416
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    98
			err = "failed to load private Python DLL " HGPYTHONLIB
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
    99
			      ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   100
			goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   101
		}
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31443
diff changeset
   102
		Py_SetPythonHome =
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31443
diff changeset
   103
		    (void *)GetProcAddress(pydll, "Py_SetPythonHome");
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   104
		if (Py_SetPythonHome == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   105
			err = "failed to get Py_SetPythonHome";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   106
			goto bail;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   107
		}
31443
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   108
		Py_SetPythonHome(pyhome);
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   109
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   110
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   111
	if (pydll == NULL) {
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   112
		pydll = LoadLibrary(_T(HGPYTHONLIB) _T(".dll"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   113
		if (pydll == NULL) {
26662
d215def59c3b exewrapper: report name of failed DLL in error message
Adrian Buehlmann <adrian@cadifra.com>
parents: 17732
diff changeset
   114
			err = "failed to load Python DLL " HGPYTHONLIB ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   115
			goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   116
		}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   117
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   118
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31443
diff changeset
   119
	Py_Main = (void *)GetProcAddress(pydll, "Py_Main");
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   120
	if (Py_Main == NULL) {
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   121
		err = "failed to get Py_Main";
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   122
		goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   123
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   124
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   125
	/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   126
	Only add the pyscript to the args, if it's not already there. It may
17063
3fbc6e3abdbd exewrapper: use generic term script
Adrian Buehlmann <adrian@cadifra.com>
parents: 17058
diff changeset
   127
	already be there, if the script spawned a child process of itself, in
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   128
	the same way as it got called, that is, with the pyscript already in
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   129
	place. So we optionally accept the pyscript as the first argument
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   130
	(argv[1]), letting our exe taking the role of the python interpreter.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   131
	*/
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   132
	if (argc >= 2 && _tcscmp(argv[1], pyscript) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   133
		/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   134
		pyscript is already in the args, so there is no need to copy
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   135
		the args and we can directly call the python interpreter with
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   136
		the original args.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   137
		*/
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   138
		return Py_Main(argc, argv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   139
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   140
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   141
	/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   142
	Start assembling the args for the Python interpreter call. We put the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   143
	name of our exe (argv[0]) in the position where the python.exe
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   144
	canonically is, and insert the pyscript next.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   145
	*/
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   146
	pyargv = malloc((argc + 5) * sizeof(TCHAR *));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   147
	if (pyargv == NULL) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   148
		err = "not enough memory";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   149
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   150
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   151
	n = 0;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   152
	pyargv[n++] = argv[0];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   153
	pyargv[n++] = pyscript;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   154
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   155
	/* copy remaining args from the command line */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   156
	for (i = 1; i < argc; i++)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   157
		pyargv[n++] = argv[i];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   158
	/* argv[argc] is guaranteed to be NULL, so we forward that guarantee */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   159
	pyargv[n] = NULL;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   160
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   161
	ret = Py_Main(n, pyargv); /* The Python interpreter call */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   162
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   163
	free(pyargv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   164
	return ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   165
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   166
bail:
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   167
	fprintf(stderr, "abort: %s\n", err);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   168
	return 255;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   169
}