Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper handling of bytes/str in split and join throws TypeError with subprocess.call() #187

Closed
augustjd opened this issue May 22, 2017 · 4 comments

Comments

@augustjd
Copy link

augustjd commented May 22, 2017

I'm using pyfakefs with some test cases that involve spinning up subprocesses, but unfortunately, the subprocess library doesn't appear to be supported yet, because of path resolution for the executables.

This may be harder than it looks, but on a cursory glance it seems that the only hangup is that os.path functions like split and join support both bytes and str to be passed as arguments, and return the corresponding type. The subprocess library internally calls os functions with bytes arguments, but this causes a TypeError because os.path.split() and the like in pyfakefs only support strings.

Here's a minimal example:

import subprocess
from pyfakefs import fake_filesystem_unittest

class TestSubprocess(fake_filesystem_unittest.TestCase):
    def setUp(self):
        self.setUpPyfakefs()
        self.copyRealFile("/bin/nc", "/bin/nc")

    def test_run_nc(self):
        subprocess.call("/bin/nc")  # throws TypeError

Running this throws:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/subprocess.py", line 1245, in _execute_child
    if os.path.dirname(executable):
  File "./env/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py", line 2416, in dirname
    return self.split(path)[0]
  File "./env/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py", line 2314, in split
    return self.filesystem.SplitPath(path)
  File "./env/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py", line 970, in SplitPath
    path_components = path.split(self.path_separator)
TypeError: a bytes-like object is required, not 'str'

subprocess would be a great additional library to add to the stable of standard libs that work with pyfakefs.

@mrbean-bremen
Copy link
Member

You are right - there may be quite some places where this is not supported correctly, as the initial implementation has been done in Python 2, and the Python 3 support is not complete.
I will have a look at this, at least at the subprocess call.

@mrbean-bremen
Copy link
Member

Unfortunately, I cannot reproduce this concrete problem on my Windows machine, as this happens in the posix-specific part of subprocess, but I will look at it tomorrow anyway, if I find the time.
Thanks for reporting this!

@mrbean-bremen
Copy link
Member

mrbean-bremen commented May 23, 2017

Ok, just looked into this - I did not get it right yesterday (was a little tired). Unfortunately, you seem to be out of luck here. There are indeed some str/byte issues that we may fix (I will have a look at these later), but the actual excecution code calls OS functionality that cannot be changed to use the fake file system, as far as I can see. Sorry for that...
Edit: My own approach would be to mock subprocess to be able to check the calling arguments and not try to run an executable in the test at all.

@augustjd
Copy link
Author

Bummer. Thanks for looking into this.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue May 24, 2017
- added FakeOsModule.getcwdb() for Python 3
- see pytest-dev#187
mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue May 25, 2017
- added FakeOsModule.getcwdb() for Python 3
- see pytest-dev#187
mrbean-bremen added a commit that referenced this issue May 25, 2017
- added FakeOsModule.getcwdb() for Python 3
- see #187
@pytest-dev pytest-dev locked and limited conversation to collaborators Jul 2, 2023
@mrbean-bremen mrbean-bremen converted this issue into discussion #856 Jul 2, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants