commit 1ae6809bce7c79ec5bf63b22790ec3d9f0d943cf
parent 6dfb840cac304b0a6a64afe4c5f8bf89ea0e33cb
Author: gracefu <81774659+gracefuu@users.noreply.github.com>
Date: Wed, 23 Apr 2025 02:55:41 +0800
Merge pickler and rebuild
Diffstat:
4 files changed, 91 insertions(+), 92 deletions(-)
diff --git a/make3/__init__.py b/make3/__init__.py
@@ -1,4 +1,3 @@
from .io import *
-from .pickler import *
from .rebuild import *
from .helpers import *
diff --git a/make3/pickler.py b/make3/pickler.py
@@ -1,88 +0,0 @@
-from io import BytesIO
-from types import CellType, CodeType, FunctionType
-from typing import Any
-from copyreg import dispatch_table
-from importlib import import_module
-from inspect import getmodule
-from pickle import Pickler
-
-
-def pickle_code_type(code: CodeType):
- return (
- unpickle_code_type,
- (
- code.co_argcount,
- code.co_posonlyargcount,
- code.co_kwonlyargcount,
- code.co_nlocals,
- code.co_stacksize,
- code.co_flags,
- code.co_code,
- code.co_consts,
- code.co_names,
- code.co_varnames,
- code.co_filename,
- code.co_name,
- code.co_qualname,
- code.co_firstlineno,
- code.co_linetable,
- code.co_exceptiontable,
- code.co_freevars,
- code.co_cellvars,
- ),
- )
-
-
-def unpickle_code_type(*args):
- return CodeType(*args)
-
-
-def pickle_cell_type(cell: CellType):
- return (unpickle_cell_type, (cell.cell_contents,))
-
-
-def unpickle_cell_type(*args):
- return CellType(*args)
-
-
-def pickle_function_type(f: FunctionType):
- mod = getmodule(f)
- return (
- unpickle_function_type,
- (
- f.__code__,
- mod.__name__ if mod is not None else None,
- (
- tuple(CellType(cell.cell_contents) for cell in f.__closure__)
- if f.__closure__
- else None
- ),
- ),
- )
-
-
-def unpickle_function_type(code, mod_name, closure):
- return FunctionType(code, globals=import_module(mod_name).__dict__, closure=closure)
-
-
-class FunctionPickler(Pickler):
- dispatch_table = dispatch_table.copy()
- dispatch_table[CodeType] = pickle_code_type
- dispatch_table[CellType] = pickle_cell_type
-
- def reducer_override(self, obj): # type: ignore
- if type(obj) is not FunctionType:
- return NotImplemented
- obj_mod = getmodule(obj)
- if obj_mod is None:
- return NotImplemented
- if obj.__name__ in dir(obj_mod):
- return NotImplemented
- return pickle_function_type(obj)
-
-
-def pickle_with(pickler_cls: type, obj: Any) -> bytes:
- i = BytesIO()
- pickler_cls(i).dump(obj)
- i.seek(0)
- return i.read()
diff --git a/make3/rebuild.py b/make3/rebuild.py
@@ -1,9 +1,97 @@
-from .pickler import FunctionPickler, pickle_with
-from typing import Any, Callable, overload
from contextvars import ContextVar, copy_context
+from copyreg import dispatch_table
from functools import wraps
+from importlib import import_module
+from inspect import getmodule
+from io import BytesIO
+from pickle import Pickler
+from types import CellType, CodeType, FunctionType
+from typing import Any
+from typing import Any, Callable, overload
import pickle
+
+def pickle_code_type(code: CodeType):
+ return (
+ unpickle_code_type,
+ (
+ code.co_argcount,
+ code.co_posonlyargcount,
+ code.co_kwonlyargcount,
+ code.co_nlocals,
+ code.co_stacksize,
+ code.co_flags,
+ code.co_code,
+ code.co_consts,
+ code.co_names,
+ code.co_varnames,
+ code.co_filename,
+ code.co_name,
+ code.co_qualname,
+ code.co_firstlineno,
+ code.co_linetable,
+ code.co_exceptiontable,
+ code.co_freevars,
+ code.co_cellvars,
+ ),
+ )
+
+
+def unpickle_code_type(*args):
+ return CodeType(*args)
+
+
+def pickle_cell_type(cell: CellType):
+ return (unpickle_cell_type, (cell.cell_contents,))
+
+
+def unpickle_cell_type(*args):
+ return CellType(*args)
+
+
+def pickle_function_type(f: FunctionType):
+ mod = getmodule(f)
+ return (
+ unpickle_function_type,
+ (
+ f.__code__,
+ mod.__name__ if mod is not None else None,
+ (
+ tuple(CellType(cell.cell_contents) for cell in f.__closure__)
+ if f.__closure__
+ else None
+ ),
+ ),
+ )
+
+
+def unpickle_function_type(code, mod_name, closure):
+ return FunctionType(code, globals=import_module(mod_name).__dict__, closure=closure)
+
+
+class FunctionPickler(Pickler):
+ dispatch_table = dispatch_table.copy()
+ dispatch_table[CodeType] = pickle_code_type
+ dispatch_table[CellType] = pickle_cell_type
+
+ def reducer_override(self, obj): # type: ignore
+ if type(obj) is not FunctionType:
+ return NotImplemented
+ obj_mod = getmodule(obj)
+ if obj_mod is None:
+ return NotImplemented
+ if obj.__name__ in dir(obj_mod):
+ return NotImplemented
+ return pickle_function_type(obj)
+
+
+def pickle_with(pickler_cls: type, obj: Any) -> bytes:
+ i = BytesIO()
+ pickler_cls(i).dump(obj)
+ i.seek(0)
+ return i.read()
+
+
rerun_db_var: ContextVar[dict] = ContextVar("rerun_db")
rerun_changes_var: ContextVar[list[tuple[Any, bytes]]] = ContextVar("rerun_changes")
diff --git a/tar-sketch/a.txt b/tar-sketch/a.txt
@@ -1 +1 @@
-Wed Apr 23 02:53:06 AM +08 2025
+Wed Apr 23 02:56:16 AM +08 2025