pymunk icon indicating copy to clipboard operation
pymunk copied to clipboard

pypi page example throwing Python-CFFI error and not terminating

Open MichaelQuaMan opened this issue 2 years ago • 2 comments

Summary

  • Initial example code not breaking on Ctrl + c
  • Throwing Python-CFFI error
image
  • Keeps running and need to find and kill the Python process

Expectation

  • Code should probably terminate on Ctrl + c and gracefully close.

Other considerations

  • It would be great if the demo has visual output:
    • there could be a tiny screenshot depicting the expected output
    • maybe a very short description of what the user should expect to see on screen.

Note: I see the code comments and they don't say what I should see on-screen.

So, textual output maybe the only output?

Not sure, but as a user it would be nice to know what to expect before I run the code.

Also, it would be great if
- Ctrl+c worked to break the loop (just for this example) so the user could quickly kill it.

Code

import pymunk  # Import pymunk..

space = pymunk.Space()  # Create a Space which contain the simulation
space.gravity = 0, -981  # Set its gravity

body = pymunk.Body()  # Create a Body
body.position = 50, 100  # Set the position of the body

poly = pymunk.Poly.create_box(body)  # Create a box shape and attach to body
poly.mass = 10  # Set the mass on the shape
space.add(body, poly)  # Add both body and shape to the simulation

print_options = pymunk.SpaceDebugDrawOptions()  # For easy printing

while True:  # Infinite loop simulation
    space.step(0.02)  # Step the simulation one step forward
    space.debug_draw(print_options)  # Print the state of the simulation

MichaelQuaMan avatar Jan 01 '23 20:01 MichaelQuaMan

Based on the exception Exception ignored from cffi callback ext_cpSpaceDebugDrawPolygonimpl, looks like the keyboardInterrupt may be ignored on purpose in the underlying C libs.

Maybe the interrupt can be caught in the pythong and call something like sys.exit()

Update

  • if I hit the Ctrl + c several times, from the terminal, it will eventually die.

Note: it still gives and audible warning and throws the CFFI exception in a popup, but it does eventually die.

It just really wants to LIVE! Who can blame it.

MichaelQuaMan avatar Jan 01 '23 20:01 MichaelQuaMan

Oh.. Maybe it would be better to limit the number of loops to 100 or something, to not let the user get trapped?

The reason why the output is text only (while many of the more advanced pymunk examples have more visually interesting output) is that I wanted the readme example to not have any additional dependencies except pymunk. Its also nice that it works directly in the terminal.

I have thought about adding a tkinter drawing module, but Im not sure how useful it would be except to make this initial quick demo in the readme nicer..

(The other examples use pygame, pyglet etc).

viblo avatar Jan 02 '23 09:01 viblo