10000 PyPy comptability preparation · 200spc/winpython@9c8acda · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c8acda

Browse files
committed
PyPy comptability preparation
1 parent 4b8a118 commit 9c8acda

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

winpython/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
OTHER DEALINGS IN THE SOFTWARE.
2929
"""
3030

31-
__version__ = '4.1.20210417'
31+
__version__ = '4.2.20210422'
3232
__license__ = __doc__
3333
__project_url__ = 'http://winpython.github.io/'

winpython/utils.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@
2828
# Local imports
2929
from winpython.py3compat import winreg
3030

31+
def get_python_executable(path = None):
32+
"""return the python executable"""
33+
my_path = sys.executable if path == None else path # default = current one
34+
my_path = path if osp.isdir(path) else osp.dirname(path)
35+
exec_py = os.path.join(path, 'python.exe')
36+
exec_pypy = os.path.join(path, 'pypy3.exe') # PyPy !
37+
python_executable = exec_pypy if osp.isfile(exec_pypy) else exec_py
38+
return python_executable
39+
40+
def get_site_packages_path(path = None):
41+
"""return the python site-packages"""
42+
my_path = sys.executable if path == None else path # default = current one
43+
my_path = path if osp.isdir(path) else osp.dirname(path)
44+
site_py = os.path.join(path, 'Lib', 'site-packages')
45+
site_pypy = os.path.join(path, 'site-packages') # PyPy !!
46+
site_packages_path = site_pypy if osp.isfile(site_pypy) else site_py
47+
return site_packages_path
3148

3249
def onerror(function, path, excinfo):
3350
"""Error handler for `shutil.rmtree`.
@@ -264,9 +281,9 @@ def print_box(text):
264281
def is_python_distribution(path):
265282
"""Return True if path is a Python distrib 10000 ution"""
266283
# XXX: This test could be improved but it seems to be sufficient
267-
return osp.isfile(
268-
osp.join(path, 'python.exe')
269-
) and osp.isdir(osp.join(path, 'Lib', 'site-packages'))
284+
has_exec = osp.isfile(get_python_executable(path))
285+
has_site = osp.isdir(get_site_packages_path(path))
286+
return has_exec and has_site
270287

271288

272289
# =============================================================================
@@ -349,9 +366,8 @@ def get_pandoc_version(path):
349366

350367
def python_query(cmd, path):
351368
"""Execute Python command using the Python interpreter located in *path*"""
352-
return exec_shell_cmd(
353-
'python -c "%s"' % cmd, path
354-
).splitlines()[0]
369+
the_exe = get_python_executable(path)
370+
return exec_shell_cmd('%s -c "%s"' % (the_exe, cmd), path).splitlines()[0]
355371

356372

357373
def get_python_infos(path):
@@ -417,6 +433,11 @@ def patch_shebang_line(
417433
shebang_line = re.compile(
418434
b"(#!.*pythonw?\.exe)"
419435
) # Python3+
436+
if 'pypy3' in sys.executable:
437+
shebang_line = re.compile(
438+
b"(#!.*pypy3w?\.exe)"
439+
) # Pypy3+
440+
420441
target_dir = target_dir.encode('utf-8')
421442
with open(fname, 'rb') as fh:
422443
initial_content = fh.read()
@@ -461,11 +482,15 @@ def patch_shebang_line_py(
461482
return
462483
if to_movable:
463484
exec_path = '#!.\python.exe'
485+
if 'pypy3' in sys.executable: # PyPy !
486+
exec_path = '#!.\pypy3.exe'
464487
else:
465488
exec_path = '#!' + sys.executable
466489
for line in fileinput.input(fname, inplace=True):
467490
if re.match('^#\!.*python\.exe$', line) is not None:
468491
print(exec_path)
492+
elif re.match('^#\!.*pypy3\.exe$', line) is not None:# PyPy !
493+
print(exec_path)
469494
else:
470495
print(line, end='')
471496

winpython/wppm.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
Created on Fri Aug 03 14:32:26 2012
1111
"""
12-
12+
# pypy3 to patch from 'python' to 'pypy3': 379 493 497 627 692 696 743 767 785
1313
from __future__ import print_function
1414

1515
import os
@@ -376,7 +376,7 @@ def get_installed_packages(self, update=False):
376376
else:
377377
# indirect way: we interrogate something else
378378
cmdx = [
379-
osp.join(self.target, 'python.exe'),
379+
utils.get_python_executable(self.target), # PyPy !
380380
'-c',
381381
"import pip;from pip._internal.utils.misc import get_installed_distributions as pip_get_installed_distributions ;print('+!+'.join(['%s@+@%s@+@' % (i.key,i.version) for i in pip_get_installed_distributions()]))",
382382
]
@@ -490,11 +490,13 @@ def do_pip_action(
490490
'/D',
491491
self.target,
492492
r'&&',
493-
osp.join(self.target, 'python.exe'),
493+
utils.get_python_executable(self.target),
494+
# Before PyPy: osp.join(self.target, 'python.exe')
494495
]
495496
complement += ['-m', 'pip']
496497
else:
497-
executing = osp.join(self.target, 'python.exe')
498+
executing = utils.get_python_executable(self.target)
499+
# Before PyPy: osp.join(self.target, 'python.exe')
498500
complement = ['-m', 'pip']
499501
try:
500502
fname = utils.do_script(
@@ -525,6 +527,10 @@ def patch_standard_packages(
525527
origin = self.target + (
526528
r"\Lib\site-packages\pywin32_system32"
527529
)
530+
if 'pypy3' in sys.executable:
531+
origin = self.target + (
532+
r"\site-packages\pywin32_system32"
533+
)
528534
destin = self.target
529535
if osp.isdir(origin):
530536
for name in os.listdir(origin):
@@ -548,29 +554,34 @@ def patch_standard_packages(
548554
sheb_fix = " executable = get_executable()"
549555
sheb_mov1 = " executable = os.path.join(os.path.basename(get_executable()))"
550556
sheb_mov2 = " executable = os.path.join('..',os.path.basename(get_executable()))"
557+
if 'pypy3' in sys.executable:
558+
the_place=r"\site-packages\pip\_vendor\distlib\scripts.py"
559+
else:
560+
the_place=r"\Lib\site-packages\pip\_vendor\distlib\scripts.py"
561+
print(the_place)
551562
if to_movable:
552563
utils.patch_sourcefile(
553564
self.target
554-
+ r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
565+
+ the_place,
555566
sheb_fix,
556567
sheb_mov1,
557568
)
558569
utils.patch_sourcefile(
559570
self.target
560-
+ r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
571+
+ the_place,
561572
sheb_mov2,
562573
sheb_mov1,
563574
)
564575
else:
565576
utils.patch_sourcefile(
566577
self.target
567-
+ r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
578+
+ the_place,
568579
sheb_mov1,
569580
sheb_fix,
570581
)
571582
utils.patch_sourcefile(
572583
self.target
573-
+ r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
584+
+ the_place,
574585
sheb_mov2,
575586
sheb_fix,
576587
)
@@ -686,6 +697,11 @@ def handle_specific_packages(self, package):
686697
tmp_string = r'''@echo off
687698
if "%WINPYDIR%"=="" call "%~dp0..\..\scripts\env.bat"
688699
"%WINPYDIR%\python.exe" "%WINPYDIR%\Lib\site-packages\package.name\uic\pyuic.py" %1 %2 %3 %4 %5 %6 %7 %8 %9'''
700+
701+
# PyPy adaption: python.exe or pypy3.exe
702+
my_exec = osp.basename(utils.get_python_executable(self.target))
703+
tmp_string = tmp_string.replace('python.exe', my_exec)
704+
689705
self.create_file(
690706
package,
691707
'pyuic%s.bat' % package.name[-1],
@@ -730,9 +746,10 @@ def uninstall(self, package):
730746
if not package.name == 'pip':
731747
# trick to get true target (if not current)
732748
this_executable_path = self.target
749+
this_exec = utils.get_python_executable(self.target) # PyPy !
733750
subprocess.call(
734751
[
735-
this_executable_path + r'\python.exe',
752+
this_exec,
736753
'-m',
737754
'pip',
738755
'uninstall',
@@ -755,9 +772,7 @@ def install_bdist_direct(
755772
try:
756773
fname = utils.direct_pip_install(
757774
package.fname,
758-
python_exe=osp.join(
759-
self.target, 'python.exe'
760-
),
775+
python_exe=utils.get_python_executable(self.target), # PyPy !
761776
architecture=self.architecture,
762777
verbose=self.verbose,
763778
install_options=install_options,
@@ -773,9 +788,7 @@ def install_script(self, script, install_options=None):
773788
try:
774789
fname = utils.do_script(
775790
script,
776-
python_exe=osp.join(
777-
self.target, 'python.exe'
778-
),
791+
python_exe=utils.get_python_executable(self.target), # PyPy3 !
779792
architecture=self.architecture,
780793
verbose=self.verbose,
781794
install_options=install_options,

0 commit comments

Comments
 (0)
0