10000 Initial (not yet working) code for maybe one day monkey-patching out … · r3m0t/python-future@9615329 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9615329

Browse files
committed
Initial (not yet working) code for maybe one day monkey-patching out the bugs in the Py2 stdlib
1 parent 38a2cea commit 9615329

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

future/standard_library/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,46 @@ def suspend_hooks():
346346
enable_hooks()
347347

348348

349+
def os_path_join(a, *p):
350+
"""
351+
Replacement os.path.join from Python 3.3 (posixpath.py) which doesn't
352+
add a byte-string to a unicode string as Python 2.7's does.
353+
354+
Join two or more pathname components, inserting '/' as needed.
355+
If any component is an absolute path, all previous path components
356+
will be discarded. An empty last part will result in a path that
357+
ends with a separator."""
358+
sep = _get_sep(a)
359+
path = a
360+
try:
361+
for b in p:
362+
if b.startswith(sep):
363+
path = b
364+
elif not path or path.endswith(sep):
365+
path += b
366+
else:
367+
path += sep + b
368+
except TypeError:
369+
valid_types = all(isinstance(s, (str, bytes, bytearray))
370+
for s in (a, ) + p)
371+
if valid_types:
372+
# Must have a mixture of text and binary data
373+
raise TypeError("Can't mix strings and bytes in path "
374+
"components.") from None
375+
raise
376+
return path
377+
378+
379+
def monkey_patch_stdlib():
380+
"""
381+
Patches out some bugs, like the dodgy os.path.join in Python 2.x,
382+
which adds the byte-string '/' to unicode paths.
383+
"""
384+
if not module in sys.modules:
385+
__import__(module)
386+
modname, module = module, sys.modules[module]
387+
388+
349389
if not utils.PY3:
350390
enable_hooks()
391+
monkey_patch_stdlib()

0 commit comments

Comments
 (0)
0