Skip to content

Commit f6db0b6

Browse files
committedNov 5, 2023
'call_python_array()' now adds the newlines.
Not the caller.
1 parent 7bcc592 commit f6db0b6

File tree

1 file changed

+91
-99
lines changed

1 file changed

+91
-99
lines changed
 

‎src/envtool_py.c

+91-99
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ typedef struct python_info {
192192
HANDLE dll_hnd;
193193

194194
/** The index of this Python in the `py_programs` smartlist.
195-
* I.e. in the cache-file "python_in_pathX = ...", `py_index == X`.
195+
* I.e. in the cache-file "python_in_pathX = ...", `py_index == X`.
196196
*/
197197
int py_index;
198198

@@ -241,7 +241,6 @@ static int longest_py_version = 0; /** set in py_init() */
241241
static int global_indent = 0;
242242
static int warn_on_py_fail = 1;
243243

244-
245244
/**
246245
* Variables used in the generic callback helper function popen_append_out().
247246
*/
@@ -253,10 +252,6 @@ static bool popen_py_crash = false;
253252
static int get_python_version (const char *exe_name);
254253
static bool py_add_module (struct python_info *pi, const struct python_module *m);
255254

256-
#if defined(__GNUC__) && (__GNUC__ >= 7)
257-
#pragma GCC diagnostic ignored "-Wformat-truncation"
258-
#endif
259-
260255
/**
261256
* The list of Pythons from the `"%PATH"` and from the
262257
* `"HKLM\Software\Python\PythonCore\xx\InstallPath"` locations.
@@ -271,22 +266,22 @@ static smartlist_t *py_programs;
271266
* \param is_opt `== true` if the function is optional.
272267
* \param f the name of the function (without any `"`).
273268
*/
274-
#define LOAD_FUNC(pi, is_opt, f) do { \
275-
f = GETPROCADDRESS (func_##f, pi->dll_hnd, #f); \
276-
if (!f && !is_opt) { \
277-
WARN ("Failed to find \"%s()\" in %s.\n", \
278-
#f, pi->dll_name); \
279-
goto failed; \
280-
} \
281-
TRACE (3, "Function %s(): %*s 0x%p.\n", \
282-
#f, 23-(int)strlen(#f), "", f); \
269+
#define LOAD_FUNC(pi, is_opt, f) do { \
270+
f = GETPROCADDRESS (func_##f, pi->dll_hnd, #f); \
271+
if (!f && !is_opt) { \
272+
WARN ("Failed to find \"%s()\" in %s.\n", \
273+
#f, pi->dll_name); \
274+
goto failed; \
275+
} \
276+
TRACE (3, "Function %s(): %*s 0x%p.\n", \
277+
#f, 23-(int)strlen(#f), "", f); \
283278
} while (0)
284279

285280
/**
286281
* \def LOAD_INT_PTR(pi, ptr)
287282
* A `GetProcAddress()` helper for setting an int-pointer.
288283
* \param pi the `struct python_info` to work on.
289-
* \param ptr the name of the variable (without any `"`).
284+
* \param ptr the int-pointer (and name) to import.
290285
*/
291286
#define LOAD_INT_PTR(pi, ptr) do { \
292287
ptr = GETPROCADDRESS (int*, pi->dll_hnd, #ptr); \
@@ -1127,7 +1122,7 @@ static char *call_python_func (struct python_info *pi, const char *prog, unsigne
11271122

11281123
/**
11291124
* As above, but with the Python code given as a NULL-terminated array.
1130-
* Each element should be EOL-terminated (`"\n"`).
1125+
* Each element should NOT have newlines. These are done here.
11311126
*/
11321127
static char *call_python_array (struct python_info *pi, const char **array, unsigned line)
11331128
{
@@ -1136,13 +1131,17 @@ static char *call_python_array (struct python_info *pi, const char **array, unsi
11361131
size_t i = 0, size = 1;
11371132

11381133
for (a = array[0]; a; a = array[++i])
1139-
size += strlen (a);
1134+
{
1135+
size += strlen (a) + 1;
1136+
ASSERT (strchr(a, '\n') == NULL);
1137+
}
11401138

11411139
prog = p = alloca (size);
11421140
for (a = array[0]; a; a = array[++i])
11431141
{
11441142
strcpy (p, a);
11451143
p += strlen (a);
1144+
*p++ = '\n';
11461145
}
11471146
return call_python_func (pi, prog, line);
11481147
}
@@ -1201,13 +1200,6 @@ static char *fcheck_open_mem (struct python_module *m, const char *file, size_t
12011200

12021201
snprintf (fqfn, sizeof(fqfn), "%s%c%s", pkg_dir, DIR_SEP, file);
12031202
FREE (pkg_dir);
1204-
1205-
/* Hack to avoid this stupid 'gcc 12+ warning':
1206-
* envtool_py.c:1142:13: warning: dangling pointer 'file' to 'fqfn' may be used [-Wdangling-pointer=]
1207-
* 1142 | return fopen_mem (file, f_size);
1208-
* | ^~~~~~~~~~~~~~~~~~~~~~~~
1209-
*
1210-
*/
12111203
file = strdupa (fqfn);
12121204
}
12131205
else
@@ -1255,7 +1247,7 @@ static void py_get_meta_details (struct python_module *m)
12551247
if (p && q > p)
12561248
{
12571249
email = m->author_email;
1258-
m->author_email = str_ndup (p+1, q-p);
1250+
m->author_email = str_ndup (p+1, q - p);
12591251
FREE (email);
12601252
}
12611253
if (m->author && p)
@@ -1317,10 +1309,10 @@ static void py_get_meta_details (struct python_module *m)
13171309
*/
13181310
static bool test_python_funcs (struct python_info *pi, bool reinit)
13191311
{
1320-
static const char *prog[] = { "import sys\n",
1321-
"print (sys.version_info)\n",
1322-
"for i in range(3):\n",
1323-
" print (\" Hello world\")\n",
1312+
static const char *prog[] = { "import sys",
1313+
"print (sys.version_info)",
1314+
"for i in range(3):",
1315+
" print (\" Hello world\")",
13241316
NULL
13251317
};
13261318
const char *name = py_variant_name (pi->variant);
@@ -1570,7 +1562,7 @@ static int py_print_modinfo (const char *spec, bool get_details)
15701562
}
15711563
}
15721564

1573-
if (match_all /* && !get_details */)
1565+
if (match_all && !get_details)
15741566
C_printf ("~6Found %d modules~0 (%d are ZIP/EGG files).\n", found, zips_found);
15751567
return (found);
15761568
}
@@ -1621,73 +1613,73 @@ static int py_print_modinfo (const char *spec, bool get_details)
16211613
* modules and packages at runtime.
16221614
*/
16231615
static const char *py_list_modules2[] = {
1624-
"from __future__ import print_function\n",
1625-
"import os, sys, pip, imp\n",
1626-
"\n",
1627-
"types = { imp.PY_SOURCE: 'source file', # = 1\n",
1628-
" imp.PY_COMPILED: 'object file',\n",
1629-
" imp.C_EXTENSION: 'shared library', # = 3\n",
1630-
" imp.PKG_DIRECTORY: 'package directory',\n",
1631-
" imp.C_BUILTIN: 'built-in module', # = 6\n",
1632-
" imp.PY_FROZEN: 'frozen module'\n",
1633-
" }\n",
1634-
"mod_paths = {}\n",
1635-
"mod_builtins = {}\n",
1636-
"prev_pathname = ''\n",
1637-
"prev_modtype = None\n",
1638-
"\n",
1639-
"def get_module_path (mod, mod_type, mod_path):\n",
1640-
" next_scope = mod.index ('.') + 1\n",
1641-
" last_scope = mod.rindex ('.') + 1\n",
1642-
"\n",
1643-
" fname = mod_path + '\\\\' + mod[next_scope:].replace('.','\\\\\') + '.py'\n",
1644-
" if os.path.exists(fname):\n",
1645-
" return fname\n",
1646-
"\n",
1647-
" init = mod_path + '\\\\' + mod[last_scope:] + '\\\\__init__.py'\n",
1648-
" if os.path.exists(init):\n",
1649-
" return init\n",
1650-
"\n",
1651-
" try:\n",
1652-
" if mod_builtins[mod[last_scope:]] == 1:\n",
1653-
" return '__builtin__ ' + mod[last_scope:]\n",
1654-
" except KeyError:\n",
1655-
" pass\n",
1656-
"\n",
1657-
" try:\n",
1658-
" return mod_paths [mod[last_scope:]] + ' !'\n",
1659-
" except KeyError:\n",
1660-
" pass\n",
1661-
"\n",
1662-
" try:\n",
1663-
" _x, pathname, _y = imp.find_module (mod, mod_path)\n",
1664-
" return pathname\n",
1665-
" except ImportError:\n",
1666-
" return '<unknown>'\n",
1667-
" except RuntimeError:\n",
1668-
" mod_builtins [mod] = 1\n",
1669-
" return '__builtin__ ' + mod[last_scope:]\n",
1670-
"\n",
1671-
"for s in sorted(sys.modules):\n",
1672-
" print ('%s' % s, end='')\n",
1673-
" if '.' in s:\n",
1674-
" print (',%s' % get_module_path(s,prev_modtype,prev_pathname))\n",
1675-
" continue\n",
1676-
"\n",
1677-
" try:\n",
1678-
" _, pathname, descr = imp.find_module (s)\n",
1679-
" t = types [descr[2]]\n",
1680-
" prev_modtype = t\n",
1681-
" print (',%s,' % t, end='')\n",
1682-
" if pathname and '\\\\' in pathname:\n",
1683-
" print ('%s' % pathname)\n",
1684-
" prev_pathname = pathname\n",
1685-
" prev_modtype = t\n",
1686-
" mod_paths [s] = pathname\n",
1687-
" else:\n",
1688-
" mod_builtins [s] = 1\n",
1689-
" except ImportError:\n",
1690-
" print (',<unknown>')\n",
1616+
"from __future__ import print_function",
1617+
"import os, sys, pip, imp",
1618+
"",
1619+
"types = { imp.PY_SOURCE: 'source file', # = 1",
1620+
" imp.PY_COMPILED: 'object file',",
1621+
" imp.C_EXTENSION: 'shared library', # = 3",
1622+
" imp.PKG_DIRECTORY: 'package directory',",
1623+
" imp.C_BUILTIN: 'built-in module', # = 6",
1624+
" imp.PY_FROZEN: 'frozen module'",
1625+
" }",
1626+
"mod_paths = {}",
1627+
"mod_builtins = {}",
1628+
"prev_pathname = ''",
1629+
"prev_modtype = None",
1630+
"",
1631+
"def get_module_path (mod, mod_type, mod_path):",
1632+
" next_scope = mod.index ('.') + 1",
1633+
" last_scope = mod.rindex ('.') + 1",
1634+
"",
1635+
" fname = mod_path + '\\\\' + mod[next_scope:].replace('.','\\\\\') + '.py'",
1636+
" if os.path.exists(fname):",
1637+
" return fname",
1638+
"",
1639+
" init = mod_path + '\\\\' + mod[last_scope:] + '\\\\__init__.py'",
1640+
" if os.path.exists(init):",
1641+
" return init",
1642+
"",
1643+
" try:",
1644+
" if mod_builtins[mod[last_scope:]] == 1:",
1645+
" return '__builtin__ ' + mod[last_scope:]",
1646+
" except KeyError:",
1647+
" pass",
1648+
"",
1649+
" try:",
1650+
" return mod_paths [mod[last_scope:]] + ' !'",
1651+
" except KeyError:",
1652+
" pass",
1653+
"",
1654+
" try:",
1655+
" _x, pathname, _y = imp.find_module (mod, mod_path)",
1656+
" return pathname",
1657+
" except ImportError:",
1658+
" return '<unknown>'",
1659+
" except RuntimeError:",
1660+
" mod_builtins [mod] = 1",
1661+
" return '__builtin__ ' + mod[last_scope:]",
1662+
"",
1663+
"for s in sorted(sys.modules):",
1664+
" print ('%s' % s, end='')",
1665+
" if '.' in s:",
1666+
" print (',%s' % get_module_path(s,prev_modtype,prev_pathname))",
1667+
" continue",
1668+
"",
1669+
" try:",
1670+
" _, pathname, descr = imp.find_module (s)",
1671+
" t = types [descr[2]]",
1672+
" prev_modtype = t",
1673+
" print (',%s,' % t, end='')",
1674+
" if pathname and '\\\\' in pathname:",
1675+
" print ('%s' % pathname)",
1676+
" prev_pathname = pathname",
1677+
" prev_modtype = t",
1678+
" mod_paths [s] = pathname",
1679+
" else:",
1680+
" mod_builtins [s] = 1",
1681+
" except ImportError:",
1682+
" print (',<unknown>')",
16911683
NULL
16921684
};
16931685

@@ -3291,7 +3283,7 @@ static char *py_exec_internal (struct python_info *pi, const char **py_argv, boo
32913283
const char *prog0, *fmt = NULL;
32923284
char *str, *prog;
32933285
size_t size;
3294-
arg_vector av = { 0, NULL, NULL }; /* Fill in to shutup warnings from gcc */
3286+
arg_vector av = { 0, NULL, NULL };
32953287

32963288
C_printf ("Executing ~6%s~0 using ~6%s~0: ", py_argv[0], pi->dll_name);
32973289

0 commit comments

Comments
 (0)
Please sign in to comment.