From a4652f0324fe955564942ce4745817c71e71b21d Mon Sep 17 00:00:00 2001 From: KmolYuan Date: Tue, 14 Dec 2021 09:37:51 +0800 Subject: [PATCH] Hide copy API. --- cython/README.md | 3 --- cython/python_solvespace/slvs.pyi | 7 ++----- cython/python_solvespace/slvs.pyx | 24 ++++++++++++++---------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/cython/README.md b/cython/README.md index 490a9675..b8155246 100644 --- a/cython/README.md +++ b/cython/README.md @@ -44,9 +44,6 @@ import pickle print(pickle.dumps(sys)) sys_new = sys.copy() -# equivalent to -func, args = sys.__reduce__() -sys_new = func(*args) ``` The entity and parameter handles should have the same lifetime to the solver. diff --git a/cython/python_solvespace/slvs.pyi b/cython/python_solvespace/slvs.pyi index 11a058c9..8c2f05f8 100644 --- a/cython/python_solvespace/slvs.pyi +++ b/cython/python_solvespace/slvs.pyi @@ -95,11 +95,8 @@ class Entity: class SolverSystem: - def __init__(self, g: int = 0, param_list=None, entity_list=None, cons_list=None) -> None: - """Create a solver system. - - The current group, parameters, entities, constraints can be set from an existing solver. - """ + def __init__(self) -> None: + """Create a solver system.""" ... def __reduce__(self): diff --git a/cython/python_solvespace/slvs.pyx b/cython/python_solvespace/slvs.pyx index 52366364..72cbf3ee 100644 --- a/cython/python_solvespace/slvs.pyx +++ b/cython/python_solvespace/slvs.pyx @@ -13,6 +13,16 @@ from cpython.object cimport Py_EQ, Py_NE from collections import Counter +def _create_sys(dof_v, g, param_list, entity_list, cons_list): + cdef SolverSystem s = SolverSystem.__new__(SolverSystem) + s.dof_v = dof_v + s.g = g + s.param_list = param_list + s.entity_list = entity_list + s.cons_list = cons_list + return s + + cpdef tuple quaternion_u(double qw, double qx, double qy, double qz): """Input quaternion, return unit vector of U axis. @@ -290,17 +300,11 @@ cdef class SolverSystem: The operation of entities and constraints are using the methods of this class. """ - def __cinit__(self, int g = 0, param_list=None, entity_list=None, cons_list=None): - self.g = g - if param_list is not None: - self.param_list = param_list - if entity_list is not None: - self.entity_list = entity_list - if cons_list is not None: - self.cons_list = cons_list + def __cinit__(self): + self.g = 0 def __reduce__(self): - return (self.__class__, (self.g, self.param_list, self.entity_list, self.cons_list)) + return (_create_sys, (self.dof_v, self.g, self.param_list, self.entity_list, self.cons_list)) def entity(self, int i) -> Entity: """Generate entity handle, it can only be used with this system. @@ -316,7 +320,7 @@ cdef class SolverSystem: cpdef SolverSystem copy(self): """Copy the solver.""" - return SolverSystem.__new__(SolverSystem, self.g, self.param_list, self.entity_list, self.cons_list) + return _create_sys(self.dof_v, self.g, self.param_list, self.entity_list, self.cons_list) cpdef void clear(self): """Clear the system."""