瀏覽代碼

pgraph: Add pickling for LoaderFileTypeRegistry

Useful to test that pickling singletons works
rdb 5 年之前
父節點
當前提交
e755f87130

+ 2 - 0
panda/src/pgraph/loaderFileTypeRegistry.h

@@ -53,6 +53,8 @@ PUBLISHED:
 
   static LoaderFileTypeRegistry *get_global_ptr();
 
+  EXTENSION(PyObject *__reduce__() const);
+
 private:
   void record_extension(const std::string &extension, LoaderFileType *type);
 

+ 10 - 0
panda/src/pgraph/loaderFileTypeRegistry_ext.cxx

@@ -18,6 +18,7 @@
 #include "pythonLoaderFileType.h"
 
 extern struct Dtool_PyTypedObject Dtool_LoaderFileType;
+extern struct Dtool_PyTypedObject Dtool_LoaderFileTypeRegistry;
 
 /**
  * Registers a loader file type that is implemented in Python.
@@ -112,4 +113,13 @@ unregister_type(PyObject *type) {
   Py_XDECREF(save_func);
 }
 
+/**
+ * Implements pickle support.
+ */
+PyObject *Extension<LoaderFileTypeRegistry>::
+__reduce__() const {
+  PyObject *func = PyObject_GetAttrString((PyObject *)&Dtool_LoaderFileTypeRegistry, "get_global_ptr");
+  return Py_BuildValue("N()", func);
+}
+
 #endif

+ 2 - 0
panda/src/pgraph/loaderFileTypeRegistry_ext.h

@@ -33,6 +33,8 @@ public:
   void register_deferred_type(PyObject *entry_point);
 
   void unregister_type(PyObject *type);
+
+  PyObject *__reduce__() const;
 };
 
 #endif  // HAVE_PYTHON

+ 9 - 0
tests/pgraph/test_loader_types.py

@@ -3,6 +3,7 @@ import pytest
 import tempfile
 import os
 from contextlib import contextmanager
+import sys
 
 
 @pytest.fixture
@@ -218,3 +219,11 @@ def test_loader_ram_cache(test_filename):
         assert model1 == model2
 
         ModelPool.release_model(model2)
+
+
[email protected](sys.version_info < (3, 4), reason="Requires Python 3.4")
+def test_loader_file_type_registry_pickle():
+    from direct.stdpy.pickle import dumps, loads
+
+    registry = LoaderFileTypeRegistry.get_global_ptr()
+    assert loads(dumps(registry, -1)) == registry