Repository: pypa/virtualenv

Issue:
M1 Mac codesigning issue:  execv() arg 2 must contain only strings
**Issue**

I've seen that there has been a lot of work put in already to make virtualenv run under arm64 + MacOS 12, so thanks for that :) Unfortunately, I'm still running into an issue when trying to create a Python 2 virtualenv on my M1 MacBook. The error is an "TypeError:  execv() arg 2 must contain only strings" and seems to happen when trying to copy the python executable for codesigning. I hope someone here can help me resolve that. I might also be able to help out with a PR, but would appreciate any guidance you can already give 😁 

**Environment**

- OS: 12.0.1 (M1 MacBook)
-  I'm using MacOS' system Python
- ``pip list`` of the host python where ``virtualenv`` is installed:

```console
Package                                Version
-------------------------------------- -----------
altgraph                               0.10.2
asn1crypto                             0.24.0
bdist-mpkg                             0.5.0
bonjour-py                             0.3
cffi                                   1.14.3
configparser                           4.0.2
contextlib2                            0.6.0.post1
cryptography                           2.9.2
distlib                                0.3.4
enum34                                 1.1.6
filelock                               3.2.1
future                                 0.17.1
importlib-metadata                     2.1.2
importlib-resources                    3.3.1
ipaddress                              1.0.22
macholib                               1.5.1
matplotlib                             1.3.1
modulegraph                            0.10.4
numpy                                  1.8.0rc1
pathlib2                               2.3.6
pip                                    20.3.4
platformdirs                           2.0.2
py2app                                 0.7.3
pycparser                              2.19
pyobjc-core                            2.5.1
pyobjc-framework-Accounts              2.5.1
pyobjc-framework-AddressBook           2.5.1
pyobjc-framework-AppleScriptKit        2.5.1
pyobjc-framework-AppleScriptObjC       2.5.1
pyobjc-framework-Automator             2.5.1
pyobjc-framework-CFNetwork             2.5.1
pyobjc-framework-Cocoa                 2.5.1
pyobjc-framework-Collaboration         2.5.1
pyobjc-framework-CoreData              2.5.1
pyobjc-framework-CoreLocation          2.5.1
pyobjc-framework-CoreText              2.5.1
pyobjc-framework-DictionaryServices    2.5.1
pyobjc-framework-EventKit              2.5.1
pyobjc-framework-ExceptionHandling     2.5.1
pyobjc-framework-FSEvents              2.5.1
pyobjc-framework-InputMethodKit        2.5.1
pyobjc-framework-InstallerPlugins      2.5.1
pyobjc-framework-InstantMessage        2.5.1
pyobjc-framework-LatentSemanticMapping 2.5.1
pyobjc-framework-LaunchServices        2.5.1
pyobjc-framework-Message               2.5.1
pyobjc-framework-OpenDirectory         2.5.1
pyobjc-framework-PreferencePanes       2.5.1
pyobjc-framework-PubSub                2.5.1
pyobjc-framework-QTKit                 2.5.1
pyobjc-framework-Quartz                2.5.1
pyobjc-framework-ScreenSaver           2.5.1
pyobjc-framework-ScriptingBridge       2.5.1
pyobjc-framework-SearchKit             2.5.1
pyobjc-framework-ServiceManagement     2.5.1
pyobjc-framework-Social                2.5.1
pyobjc-framework-SyncServices          2.5.1
pyobjc-framework-SystemConfiguration   2.5.1
pyobjc-framework-WebKit                2.5.1
pyOpenSSL                              19.0.0
pyparsing                              2.0.1
python-dateutil                        1.5
pytz                                   2013.7
scandir                                1.10.0
scipy                                  0.13.0b1
setuptools                             41.0.1
singledispatch                         3.7.0
six                                    1.12.0
typing                                 3.10.0.0
virtualenv                             20.13.0
wheel                                  0.33.1
xattr                                  0.6.4
zipp                                   1.2.0
```

**Output of the virtual environment creation**

```console
53 setup logging to NOTSET [DEBUG report:39]
65 find interpreter for spec PythonSpec(path=/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python) [INFO builtin:62]
65 proposed PythonInfo({'base_exec_prefix': None, 'system_stdlib': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 'has_venv': False, 'prefix': u'/System/Library/Frameworks/Python.framework/Versions/2.7', 'stdout_encoding': u'UTF-8', 'executable': '/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python', 'implementation': u'CPython', 'exec_prefix': u'/System/Library/Frameworks/Python.framework/Versions/2.7', 'platform': u'darwin', 'version': u'2.7.18 (default, Oct  2 2021, 04:20:38) \n[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [+internal-os, ptrauth-isa=deploymen', 'sysconfig_paths': {u'platstdlib': u'{platbase}/lib/python{py_version_short}', u'platlib': u'{platbase}/lib/python{py_version_short}/site-packages', u'purelib': u'{base}/lib/python{py_version_short}/site-packages', u'stdlib': u'{base}/lib/python{py_version_short}', u'scripts': u'{base}/bin', u'include': u'{base}/include/python{py_version_short}', u'data': u'{base}'}, 'sysconfig_scheme': None, 'base_prefix': None, 'system_stdlib_platform': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 'file_system_encoding': u'utf-8', 'version_info': VersionInfo(major=2, minor=7, micro=18, releaselevel=u'final', serial=0), 'sysconfig_vars': {u'base': u'/System/Library/Frameworks/Python.framework/Versions/2.7', u'platbase': u'/System/Library/Frameworks/Python.framework/Versions/2.7', u'PYTHONFRAMEWORK': u'Python', u'py_version_short': u'2.7'}, 'path': [u'/usr/local/bin', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', u'/Users/peter/Library/Python/2.7/lib/python/site-packages', u'/Library/Python/2.7/site-packages', u'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', u'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', u'/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg'], 'max_size': 9223372036854775807, 'sysconfig': {u'makefile_filename': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/Makefile'}, 'real_prefix': None, 'distutils_install': {u'purelib': u'lib/python2.7/site-packages', u'headers': u'include/python2.7/UNKNOWN', u'platlib': u'lib/python2.7/site-packages', u'data': u'', u'scripts': u'bin'}, 'architecture': 64, 'original_executable': u'/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python', 'os': u'posix', 'system_executable': u'/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python'}) [INFO builtin:69]
65 accepted PythonInfo({'base_exec_prefix': None, 'system_stdlib': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 'has_venv': False, 'prefix': u'/System/Library/Frameworks/Python.framework/Versions/2.7', 'stdout_encoding': u'UTF-8', 'executable': '/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python', 'implementation': u'CPython', 'exec_prefix': u'/System/Library/Frameworks/Python.framework/Versions/2.7', 'platform': u'darwin', 'version': u'2.7.18 (default, Oct  2 2021, 04:20:38) \n[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [+internal-os, ptrauth-isa=deploymen', 'sysconfig_paths': {u'platstdlib': u'{platbase}/lib/python{py_version_short}', u'platlib': u'{platbase}/lib/python{py_version_short}/site-packages', u'purelib': u'{base}/lib/python{py_version_short}/site-packages', u'stdlib': u'{base}/lib/python{py_version_short}', u'scripts': u'{base}/bin', u'include': u'{base}/include/python{py_version_short}', u'data': u'{base}'}, 'sysconfig_scheme': None, 'base_prefix': None, 'system_stdlib_platform': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 'file_system_encoding': u'utf-8', 'version_info': VersionInfo(major=2, minor=7, micro=18, releaselevel=u'final', serial=0), 'sysconfig_vars': {u'base': u'/System/Library/Frameworks/Python.framework/Versions/2.7', u'platbase': u'/System/Library/Frameworks/Python.framework/Versions/2.7', u'PYTHONFRAMEWORK': u'Python', u'py_version_short': u'2.7'}, 'path': [u'/usr/local/bin', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', u'/Users/peter/Library/Python/2.7/lib/python/site-packages', u'/Library/Python/2.7/site-packages', u'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', u'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', u'/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg'], 'max_size': 9223372036854775807, 'sysconfig': {u'makefile_filename': u'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/Makefile'}, 'real_prefix': None, 'distutils_install': {u'purelib': u'lib/python2.7/site-packages', u'headers': u'include/python2.7/UNKNOWN', u'platlib': u'lib/python2.7/site-packages', u'data': u'', u'scripts': u'bin'}, 'architecture': 64, 'original_executable': u'/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python', 'os': u'posix', 'system_executable': u'/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python'}) [DEBUG builtin:71]
70 filesystem is not case-sensitive [DEBUG info:29]
108 create virtual environment via CPython2macOsArmFramework(dest=/Users/peter/path/venv, clear=False, no_vcs_ignore=False, global=False) [INFO session:52]
108 write /Users/peter/path/venv/pyvenv.cfg [DEBUG pyenv_cfg:34]
108 	home = /System/Library/Frameworks/Python.framework/Versions/2.7 [DEBUG pyenv_cfg:38]
108 	implementation = CPython [DEBUG pyenv_cfg:38]
108 	version_info = 2.7.18.final.0 [DEBUG pyenv_cfg:38]
108 	virtualenv = 20.13.0 [DEBUG pyenv_cfg:38]
108 	include-system-site-packages = false [DEBUG pyenv_cfg:38]
108 	base-prefix = /System/Library/Frameworks/Python.framework/Versions/2.7 [DEBUG pyenv_cfg:38]
108 	base-exec-prefix = /System/Library/Frameworks/Python.framework/Versions/2.7 [DEBUG pyenv_cfg:38]
108 	base-executable = /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python [DEBUG pyenv_cfg:38]
108 remove file /Users/peter/path/venv/bin/python [DEBUG _sync:37]
108 copy /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python to /Users/peter/path/venv/bin/python [DEBUG _sync:51]
109 remove file /Users/peter/path/venv/lib/python2.7/os.py [DEBUG _sync:37]
109 symlink /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py to /Users/peter/path/venv/lib/python2.7/os.py [DEBUG _sync:43]
109 remove file /Users/peter/path/venv/lib/python2.7/os.pyc [DEBUG _sync:37]
109 symlink /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc to /Users/peter/path/venv/lib/python2.7/os.pyc [DEBUG _sync:43]
109 remove file /Users/peter/path/venv/include/python2.7 [DEBUG _sync:37]
110 symlink directory /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 to /Users/peter/path/venv/include/python2.7 [DEBUG _sync:43]
110 remove file /Users/peter/path/venv/lib/python2.7/config/Makefile [DEBUG _sync:37]
110 symlink /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/Makefile to /Users/peter/path/venv/lib/python2.7/config/Makefile [DEBUG _sync:43]
110 remove file /Users/peter/path/venv/.Python [DEBUG _sync:37]
110 symlink /System/Library/Frameworks/Python.framework/Versions/2.7/Python to /Users/peter/path/venv/.Python [DEBUG _sync:43]
110 remove file /Users/peter/path/venv/lib/python2.7/lib-dynload [DEBUG _sync:37]
110 symlink directory /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload to /Users/peter/path/venv/lib/python2.7/lib-dynload [DEBUG _sync:43]
110 create virtualenv import hook file /Users/peter/path/venv/lib/python2.7/site-packages/_virtualenv.pth [DEBUG api:95]
110 create /Users/peter/path/venv/lib/python2.7/site-packages/_virtualenv.py [DEBUG api:98]
111 change Mach-O for /Users/peter/path/venv/bin/python from /System/Library/Frameworks/Python.framework/Versions/2.7/Python to @executable_path/../.Python [DEBUG mac_os:201]
111 Changing signature of copied python exe /Users/peter/path/venv/bin/python [DEBUG mac_os:132]
115 Could not change MacOS code signing on copied python exe at /Users/peter/path/venv/bin/python [CRITICAL mac_os:144]
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in <module>
    load_entry_point('virtualenv==20.13.0', 'console_scripts', 'virtualenv')()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/__main__.py", line 65, in run_with_catch
    run(args, options, env)
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/__main__.py", line 18, in run
    session = cli_run(args, options, env)
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/run/__init__.py", line 32, in cli_run
    of_session.run()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/run/session.py", line 46, in run
    self._create()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/run/session.py", line 53, in _create
    self.creator.run()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/create/creator.py", line 171, in run
    self.create()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py", line 122, in create
    self.fix_signature()
  File "/Library/Python/2.7/site-packages/virtualenv-20.13.0-py2.7.egg/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py", line 137, in fix_signature
    subprocess.check_call(["cp", exe, bak_dir])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 185, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
TypeError: execv() arg 2 must contain only strings
```



Discussion:
Did you find a solution for this issue?

• Install PYTHON2.7
Download this pkg
https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg
Python is installing pythonw check the symlink, for me it was installed here: /Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw

Get PIP :                   **curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py**
Install PIP                 **python get-pip.py**
Install virtualenv:     **pip install virtualenv**

Create virtualenv with the new python:
 **virtualenv -p /Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw testenv**

Resolving patch:
diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
--- a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
+++ b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
@@ -7,7 +7,7 @@
 from abc import ABCMeta, abstractmethod
 from textwrap import dedent
 
-from six import add_metaclass
+from six import add_metaclass, text_type
 
 from virtualenv.create.via_global_ref.builtin.ref import ExePathRefToDest, PathRefToDest, RefMust
 from virtualenv.info import IS_MAC_ARM64
@@ -134,10 +134,11 @@ def fix_signature(self):
             # Reset the signing on Darwin since the exe has been modified.
             # Note codesign fails on the original exe, it needs to be copied and moved back.
             bak_dir.mkdir(parents=True, exist_ok=True)
-            subprocess.check_call(["cp", exe, bak_dir])
-            subprocess.check_call(["mv", bak_dir / exe.name, exe])
+            subprocess.check_call(["cp", text_type(exe), text_type(bak_dir)])
+            subprocess.check_call(["mv", text_type(bak_dir / exe.name), text_type(exe)])
             bak_dir.rmdir()
-            cmd = ["codesign", "-s", "-", "--preserve-metadata=identifier,entitlements,flags,runtime", "-f", exe]
+            metadata = "--preserve-metadata=identifier,entitlements,flags,runtime"
+            cmd = ["codesign", "-s", "-", metadata, "-f", text_type(exe)]
             logging.debug("Changing Signature: %s", cmd)
             subprocess.check_call(cmd)
         except Exception:
@@ -198,7 +199,7 @@ def fix_mach_o(exe, current, new, max_size):
     unneeded bits of information, however Mac OS X 10.5 and earlier cannot read this new Link Edit table format.
     """
     try:
-        logging.debug(u"change Mach-O for %s from %s to %s", ensure_text(exe), current, ensure_text(new))
+        logging.debug("change Mach-O for %s from %s to %s", ensure_text(exe), current, ensure_text(new))
         _builtin_change_mach_o(max_size)(exe, current, new)
     except Exception as e:
         logging.warning("Could not call _builtin_change_mac_o: %s. " "Trying to call install_name_tool instead.", e)


Test patch: