72 Commits

Author SHA1 Message Date
John C. Allwein
21b39da65d pythonTask: fix refcount leak of non-panda Future::done
A missing Py_DECREF on the future's "done" method
caused both the bound method and the underlying
self instance of the future to be leaked when
awaiting non-panda futures (such as _asyncio.Future).

This change includes a simple new test addition
to catch this in the future.
2024-08-08 21:51:03 -06:00
rdb
e5bd00f91f task: Implement garbage collector support for PythonTask
This adds persistent wrapper support (introduced by the previous commit) to PythonTask, which makes it possible for reference cycles involving tasks to be found and destroyed.

The major caveat is that it always creates a reference cycle.  This can be broken automatically if there is no more Python reference to it by the time the last C++ reference is dropped, but the other way around requires the garbage collector.

For tasks, I think this it is generally the case that the last reference is in C++, since tasks are usually created and then handed off to the C++ task manager, and for applications that don't want to rely on the GC, it is easy to work around.  If this turns out to be a problem, though, we can add a special garbage collection pass to the task manager.
2024-03-29 20:51:45 +01:00
rdb
73360393df Switch use of Py_INCREF to new Py_NewRef function
This is more idiomatic, simplifies the code, makes it easier to port to eg. HPy later on, and makes it easier to use the limited ABI (where it can be optimized to a tail call) later on

A polyfill is provided for compatibility with Python versions 3.8 and 3.9
2024-01-29 17:57:53 +01:00
rdb
f1f15a92a4 task: Fix refcount leak when assigning attribute of task object
Regression on 1.11
2024-01-29 16:03:43 +01:00
rdb
c99fe581d3 Merge branch 'release/1.10.x' 2023-09-16 09:57:09 +02:00
rdb
0bc290eb2c event: Replace _PyGen_FetchStopIterationValue in Python 3.13+
See python/cpython#106320 and python/cpython#107032

Closes #1526
2023-08-05 16:37:01 +02:00
rdb
20334cce05 Merge branch 'release/1.10.x' 2022-06-29 19:09:59 +02:00
rdb
9ab460c900 event: Fix memory leak in debug check of task.set_owner(...)
Fixes #1328
2022-06-29 15:13:57 +02:00
rdb
a0be50c769 general: Fix assorted compiler warnings 2022-03-01 16:09:07 +01:00
rdb
5ef1b44455 event: cancel future being awaited when cancelling coroutine task
Fixes #1136
2021-04-18 17:05:00 +02:00
rdb
8cbe36b352 Merge branch 'release/1.10.x' 2021-03-01 19:33:27 +01:00
rdb
6a51709332 event: Don't suppress coroutine exceptions in optimized builds 2021-02-26 11:44:50 +01:00
rdb
a64dcd7c5d event: (BREAKING CHANGE) bare yield in task now means "cont"
This is more useful and consistent; you can after all just use "return" to end the task, whereas you can now just use "yield" to continue the next frame rather than "yield Task.cont".

This is a follow-up to f6b39345f718b3ea9e6d01e9e71c6265a8511e58, which already enabled this behavior when an __await__ (that is awaited from a task) yielded None.
2021-02-07 15:14:04 +01:00
rdb
e8b4b6476c Merge branch 'release/1.10.x' 2021-02-07 15:01:36 +01:00
rdb
8c8a73a1a7 event: Fix compilation with Python 2.7 2021-02-07 14:49:10 +01:00
rdb
f6b39345f7 event: don't exit task if future __await__ yields None
This matches the behavior of asyncio's Task implementation, where this is the equivalent of `yield Task.cont`.

I've kept regular generator tasks unaffected for now, since this might break existing usage.
2021-02-07 12:38:16 +01:00
rdb
a1b2d5b8dc Make use of new function call methods in Python 3.9 2020-12-12 17:28:21 +01:00
Daniel
4f63ef635d panda: Remove PyEval_InitThreads calls when compiling against Python 3.9+
Cherry-pick from #1053
2020-12-12 00:16:58 +01:00
Daniel
54cb519dc9 panda: Remove PyEval_InitThreads calls when compiling against Python 3.9+
Closes #1053
2020-11-23 10:56:09 +01:00
rdb
3598222977 Merge branch 'release/1.10.x' 2020-05-05 16:47:38 +02:00
rdb
bfbbcad990 task: Support calling cancel() on currently awaiting futures
Fixes #911
2020-05-05 13:21:49 +02:00
rdb
b5d0e9eafc task: Fix memory leak when removing a task awaiting non-Panda future 2020-05-05 13:19:26 +02:00
rdb
d799a09002 cleanup: Remove support for EOL versions of Python
Fixes #905
2020-04-26 20:07:56 +02:00
Sam Edwards
856754a3de general: Don't abuse PyErr_Restore
The intended purpose of this function is to restore an exception
that has already been raised and saved with PyErr_Fetch. It should
not be used to raise new exceptions nor should it be used to clear
the current exception.

The especially egregious example is
`PyErr_Restore(exc_type, nullptr, nullptr);`
as the null value may not be handled correctly.
2019-11-05 18:44:20 -07:00
rdb
71b8bceb79 task: allow overriding methods of PythonTask 2019-03-04 22:58:50 +01:00
rdb
e64a342a9f task: fix error awaiting already scheduled task
To reproduce this issue, try `await taskMgr.add(Task.pause(1.0))`
2019-01-09 20:10:10 +01:00
Sam Edwards
b2bfb31114 general: Remove using std::* from headers
Also remove most `using namespace std;` statements. The only one that remains is in py_panda.h.

Closes #350
Closes #335
2018-06-14 16:04:49 +02:00
Sam Edwards
e2b4353800 general: Replace NULL (and 0 as pointer) with C++11 nullptr
Exceptions to this replacement are:
- .c files
- Headers included by a .c file
- stb_image.h
- dr_flac.h
- Strings
- Comments
2018-06-03 16:35:13 -06:00
rdb
b45726001e task: fix double free when failing to retrieve coroutine exception 2018-05-05 20:53:04 +02:00
rdb
ed5e5386b9 AsyncFuture improvements, incl. support for gathering futures 2017-12-21 14:07:01 +01:00
rdb
193e4b5f59 interrogate: clean py_panda.h; use macros to access Dtool_PyInstDef 2017-12-12 23:03:38 +01:00
rdb
2e20a0f16e Implement awaitable thread-safe future for async operations
This introduces AsyncFuture as a new base class of AsyncTask.  It's modelled after asyncio's Future class, except that it is thread-safe and you can use result() to block the current thread waiting for the future to finish (of course this is not necessary for use with coroutines).

AsyncFuture should be used for any operation that finishes in the future, to get the benefit of awaitability within coroutines as well as a standard interface for querying status and results of the operation as well as cancelling it.  As such, it's been implemented in various places, including texture.prepare() and win.trigger_copy().

Note that AsyncFuture is intended to be used *once*; it cannot be used more than once.  As an example of how this works, tex.prepare() will return the same future as long as the prepare isn't complete, but when it is done, subsequent calls to tex.prepare() will return a new future.
2017-12-04 22:25:28 +01:00
rdb
0c0f9adab9 Support coroutines and async/await in the task manager and loader 2017-10-31 18:56:41 +01:00
rdb
47388b0dbc Separate out CPython calls from core libs; eliminate need for pystub 2016-07-20 14:30:41 +02:00
rdb
85386049f0 Merge branch 'release/1.9.x' 2016-07-09 15:39:07 +02:00
rdb
47d1b4e220 PythonTask: don't crash if repr(owner) errors, deal better with bad owners 2016-07-09 10:02:55 +02:00
rdb
7d6b7f40f5 Merge branch 'release/1.9.x' 2016-06-09 21:52:51 +02:00
rdb
b303962e96 Disable cycle detection in tasks for now, it is way too unstable 2016-06-09 16:48:31 +02:00
rdb
e9c76328fb Fix a crash in PythonTask destructor 2016-06-08 13:18:31 +02:00
tobspr
0fcfb8e372 New file headers, new comment style 2016-02-17 17:47:48 +01:00
rdb
b63d5fbe8c Support cyclic garbage collection through PythonTask objects 2015-03-15 12:24:06 +01:00
rdb
4dc4c9eb3d Merge branch 'master' into interrogate-overhaul
Conflicts:
	dtool/src/interrogate/functionRemap.cxx
	dtool/src/interrogate/interfaceMakerPythonNative.cxx
	panda/src/event/pythonTask.cxx
	panda/src/express/multifile.h
	panda/src/express/pointerToArray.I
	panda/src/mathutil/config_mathutil.N
	panda/src/putil/config_util.N
	panda/src/putil/doubleBitMask.h
2015-02-11 14:41:58 +01:00
rdb
cbd70ad8af Move all Python bindings out of core DLLs into the .pyd files. 2015-02-10 14:30:51 +01:00
rdb
6db5d0dc11 Huge cppparser and interrogate overhaul, bringing:
* Better support for typedefs
* Same code should compile for both 32-bit and 64-bit
* C++11 typed enum support
* Synthesize __setitem__ when operator [] returns reference
* Rewrite coerce system to have better performance
* Change semantics of __getattr__ and __setattr__ to match
  Python's, add __getattribute__, __delattr__ and __delitem__
* Improve performance of slot functions a bit more
* Reduce memory overhead of type system
* Some support for wrapping arrays of numeric types
* C++11 character type support in cppparser
* Chars are handled as strings of length 1
* Template functions for CreatePyInstance that use runtime type map
* More functions from dtool as extension functions
* Code cleanup
2014-12-30 16:43:52 +01:00
rdb
a65b477734 Commit patches by cfsworks:
cxx: Fix missing includes masked by composite builds.
misc: Fix typos in comments.
config: Fix missing config forward-declarations.
direct: Adjust Python imports to panda3d.* instead of pandac.*.
display: Split graphicsWindow out into an extension.
framework: Use if/elif/elif/endif instead of ifdef/elif/elif/endif.
ode: Remove erroneous INLINE declarations.
interval: Fix missing import.
2014-10-19 22:41:47 +00:00
rdb
5f2c0a6e04 Big commit aiding in separation of Python bindings:
* Most extension functions have been moved to C++.
 * In makepanda, Python bindings are compiled into panda3d/<module>.pyd.
2014-02-20 13:33:26 +00:00
rdb
40cddd6280 Fixes for Python 3.2 2013-03-27 15:48:24 +00:00
rdb
192d10b937 Experimental Python 3 support to the C++ code including interrogate. Also adds support for rich comparison in interrogate. 2013-02-16 19:22:26 +00:00
David Rose
646f12f625 squelch backtrace on SystemExit 2012-02-16 00:19:09 +00:00
Darren Ranalli
0955e0aabc make sure DirectObject._addTask and ._clearTask are called only once per task create/destroy 2010-01-13 23:37:56 +00:00