Skip to content

Commit

Permalink
Merge pull request #838 from kycutler/kycutler/debugpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Jan 10, 2022
2 parents c5b1751 + 6ffb3d3 commit 74dfaec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
31 changes: 28 additions & 3 deletions ipykernel/compiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from IPython.core.compilerop import CachingCompiler
import tempfile
import os
import sys


def murmur2_x86(data, seed):
Expand Down Expand Up @@ -36,11 +37,35 @@ def murmur2_x86(data, seed):

return h

convert_to_long_pathname = lambda filename:filename

if sys.platform == 'win32':
try:
import ctypes
from ctypes.wintypes import MAX_PATH, LPCWSTR, LPWSTR, DWORD

_GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
_GetLongPathName.argtypes = [LPCWSTR, LPWSTR, DWORD]
_GetLongPathName.restype = DWORD

def _convert_to_long_pathname(filename):
buf = ctypes.create_unicode_buffer(MAX_PATH)
rv = _GetLongPathName(filename, buf, MAX_PATH)
if rv != 0 and rv <= MAX_PATH:
filename = buf.value
return filename

# test that it works so if there are any issues we fail just once here
_convert_to_long_pathname(__file__)
except:
pass
else:
convert_to_long_pathname = _convert_to_long_pathname

def get_tmp_directory():
tmp_dir = tempfile.gettempdir()
tmp_dir = convert_to_long_pathname(tempfile.gettempdir())
pid = os.getpid()
return tmp_dir + '/ipykernel_' + str(pid)
return tmp_dir + os.sep + 'ipykernel_' + str(pid)


def get_tmp_hash_seed():
Expand All @@ -52,7 +77,7 @@ def get_file_name(code):
cell_name = os.environ.get("IPYKERNEL_CELL_NAME")
if cell_name is None:
name = murmur2_x86(code, get_tmp_hash_seed())
cell_name = get_tmp_directory() + '/' + str(name) + '.py'
cell_name = get_tmp_directory() + os.sep + str(name) + '.py'
return cell_name


Expand Down
2 changes: 1 addition & 1 deletion ipykernel/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ async def debugInfo(self, message):
'isStarted': self.is_started,
'hashMethod': 'Murmur2',
'hashSeed': get_tmp_hash_seed(),
'tmpFilePrefix': get_tmp_directory() + '/',
'tmpFilePrefix': get_tmp_directory() + os.sep,
'tmpFileSuffix': '.py',
'breakpoints': breakpoint_list,
'stoppedThreads': self.stopped_threads,
Expand Down
6 changes: 6 additions & 0 deletions ipykernel/tests/test_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,9 @@ def test_rich_inspect_at_breakpoint(kernel_with_debug):
)

assert reply["body"]["data"] == {"text/plain": locals_[0]["value"]}


def test_convert_to_long_pathname():
if sys.platform == 'win32':
from ipykernel.compiler import _convert_to_long_pathname
_convert_to_long_pathname(__file__)

0 comments on commit 74dfaec

Please sign in to comment.