Skip to content
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

Make python bindings article partially work on win #157

Merged
merged 16 commits into from
Nov 8, 2020
Merged

Make python bindings article partially work on win #157

merged 16 commits into from
Nov 8, 2020

Conversation

Znunu
Copy link
Contributor

@Znunu Znunu commented Oct 18, 2020

This PR fixes some of the problems that I brought up in issue 151

@jima80525
Copy link
Contributor

@Znunu
Hey! Nice work. I'm pulling this down presently and testing it out.
It looks like the CI build failed. I'm not sure if you've got a circleCI account (you can use your github acct as the login), but it's pretty easy to set up. Let me know if you have issues. (those are all formatting issues, btw)

More significantly, the CFFI build is failing on (at least) python3.7. CFFI really doesn't like #ifdef directives. Looking at it, this makes some sense as it can't know what the value of that is until compilation time. :(

Does the CFFI portion work on Windows? We might need to think some more about the best way to present this.

@Znunu
Copy link
Contributor Author

Znunu commented Oct 20, 2020

CFFI didn't work for me either. tbh I didn't intend to make that portion portable, but I didn't realize I broke it, so I went ahead and tried anyway. Like you mentioned, it didn't like the directives, so I added some code that filters them out.

@Znunu
Copy link
Contributor Author

Znunu commented Oct 29, 2020

Hello?

@jima80525
Copy link
Contributor

Hey! @Znunu - this got away from me. I'll take a look this weekend.

Thanks for the ping!

Copy link
Contributor

@jima80525 jima80525 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Znunu
This is getting close! I left a couple of comments and some fixes to get linux back and running.

THANK YOU!!!!

and, again, thanks for the ding on this last week. Been getting behind on things. :)

print_banner("Building C Library")
cmd = "gcc -c -Wall -Werror -fpic cmult.c -I /usr/include/python3.7"
invoke.run(cmd)
invoke.run("gcc -shared -o cmult.so cmult.o")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you really want this to be -o libcmult.so

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh - I see the problem now. I"ll suggest a change in ctypes_test.py to work around this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now reviewing my comments this looks strange - these two were the first comments I made getting things to work. Then I went back and did the full review. :)

c_lib = ctypes.CDLL(libname)
lib_ext = ".dll" if sys.platform.startswith("win") else ".so"
libname = pathlib.Path() / f"cmult{lib_ext}"
c_lib = ctypes.CDLL(libname.resolve().__str__())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux and mac users are really gong to expect libcmult.so. I'd recommend doing this instead of line 9:

if sys.platform.startswith("win"):
    libname = "cmult.dll"
else:
    libname = "libcmult.so"

and then just using libname instead of the fstring on line 10.



@invoke.task(build_cmult)
@invoke.task()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the build on windows taking significant time? If not, I'd prefer to leave the dependency here. It makes the individual targets a bit more fool-proof.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't. On windows, the problem is that it'll only build if we supply the argument pointing to visual studio.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh. I get it. That's fine. I missed that point.

@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# IDE might complain with "no module found" here, even when it exists
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because it doesn't exist when you start out. :) CFFI creates the module when you run the build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but my IDE, PyCharm, didn't want to acknowledge it, even after making it. I actually lost a lot of time figuring out what the problem is, since I didn't even bother to try and run it, lol

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's odd. I wonder what pycharm is doing, but not enough to actually go find out, you know. :)

overview article.

If anything is causing trouble get in touch at
Znunu @ github or VimVim @ fcZBB2v @ discord"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you taking ownership for your work, but I'd propose rephrasing this to:
"Special thanks to Znunu @ github for working through the Windows compatibility issues."

(which, I know, would sound if you said it, but it will sound like it's coming from me when we merge it :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not so important for me to be credited. Just wanted to add a quick and easy way for someone to get in touch. Someone might encounter a bug or have a problem, and thus open the file. Maybe I'm doing this the wrong way. I can remove it again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a question in with Dan to see what he wants. I'll let you know when he gets back to me (I've set a reminder that if I don't hear from him by Tuesday night, I'll ding him again.)

else:
c.run("rm -rf {}".format(pattern))
if on_win:
c.run("rmdir /s /q Release >nul 2>&1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something but could we replace those delete/rm commands with something Python-native that works across platforms?

E.g. https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Can't believe I missed this. Thanks, @dbader

if on_win:
invoke.run("python cffi_test.py")
else:
invoke.run("python3 cffi_test.py", pty=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be

invoke.run("python cffi_test.py", pty=not on_win)

straight up python will work on the other systems. I was just being pedantic. :)

@Znunu
Copy link
Contributor Author

Znunu commented Nov 1, 2020

Uhhhh.... I hope I'm doing this review thing correctly
Anyway, I fixed some things you suggested, and added comments at the rest!

@Znunu Znunu changed the title Python bindings article now partially works on win Make python bindings article partially work on win Nov 1, 2020
@jima80525
Copy link
Contributor

You're doing great on the review! No worries on that front - any shortcomings are from my slow response time. :)

@jima80525
Copy link
Contributor

jima80525 commented Nov 1, 2020

Doh! One more thing I just found - it looks like you checked in cython_example.pyx which is a generated file. Can you please do a git rm on it? Otherwise you get odd messages when you do invoke clean that you've modified the code tree. :)

Never mind that comment. Turns out I checked cython-example.pyx and it needs to be there. Looks like your clean task mods went a little too far!

"*.dll",
"*.exp",
"*.lib",
"*.pyx",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! This is a problem. I missed this one at first. The *.pyx file is actually hand-build and need to NOT be removed.

This causes the invoke build-cython test-cython sequence to fail.

@jima80525
Copy link
Contributor

This is super close! I think fixing the clean target and then getting feedback from Dan on the other comment and we should be done.

THANK YOU again for this work! I really appreciate it and I hope you've learned some! Sounds like this is your first time through a PR process? If so, you're doing great! (actually, you're doing great even if it's NOT your first time, but that's be pedantic.)

@Znunu
Copy link
Contributor Author

Znunu commented Nov 2, 2020

No problem, I'm happy to contribute and appreciate your cooperation :)
Well, technically this isn't my first PR. Was working on something with a friend. For hacktoberfest we switched from just committing, to merging each others PRs ^_^"

@jima80525
Copy link
Contributor

This looks good!

@jima80525
Copy link
Contributor

Hey - I heard back from Dan - He wants us to pull the reference to you in the tasks.py and add a shout out to you in the README.md. There ISN'T a readme in this directory (shame on me), so I'll propose this:

  • you remove the reference in tasks.py
  • I'll get the PR merged
  • I'll add a README.md file for this and call you out. Do you want me to use your full name, or just znunun at github?

Thank you again for the hard work on this! Dan was impressed that we have a PR!

@jima80525
Copy link
Contributor

@Znunu - I went ahead and wrote a README.md for the directory. It might be easier (in terms of git merging etc) if you just add it.

Here's the text:

# Real Python - Python Bindings Sample Code Repo

This is the repo to accompany the [Python Bindings](https://realpython.com/python-bindings-overview/) article.

To be able to run the code, you must first install the requirements:

```console
$ python -m pip install -r requirements.txt

This should be done inside a virtual environment.

Once that is installed, you can use the invoke tool mentioned in the article to build and run the tests. See the tasks.py file or run invoke --list to get more details.

Special thanks to "znunu at github.com" for getting these examples running on a Windows platform!


If that's a hassle, I can do it after we get this merged. :)

@Znunu
Copy link
Contributor Author

Znunu commented Nov 6, 2020

I added the readme. Thanks for writing one! Removed the reference from both tasks.py and readme.md, don't care about being credited. Tried to add Dan's suggestion as well

Copy link
Contributor

@jima80525 jima80525 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jima80525 jima80525 merged commit a14a6fc into realpython:master Nov 8, 2020
@jima80525
Copy link
Contributor

Great work on this, @Znunu - I really appreciate you stepping up and helping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants