zig-zon 0.3.1__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0}/LICENSE +1 -1
- {zig-zon-0.3.1 → zig-zon-0.4.0}/PKG-INFO +8 -11
- {zig-zon-0.3.1 → zig-zon-0.4.0}/build.zig +10 -3
- {zig-zon-0.3.1 → zig-zon-0.4.0}/build.zig.zon +4 -4
- {zig-zon-0.3.1 → zig-zon-0.4.0}/pkg-info.zon +1 -0
- zig-zon-0.4.0/setup.zig.json +1 -0
- {zig-zon-0.3.1 → zig-zon-0.4.0}/src/zig_zon.zig +249 -173
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/README.md +3 -1
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/build.zig +0 -2
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/build.zig.zon +2 -2
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/CPython.zig +1 -1
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/python.zig +2 -4
- zig-zon-0.3.1/setup.zig.json +0 -1
- {zig-zon-0.3.1 → zig-zon-0.4.0}/setup.py +0 -0
- {zig-zon-0.3.1 → zig-zon-0.4.0}/src/zig_zon.pyi +0 -0
- {zig-zon-0.3.1 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/LICENSE +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/pkg-info.zon +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/Compression.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/LowerDashString.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/SDistStep.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/VenvInstallTest.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/extract_link_info.py +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/pip_runner.py +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/pkg_info.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/python-translatec.h +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/python_allocator.zig +0 -0
- {zig-zon-0.3.1/zig-pkg/zig_python_sdist-0.0.8 → zig-zon-0.4.0/zig-pkg/zig_python_sdist-0.1.0}/src/build/setup_json.zig +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: zig-zon
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: a native cpython extension to parse zig object notation strings
|
|
5
5
|
Author-email: Tobias Simetsreiter <dasimmet@gmail.com>
|
|
6
6
|
Maintainer-email: Tobias Simetsreiter <dasimmet@gmail.com>
|
|
@@ -9,6 +9,7 @@ Keywords: zig,ziglang,cpython,native,sdist,zon
|
|
|
9
9
|
Requires-Python: >=3.8
|
|
10
10
|
Project-URL: Homepage, https://codeberg.org/dasimmet/python-zig-zon
|
|
11
11
|
Project-URL: Issues, https://codeberg.org/dasimmet/python-zig-zon/issues
|
|
12
|
+
Project-URL: Source, https://codeberg.org/dasimmet/python-zig-zon
|
|
12
13
|
Classifier: Development Status :: 4 - Beta
|
|
13
14
|
Classifier: Programming Language :: Python :: 3
|
|
14
15
|
Classifier: Programming Language :: Zig
|
|
@@ -27,13 +28,13 @@ It therefore needs to download the `ziglang` python package.
|
|
|
27
28
|
Depending on your OS, you may need the python development headers
|
|
28
29
|
(e.g. `python3-dev`) installed.
|
|
29
30
|
|
|
30
|
-
## Installation
|
|
31
|
+
## Installation from [PyPi](https://pypi.org/project/zig-zon) or [Release](https://codeberg.org/dasimmet/python-zig-zon/releases)
|
|
31
32
|
|
|
32
33
|
```bash
|
|
33
34
|
# from PyPi: https://pypi.org/project/zig-zon
|
|
34
35
|
pip install zig-zon
|
|
35
|
-
# directly from the Codeberg Release:
|
|
36
|
-
pip install https://codeberg.org/dasimmet/python-zig-zon/releases/download/0.3.
|
|
36
|
+
# directly from the Codeberg Release (check for the latest version):
|
|
37
|
+
pip install https://codeberg.org/dasimmet/python-zig-zon/releases/download/0.3.2/zig_zon-0.3.2.tar.gz
|
|
37
38
|
```
|
|
38
39
|
|
|
39
40
|
## Parsing
|
|
@@ -123,7 +124,7 @@ with open("build.zig.zon", "w", encoding="utf-8") as f:
|
|
|
123
124
|
- `zig_zon.BareEnum`: Serialized as bare enums `.identifier`.
|
|
124
125
|
- `zig_zon.QuotedEnum`: Serialized as quoted enums `.@"identifier"`.
|
|
125
126
|
- `zig_zon.Enum`: Serialized as enum literals (bare or quoted as necessary).
|
|
126
|
-
- `int`, `zig_zon.Integer`, `zig_zon.Hex`: Formatted according to `integer_mode
|
|
127
|
+
- `int`, `zig_zon.Integer`, `zig_zon.Hex`: Formatted according to `integer_mode` (supports arbitrary precision).
|
|
127
128
|
- `float`, `zig_zon.Float`: Serialized as float literals.
|
|
128
129
|
- `bool`: Serialized as `true` or `false`.
|
|
129
130
|
- `None`: Serialized as `null`.
|
|
@@ -134,8 +135,8 @@ The parser returns custom classes for each Zig type that wrap and subclass stand
|
|
|
134
135
|
|
|
135
136
|
- `structs` -> `zig_zon.Struct` (subclasses `dict`)
|
|
136
137
|
- `tuples` -> `zig_zon.Tuple` (subclasses `list`)
|
|
137
|
-
- `integer literals` -> `zig_zon.Integer` (subclasses `int
|
|
138
|
-
- `hex literals` -> `zig_zon.Hex` (wraps / subclasses `int
|
|
138
|
+
- `integer literals` -> `zig_zon.Integer` (subclasses `int`, arbitrary precision)
|
|
139
|
+
- `hex literals` -> `zig_zon.Hex` (wraps / subclasses `int`, arbitrary precision)
|
|
139
140
|
- `float literals` -> `zig_zon.Float` (subclasses `float`)
|
|
140
141
|
- `booleans` -> `bool` (Python built-in `bool` is final and cannot be subclassed)
|
|
141
142
|
- `null` -> `None` (Python `NoneType` is final and cannot be subclassed)
|
|
@@ -146,10 +147,6 @@ The parser returns custom classes for each Zig type that wrap and subclass stand
|
|
|
146
147
|
|
|
147
148
|
Because the custom classes wrap and subclass their respective Python base types, both `isinstance(val, BaseType)` and `isinstance(val, zig_zon.CustomClass)` evaluate to `True`.
|
|
148
149
|
|
|
149
|
-
## Not supported values
|
|
150
|
-
|
|
151
|
-
- `bigint`
|
|
152
|
-
|
|
153
150
|
## How it works
|
|
154
151
|
|
|
155
152
|
The `zig build` commands produces a python source distribution archive ready
|
|
@@ -6,6 +6,12 @@ const zon = @import("build.zig.zon");
|
|
|
6
6
|
pub fn build(b: *Build) void {
|
|
7
7
|
const target = b.standardTargetOptions(.{});
|
|
8
8
|
const optimize = b.standardOptimizeOption(.{});
|
|
9
|
+
const limited_api_opt = b.option(
|
|
10
|
+
[]const u8,
|
|
11
|
+
"py-limited-api",
|
|
12
|
+
"the python limited api to target. defaults to python3.8. set to empty string to disable",
|
|
13
|
+
) orelse "cp38";
|
|
14
|
+
const limited_api = if (limited_api_opt.len == 0) null else limited_api_opt;
|
|
9
15
|
|
|
10
16
|
// this target and section of our build is used at pip install time
|
|
11
17
|
const ext_install_step = b.step("bdist-wheel", "build python extension module");
|
|
@@ -18,7 +24,7 @@ pub fn build(b: *Build) void {
|
|
|
18
24
|
.name = "zig-zon",
|
|
19
25
|
.version = zon.version,
|
|
20
26
|
.description = b.path("README.md"),
|
|
21
|
-
.py_limited_api =
|
|
27
|
+
.py_limited_api = limited_api,
|
|
22
28
|
.build = .{
|
|
23
29
|
.args = &.{
|
|
24
30
|
ext_install_step.name,
|
|
@@ -61,6 +67,7 @@ pub fn build(b: *Build) void {
|
|
|
61
67
|
.target = target,
|
|
62
68
|
.version = zon.version,
|
|
63
69
|
.optimize = .ReleaseSmall,
|
|
70
|
+
.py_limited_api = limited_api,
|
|
64
71
|
});
|
|
65
72
|
const lib = b.addLibrary(.{
|
|
66
73
|
.name = "zig_zon",
|
|
@@ -123,11 +130,11 @@ pub fn build(b: *Build) void {
|
|
|
123
130
|
const check_fmt = opt_check_fmt or opt_fmt_check;
|
|
124
131
|
const fmt_step = b.step("fmt", "Run zig fmt");
|
|
125
132
|
const fmt = b.addFmt(.{
|
|
126
|
-
.paths = &.{
|
|
133
|
+
.paths = b.pathList(&.{
|
|
127
134
|
"src",
|
|
128
135
|
"build.zig",
|
|
129
136
|
"build.zig.zon",
|
|
130
|
-
},
|
|
137
|
+
}),
|
|
131
138
|
.check = check_fmt,
|
|
132
139
|
});
|
|
133
140
|
fmt_step.dependOn(&fmt.step);
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
.{
|
|
2
2
|
.name = .python_zig_zon,
|
|
3
3
|
.fingerprint = 0x3d332656a3644fc9,
|
|
4
|
-
.version = "0.
|
|
5
|
-
.minimum_zig_version = "0.
|
|
4
|
+
.version = "0.4.0",
|
|
5
|
+
.minimum_zig_version = "0.17.1",
|
|
6
6
|
.dependencies = .{
|
|
7
7
|
.zig_python_sdist = .{
|
|
8
|
-
.url = "git+https://codeberg.org/dasimmet/zig-python-sdist.git?ref=0.0
|
|
9
|
-
.hash = "zig_python_sdist-0.0
|
|
8
|
+
.url = "git+https://codeberg.org/dasimmet/zig-python-sdist.git?ref=0.1.0#61ba060941eed25d48e3fbd7bc5347b036049e6b",
|
|
9
|
+
.hash = "zig_python_sdist-0.1.0-wjgdOtSFAQDLy6b2unaGk3F6S9vT8Nu-_3PeLMPnI-cU",
|
|
10
10
|
},
|
|
11
11
|
},
|
|
12
12
|
.paths = .{
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
.{ .key = "Requires-Python", .value = ">=3.8" },
|
|
8
8
|
.{ .key = "Project-URL", .value = "Homepage, https://codeberg.org/dasimmet/python-zig-zon" },
|
|
9
9
|
.{ .key = "Project-URL", .value = "Issues, https://codeberg.org/dasimmet/python-zig-zon/issues" },
|
|
10
|
+
.{ .key = "Project-URL", .value = "Source, https://codeberg.org/dasimmet/python-zig-zon" },
|
|
10
11
|
.{ .key = "Classifier", .value = "Development Status :: 4 - Beta" },
|
|
11
12
|
.{ .key = "Classifier", .value = "Programming Language :: Python :: 3" },
|
|
12
13
|
.{ .key = "Classifier", .value = "Programming Language :: Zig" },
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"zig_zon","version":{"major":0,"minor":4,"patch":0},"zig_version":{"major":0,"minor":17,"patch":1},"build":{"args":["--fork=zig-pkg/zig_python_sdist-0.1.0","bdist-wheel","-Doptimize=small"],"requires":[],"packages":[],"copy_patterns":[{"pattern":"zig-out/lib/*","package_dir":false}]},"py_limited_api":"cp38","has_ext_modules":true}
|
|
@@ -8,7 +8,6 @@ const Serializer = std.zon.Serializer;
|
|
|
8
8
|
const cpython = @import("cpython");
|
|
9
9
|
const c = cpython.c;
|
|
10
10
|
const PyObject = c.PyObject;
|
|
11
|
-
|
|
12
11
|
pub var zigmodule: c.PyModuleDef = .{
|
|
13
12
|
.m_name = "zig_zon",
|
|
14
13
|
.m_methods = @constCast(&[_]c.PyMethodDef{
|
|
@@ -34,7 +33,7 @@ pub var zigmodule: c.PyModuleDef = .{
|
|
|
34
33
|
}),
|
|
35
34
|
};
|
|
36
35
|
|
|
37
|
-
pub export fn PyInit_zig_zon() [*c]
|
|
36
|
+
pub export fn PyInit_zig_zon() [*c]PyObject {
|
|
38
37
|
const mod = c.PyModule_Create(&zigmodule);
|
|
39
38
|
if (mod == null) return null;
|
|
40
39
|
|
|
@@ -46,22 +45,91 @@ pub export fn PyInit_zig_zon() [*c]c.PyObject {
|
|
|
46
45
|
return mod;
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var
|
|
56
|
-
var
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
const PyStr = struct {
|
|
49
|
+
bytes: *PyObject,
|
|
50
|
+
slice: [:0]const u8,
|
|
51
|
+
|
|
52
|
+
pub fn init(unicode_obj: *PyObject) ?PyStr {
|
|
53
|
+
const bytes = c.PyUnicode_AsUTF8String(unicode_obj) orelse return null;
|
|
54
|
+
var len: c.Py_ssize_t = 0;
|
|
55
|
+
var ptr: [*c]u8 = undefined;
|
|
56
|
+
if (c.PyBytes_AsStringAndSize(bytes, &ptr, &len) < 0) {
|
|
57
|
+
c.Py_DecRef(bytes);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return PyStr{
|
|
61
|
+
.bytes = bytes,
|
|
62
|
+
.slice = ptr[0..@intCast(len) :0],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pub fn deinit(self: PyStr) void {
|
|
67
|
+
c.Py_DecRef(self.bytes);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
inline fn PyObject_CallNoArgs(func: *PyObject) ?*PyObject {
|
|
72
|
+
return c.PyObject_CallObject(func, null);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
inline fn PyObject_CallOneArg(func: *PyObject, arg: *PyObject) ?*PyObject {
|
|
76
|
+
return c.PyObject_CallFunctionObjArgs(func, arg, @as(?*anyopaque, null));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
inline fn PyType_GetName(op: *c.PyObject) ?*PyObject {
|
|
80
|
+
const typ: *PyObject = @ptrCast(@alignCast(c.Py_TYPE(op) orelse return null));
|
|
81
|
+
return c.PyObject_GetAttrString(typ, "__name__");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn getBuiltinType(name: [*:0]const u8) ?*PyObject {
|
|
85
|
+
const builtins = c.PyImport_ImportModule("builtins") orelse return null;
|
|
86
|
+
defer c.Py_DecRef(builtins);
|
|
87
|
+
return c.PyObject_GetAttrString(builtins, name);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
var PyDict_Type: *PyObject = undefined;
|
|
91
|
+
var PyList_Type: *PyObject = undefined;
|
|
92
|
+
var PyLong_Type: *PyObject = undefined;
|
|
93
|
+
var PyFloat_Type: *PyObject = undefined;
|
|
94
|
+
var PyUnicode_Type: *PyObject = undefined;
|
|
95
|
+
var PyBytes_Type: *PyObject = undefined;
|
|
96
|
+
var PyByteArray_Type: *PyObject = undefined;
|
|
97
|
+
var PySet_Type: *PyObject = undefined;
|
|
98
|
+
var PyFrozenSet_Type: *PyObject = undefined;
|
|
99
|
+
var PyTrue_Obj: *PyObject = undefined;
|
|
100
|
+
var PyFalse_Obj: *PyObject = undefined;
|
|
101
|
+
var Struct_Type: *PyObject = undefined;
|
|
102
|
+
var Tuple_Type: *PyObject = undefined;
|
|
103
|
+
var Integer_Type: *PyObject = undefined;
|
|
104
|
+
var Hex_Type: *PyObject = undefined;
|
|
105
|
+
var Float_Type: *PyObject = undefined;
|
|
106
|
+
var String_Type: *PyObject = undefined;
|
|
107
|
+
var ByteString_Type: *PyObject = undefined;
|
|
108
|
+
var Enum_Type: *PyObject = undefined;
|
|
109
|
+
var BareEnum_Type: *PyObject = undefined;
|
|
110
|
+
var QuotedEnum_Type: *PyObject = undefined;
|
|
111
|
+
|
|
112
|
+
inline fn PyBool_Check(obj: *c.PyObject) bool {
|
|
113
|
+
return obj == PyTrue_Obj or obj == PyFalse_Obj;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
inline fn PyFloat_Check(obj: *c.PyObject) bool {
|
|
117
|
+
return c.PyObject_IsInstance(obj, PyFloat_Type) == 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
inline fn PyByteArray_Check(obj: *c.PyObject) bool {
|
|
121
|
+
return c.PyObject_IsInstance(obj, PyByteArray_Type) == 1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
inline fn PyAnySet_Check(obj: *c.PyObject) bool {
|
|
125
|
+
return c.PyObject_IsInstance(obj, PySet_Type) == 1 or c.PyObject_IsInstance(obj, PyFrozenSet_Type) == 1;
|
|
126
|
+
}
|
|
59
127
|
|
|
60
128
|
fn createSubclass(
|
|
61
|
-
base: *c
|
|
129
|
+
base: [*c]PyObject,
|
|
62
130
|
name: [*:0]const u8,
|
|
63
131
|
doc: [*:0]const u8,
|
|
64
|
-
) ?*
|
|
132
|
+
) ?*PyObject {
|
|
65
133
|
const slots = [_]c.PyType_Slot{
|
|
66
134
|
.{ .slot = c.Py_tp_base, .pfunc = @ptrCast(base) },
|
|
67
135
|
.{ .slot = c.Py_tp_doc, .pfunc = @ptrCast(@constCast(doc)) },
|
|
@@ -78,28 +146,41 @@ fn createSubclass(
|
|
|
78
146
|
}
|
|
79
147
|
|
|
80
148
|
fn initCustomTypes(mod: [*c]c.PyObject) bool {
|
|
81
|
-
|
|
149
|
+
PyDict_Type = getBuiltinType("dict") orelse return false;
|
|
150
|
+
PyList_Type = getBuiltinType("list") orelse return false;
|
|
151
|
+
PyLong_Type = getBuiltinType("int") orelse return false;
|
|
152
|
+
PyFloat_Type = getBuiltinType("float") orelse return false;
|
|
153
|
+
PyUnicode_Type = getBuiltinType("str") orelse return false;
|
|
154
|
+
PyBytes_Type = getBuiltinType("bytes") orelse return false;
|
|
155
|
+
PyByteArray_Type = getBuiltinType("bytearray") orelse return false;
|
|
156
|
+
PySet_Type = getBuiltinType("set") orelse return false;
|
|
157
|
+
PyFrozenSet_Type = getBuiltinType("frozenset") orelse return false;
|
|
158
|
+
|
|
159
|
+
PyTrue_Obj = c.PyBool_FromLong(1) orelse return false;
|
|
160
|
+
PyFalse_Obj = c.PyBool_FromLong(0) orelse return false;
|
|
161
|
+
|
|
162
|
+
Struct_Type = createSubclass(PyDict_Type, "zig_zon.Struct", "a zon struct wrapping dict") orelse return false;
|
|
82
163
|
assert(0 <= c.PyObject_SetAttrString(mod, "Struct", Struct_Type));
|
|
83
164
|
|
|
84
|
-
Tuple_Type = createSubclass(
|
|
165
|
+
Tuple_Type = createSubclass(PyList_Type, "zig_zon.Tuple", "a zon tuple wrapping list") orelse return false;
|
|
85
166
|
assert(0 <= c.PyObject_SetAttrString(mod, "Tuple", Tuple_Type));
|
|
86
167
|
|
|
87
|
-
Integer_Type = createSubclass(
|
|
168
|
+
Integer_Type = createSubclass(PyLong_Type, "zig_zon.Integer", "a zon integer wrapping int") orelse return false;
|
|
88
169
|
assert(0 <= c.PyObject_SetAttrString(mod, "Integer", Integer_Type));
|
|
89
170
|
|
|
90
|
-
Hex_Type = createSubclass(
|
|
171
|
+
Hex_Type = createSubclass(PyLong_Type, "zig_zon.Hex", "a zon hex wrapping int") orelse return false;
|
|
91
172
|
assert(0 <= c.PyObject_SetAttrString(mod, "Hex", Hex_Type));
|
|
92
173
|
|
|
93
|
-
Float_Type = createSubclass(
|
|
174
|
+
Float_Type = createSubclass(PyFloat_Type, "zig_zon.Float", "a zon float wrapping float") orelse return false;
|
|
94
175
|
assert(0 <= c.PyObject_SetAttrString(mod, "Float", Float_Type));
|
|
95
176
|
|
|
96
|
-
String_Type = createSubclass(
|
|
177
|
+
String_Type = createSubclass(PyUnicode_Type, "zig_zon.String", "a zon string wrapping str") orelse return false;
|
|
97
178
|
assert(0 <= c.PyObject_SetAttrString(mod, "String", String_Type));
|
|
98
179
|
|
|
99
|
-
ByteString_Type = createSubclass(
|
|
180
|
+
ByteString_Type = createSubclass(PyBytes_Type, "zig_zon.ByteString", "a zon bytestring wrapping bytes") orelse return false;
|
|
100
181
|
assert(0 <= c.PyObject_SetAttrString(mod, "ByteString", ByteString_Type));
|
|
101
182
|
|
|
102
|
-
Enum_Type = createSubclass(
|
|
183
|
+
Enum_Type = createSubclass(PyUnicode_Type, "zig_zon.Enum", "a zon enum wrapping str") orelse return false;
|
|
103
184
|
assert(0 <= c.PyObject_SetAttrString(mod, "Enum", Enum_Type));
|
|
104
185
|
|
|
105
186
|
BareEnum_Type = createSubclass(Enum_Type, "zig_zon.BareEnum", "a zon bare enum wrapping Enum") orelse return false;
|
|
@@ -126,51 +207,46 @@ const ParseOptions = struct {
|
|
|
126
207
|
bytes: BytesMode = .@"error",
|
|
127
208
|
};
|
|
128
209
|
|
|
129
|
-
fn parseBytesMode(mode_obj: *
|
|
130
|
-
|
|
210
|
+
fn parseBytesMode(mode_obj: *PyObject, mode: *BytesMode) bool {
|
|
211
|
+
const s = PyStr.init(mode_obj) orelse {
|
|
131
212
|
c.PyErr_SetString(c.PyExc_TypeError, "bytes must be a string");
|
|
132
213
|
return false;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (ptr == null) return false;
|
|
137
|
-
const s = ptr[0..@intCast(len)];
|
|
138
|
-
if (std.mem.eql(u8, s, "error")) {
|
|
214
|
+
};
|
|
215
|
+
defer s.deinit();
|
|
216
|
+
if (std.mem.eql(u8, s.slice, "error")) {
|
|
139
217
|
mode.* = .@"error";
|
|
140
|
-
} else if (std.mem.eql(u8, s, "auto")) {
|
|
218
|
+
} else if (std.mem.eql(u8, s.slice, "auto")) {
|
|
141
219
|
mode.* = .auto;
|
|
142
|
-
} else if (std.mem.eql(u8, s, "always")) {
|
|
220
|
+
} else if (std.mem.eql(u8, s.slice, "always")) {
|
|
143
221
|
mode.* = .always;
|
|
144
222
|
} else {
|
|
145
|
-
_ = c.PyErr_Format(c.PyExc_ValueError, "invalid bytes mode '%s' (expected 'error', 'auto', or 'always')", ptr);
|
|
223
|
+
_ = c.PyErr_Format(c.PyExc_ValueError, "invalid bytes mode '%s' (expected 'error', 'auto', or 'always')", s.slice.ptr);
|
|
146
224
|
return false;
|
|
147
225
|
}
|
|
148
226
|
return true;
|
|
149
227
|
}
|
|
150
228
|
|
|
151
229
|
fn parseParseKwargs(
|
|
152
|
-
kw: *
|
|
230
|
+
kw: *PyObject,
|
|
153
231
|
has_positional_mode: bool,
|
|
154
232
|
options: *ParseOptions,
|
|
155
233
|
) bool {
|
|
156
234
|
var pos: c.Py_ssize_t = 0;
|
|
157
|
-
var key: ?*
|
|
158
|
-
var val: ?*
|
|
235
|
+
var key: ?*PyObject = null;
|
|
236
|
+
var val: ?*PyObject = null;
|
|
159
237
|
while (c.PyDict_Next(kw, &pos, &key, &val) != 0) {
|
|
160
238
|
if (c.PyUnicode_Check(key.?) == 0) continue;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (kptr == null) return false;
|
|
164
|
-
const kname = kptr[0..@intCast(klen)];
|
|
239
|
+
const kstr = PyStr.init(key.?) orelse return false;
|
|
240
|
+
defer kstr.deinit();
|
|
165
241
|
|
|
166
|
-
if (std.mem.eql(u8,
|
|
242
|
+
if (std.mem.eql(u8, kstr.slice, "bytes")) {
|
|
167
243
|
if (has_positional_mode) {
|
|
168
244
|
c.PyErr_SetString(c.PyExc_TypeError, "parse() got multiple values for argument 'bytes'");
|
|
169
245
|
return false;
|
|
170
246
|
}
|
|
171
247
|
if (!parseBytesMode(val.?, &options.bytes)) return false;
|
|
172
248
|
} else {
|
|
173
|
-
_ = c.PyErr_Format(c.PyExc_TypeError, "got an unexpected keyword argument '%s'",
|
|
249
|
+
_ = c.PyErr_Format(c.PyExc_TypeError, "got an unexpected keyword argument '%s'", kstr.slice.ptr);
|
|
174
250
|
return false;
|
|
175
251
|
}
|
|
176
252
|
}
|
|
@@ -185,14 +261,12 @@ pub fn parse(self: [*]PyObject, args: [*]PyObject, kwargs: ?[*]PyObject) callcon
|
|
|
185
261
|
return null;
|
|
186
262
|
}
|
|
187
263
|
const zon_obj = c.PyTuple_GetItem(args, 0);
|
|
188
|
-
|
|
264
|
+
const zon_str = PyStr.init(zon_obj) orelse {
|
|
189
265
|
c.PyErr_SetString(c.PyExc_TypeError, "zon must be a string");
|
|
190
266
|
return null;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
if (zon_ptr == null) return null;
|
|
195
|
-
const zon_slice: [:0]const u8 = zon_ptr[0..@intCast(zon_len) :0];
|
|
267
|
+
};
|
|
268
|
+
defer zon_str.deinit();
|
|
269
|
+
const zon_slice: [:0]const u8 = zon_str.slice;
|
|
196
270
|
|
|
197
271
|
var options: ParseOptions = .{};
|
|
198
272
|
if (arg_count == 2) {
|
|
@@ -208,7 +282,7 @@ pub fn parse(self: [*]PyObject, args: [*]PyObject, kwargs: ?[*]PyObject) callcon
|
|
|
208
282
|
defer arena_instance.deinit();
|
|
209
283
|
const arena = arena_instance.allocator();
|
|
210
284
|
|
|
211
|
-
const ast = Ast.parse(arena, zon_slice, .zon) catch |err| {
|
|
285
|
+
const ast = Ast.parse(arena, zon_slice, .{ .mode = .zon }) catch |err| {
|
|
212
286
|
const errstr = std.fmt.allocPrintSentinel(
|
|
213
287
|
arena,
|
|
214
288
|
"zig-zon ast error: {s}",
|
|
@@ -237,7 +311,7 @@ pub fn parse(self: [*]PyObject, args: [*]PyObject, kwargs: ?[*]PyObject) callcon
|
|
|
237
311
|
|
|
238
312
|
const main_node_index = ast.nodeData(.root).node;
|
|
239
313
|
|
|
240
|
-
return parseZonAst(arena, ast, main_node_index, options) catch |err| {
|
|
314
|
+
return parseZonAst(arena, &ast, main_node_index, options) catch |err| {
|
|
241
315
|
if (c.PyErr_Occurred() == null) {
|
|
242
316
|
const errstr = std.fmt.allocPrintSentinel(
|
|
243
317
|
arena,
|
|
@@ -251,7 +325,7 @@ pub fn parse(self: [*]PyObject, args: [*]PyObject, kwargs: ?[*]PyObject) callcon
|
|
|
251
325
|
};
|
|
252
326
|
}
|
|
253
327
|
|
|
254
|
-
inline fn PyNone() [*c]
|
|
328
|
+
inline fn PyNone() [*c]PyObject {
|
|
255
329
|
return c.Py_BuildValue("");
|
|
256
330
|
}
|
|
257
331
|
|
|
@@ -265,7 +339,7 @@ const ZonError = error{
|
|
|
265
339
|
WriteFailed,
|
|
266
340
|
} || IdentifierError || NumberLiteralError;
|
|
267
341
|
|
|
268
|
-
fn parseZonAst(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: ParseOptions) ZonError!*
|
|
342
|
+
fn parseZonAst(arena: Allocator, tree: *const Ast, node: Ast.Node.Index, options: ParseOptions) ZonError!*PyObject {
|
|
269
343
|
const node_tag = tree.nodeTag(node);
|
|
270
344
|
switch (node_tag) {
|
|
271
345
|
.struct_init_comma,
|
|
@@ -298,12 +372,12 @@ fn parseZonAst(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Parse
|
|
|
298
372
|
);
|
|
299
373
|
const base_str = c.PyUnicode_FromStringAndSize(enum_parsed_name.ptr, @intCast(enum_parsed_name.len)) orelse return error.InvalidLiteral;
|
|
300
374
|
defer c.Py_DecRef(base_str);
|
|
301
|
-
return
|
|
375
|
+
return PyObject_CallOneArg(QuotedEnum_Type, base_str) orelse error.InvalidLiteral;
|
|
302
376
|
}
|
|
303
377
|
|
|
304
378
|
const base_str = c.PyUnicode_FromStringAndSize(enum_name.ptr, @intCast(enum_name.len)) orelse return error.InvalidLiteral;
|
|
305
379
|
defer c.Py_DecRef(base_str);
|
|
306
|
-
return
|
|
380
|
+
return PyObject_CallOneArg(BareEnum_Type, base_str) orelse error.InvalidLiteral;
|
|
307
381
|
},
|
|
308
382
|
.string_literal, .multiline_string_literal => {
|
|
309
383
|
var buf = std.ArrayListUnmanaged(u8).empty;
|
|
@@ -319,23 +393,23 @@ fn parseZonAst(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Parse
|
|
|
319
393
|
.always => {
|
|
320
394
|
const base_bytes = c.PyBytes_FromStringAndSize(buf.items.ptr, @intCast(buf.items.len)) orelse return error.PyErr;
|
|
321
395
|
defer c.Py_DecRef(base_bytes);
|
|
322
|
-
return
|
|
396
|
+
return PyObject_CallOneArg(ByteString_Type, base_bytes) orelse error.PyErr;
|
|
323
397
|
},
|
|
324
398
|
.auto => {
|
|
325
399
|
if (std.unicode.utf8ValidateSlice(buf.items)) {
|
|
326
400
|
const base_str = c.PyUnicode_FromStringAndSize(buf.items.ptr, @intCast(buf.items.len)) orelse return error.PyErr;
|
|
327
401
|
defer c.Py_DecRef(base_str);
|
|
328
|
-
return
|
|
402
|
+
return PyObject_CallOneArg(String_Type, base_str) orelse error.PyErr;
|
|
329
403
|
} else {
|
|
330
404
|
const base_bytes = c.PyBytes_FromStringAndSize(buf.items.ptr, @intCast(buf.items.len)) orelse return error.PyErr;
|
|
331
405
|
defer c.Py_DecRef(base_bytes);
|
|
332
|
-
return
|
|
406
|
+
return PyObject_CallOneArg(ByteString_Type, base_bytes) orelse error.PyErr;
|
|
333
407
|
}
|
|
334
408
|
},
|
|
335
409
|
.@"error" => {
|
|
336
410
|
const base_str = c.PyUnicode_DecodeUTF8(buf.items.ptr, @intCast(buf.items.len), "strict") orelse return error.PyErr;
|
|
337
411
|
defer c.Py_DecRef(base_str);
|
|
338
|
-
return
|
|
412
|
+
return PyObject_CallOneArg(String_Type, base_str) orelse error.PyErr;
|
|
339
413
|
},
|
|
340
414
|
}
|
|
341
415
|
},
|
|
@@ -371,34 +445,44 @@ fn parseZonAst(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Parse
|
|
|
371
445
|
}
|
|
372
446
|
|
|
373
447
|
const NumberLiteralError = error{
|
|
374
|
-
BigIntNotImplemented,
|
|
375
448
|
InvalidCharacter,
|
|
376
449
|
HexFloatNotImplemented,
|
|
377
450
|
NumberLiteralFail,
|
|
451
|
+
OutOfMemory,
|
|
378
452
|
};
|
|
379
453
|
|
|
380
|
-
fn parseNumberLiteral(arena: Allocator, tree: Ast, node: Ast.Node.Index, negated: bool) NumberLiteralError!*
|
|
381
|
-
_ = arena;
|
|
454
|
+
fn parseNumberLiteral(arena: Allocator, tree: *const Ast, node: Ast.Node.Index, negated: bool) NumberLiteralError!*PyObject {
|
|
382
455
|
const num_token = tree.lastToken(node);
|
|
383
456
|
const token_bytes = tree.tokenSlice(num_token);
|
|
457
|
+
const is_hex = std.mem.startsWith(u8, token_bytes, "0x") or std.mem.startsWith(u8, token_bytes, "0X");
|
|
458
|
+
const cls = if (is_hex) Hex_Type else Integer_Type;
|
|
384
459
|
const parsed = std.zig.parseNumberLiteral(token_bytes);
|
|
385
460
|
switch (parsed) {
|
|
386
461
|
.int => |n| {
|
|
462
|
+
if (negated and n > 9223372036854775808) {
|
|
463
|
+
const s = try std.fmt.allocPrintSentinel(arena, "-{s}", .{token_bytes}, 0);
|
|
464
|
+
const base_int = c.PyLong_FromString(s.ptr, null, 0) orelse return error.NumberLiteralFail;
|
|
465
|
+
defer c.Py_DecRef(base_int);
|
|
466
|
+
return PyObject_CallOneArg(cls, base_int) orelse error.NumberLiteralFail;
|
|
467
|
+
}
|
|
387
468
|
const factor: i64 = if (negated) -1 else 1;
|
|
388
469
|
const neglim = 9223372036854775807; // 2**63 - 1
|
|
389
|
-
const base_int: *
|
|
470
|
+
const base_int: *PyObject = if (n <= neglim)
|
|
390
471
|
c.PyLong_FromSsize_t(factor * @as(i64, @intCast(n))) orelse return error.NumberLiteralFail
|
|
391
|
-
else
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
472
|
+
else if (negated and n == 9223372036854775808)
|
|
473
|
+
c.PyLong_FromLongLong(std.math.minInt(i64)) orelse return error.NumberLiteralFail
|
|
474
|
+
else
|
|
475
|
+
c.PyLong_FromSize_t(n) orelse return error.NumberLiteralFail;
|
|
395
476
|
defer c.Py_DecRef(base_int);
|
|
396
477
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
478
|
+
return PyObject_CallOneArg(cls, base_int) orelse error.NumberLiteralFail;
|
|
479
|
+
},
|
|
480
|
+
.big_int => {
|
|
481
|
+
const s = try std.fmt.allocPrintSentinel(arena, "{s}{s}", .{ if (negated) "-" else "", token_bytes }, 0);
|
|
482
|
+
const base_int = c.PyLong_FromString(s.ptr, null, 0) orelse return error.NumberLiteralFail;
|
|
483
|
+
defer c.Py_DecRef(base_int);
|
|
484
|
+
return PyObject_CallOneArg(cls, base_int) orelse error.NumberLiteralFail;
|
|
400
485
|
},
|
|
401
|
-
.big_int => return error.BigIntNotImplemented,
|
|
402
486
|
.float => |n| {
|
|
403
487
|
const float_payload = switch (n) {
|
|
404
488
|
.decimal => token_bytes,
|
|
@@ -409,7 +493,7 @@ fn parseNumberLiteral(arena: Allocator, tree: Ast, node: Ast.Node.Index, negated
|
|
|
409
493
|
|
|
410
494
|
const base_float = c.PyFloat_FromDouble(factor * float) orelse return error.NumberLiteralFail;
|
|
411
495
|
defer c.Py_DecRef(base_float);
|
|
412
|
-
return
|
|
496
|
+
return PyObject_CallOneArg(Float_Type, base_float) orelse error.NumberLiteralFail;
|
|
413
497
|
},
|
|
414
498
|
.failure => |err| {
|
|
415
499
|
std.log.err("number literal: {} bytes: {s}", .{ err, token_bytes });
|
|
@@ -418,11 +502,11 @@ fn parseNumberLiteral(arena: Allocator, tree: Ast, node: Ast.Node.Index, negated
|
|
|
418
502
|
}
|
|
419
503
|
}
|
|
420
504
|
|
|
421
|
-
fn parseZonStruct(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: ParseOptions) ZonError!*
|
|
505
|
+
fn parseZonStruct(arena: Allocator, tree: *const Ast, node: Ast.Node.Index, options: ParseOptions) ZonError!*PyObject {
|
|
422
506
|
var buf: [2]Ast.Node.Index = undefined;
|
|
423
507
|
const struct_init = tree.fullStructInit(&buf, node) orelse return error.StructExpected;
|
|
424
508
|
|
|
425
|
-
const ret =
|
|
509
|
+
const ret = PyObject_CallNoArgs(Struct_Type) orelse return error.StructExpected;
|
|
426
510
|
errdefer c.Py_DecRef(ret);
|
|
427
511
|
|
|
428
512
|
for (struct_init.ast.fields) |field_init| {
|
|
@@ -436,7 +520,7 @@ fn parseZonStruct(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Pa
|
|
|
436
520
|
return ret;
|
|
437
521
|
}
|
|
438
522
|
|
|
439
|
-
fn parseZonArray(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: ParseOptions) !*
|
|
523
|
+
fn parseZonArray(arena: Allocator, tree: *const Ast, node: Ast.Node.Index, options: ParseOptions) !*PyObject {
|
|
440
524
|
// ret.* = c.Py_BuildValue("l", @as(u64, 1));
|
|
441
525
|
var buf: [2]Ast.Node.Index = undefined;
|
|
442
526
|
const array_init = tree.fullArrayInit(&buf, node) orelse {
|
|
@@ -445,7 +529,7 @@ fn parseZonArray(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Par
|
|
|
445
529
|
return error.ZonArrayInitFailed;
|
|
446
530
|
};
|
|
447
531
|
|
|
448
|
-
const ret =
|
|
532
|
+
const ret = PyObject_CallNoArgs(Tuple_Type) orelse return error.ZonArrayInitFailed;
|
|
449
533
|
errdefer c.Py_DecRef(ret);
|
|
450
534
|
|
|
451
535
|
for (array_init.ast.elements) |elem_node| {
|
|
@@ -458,7 +542,7 @@ fn parseZonArray(arena: Allocator, tree: Ast, node: Ast.Node.Index, options: Par
|
|
|
458
542
|
|
|
459
543
|
const IdentifierError = error{OutOfMemory} || StringLiteralError;
|
|
460
544
|
|
|
461
|
-
fn identifierTokenString(arena: Allocator, ast: Ast, token: Ast.TokenIndex) IdentifierError![:0]const u8 {
|
|
545
|
+
fn identifierTokenString(arena: Allocator, ast: *const Ast, token: Ast.TokenIndex) IdentifierError![:0]const u8 {
|
|
462
546
|
assert(ast.tokenTag(token) == .identifier);
|
|
463
547
|
const ident_name = ast.tokenSlice(token);
|
|
464
548
|
if (!std.mem.startsWith(u8, ident_name, "@")) {
|
|
@@ -509,12 +593,18 @@ const IntegerMode = enum {
|
|
|
509
593
|
const StringifyOptions = struct {
|
|
510
594
|
integer_mode: IntegerMode = .auto,
|
|
511
595
|
dict_as_tuples: bool = false,
|
|
512
|
-
encoding: [
|
|
596
|
+
encoding: [64:0]u8 = initEncoding("utf-8"),
|
|
513
597
|
whitespace: bool = true,
|
|
598
|
+
|
|
599
|
+
fn initEncoding(comptime default: []const u8) [64:0]u8 {
|
|
600
|
+
var buf: [64:0]u8 = @splat(0);
|
|
601
|
+
@memcpy(buf[0..default.len], default);
|
|
602
|
+
return buf;
|
|
603
|
+
}
|
|
514
604
|
};
|
|
515
605
|
|
|
516
606
|
inline fn isNone(obj: *c.PyObject) bool {
|
|
517
|
-
return obj == @as(*
|
|
607
|
+
return obj == @as(*PyObject, @ptrCast(&c._Py_NoneStruct));
|
|
518
608
|
}
|
|
519
609
|
|
|
520
610
|
fn isNamedTuple(obj: *c.PyObject) bool {
|
|
@@ -524,7 +614,7 @@ fn isNamedTuple(obj: *c.PyObject) bool {
|
|
|
524
614
|
|
|
525
615
|
fn serializeObject(
|
|
526
616
|
serializer: *Serializer,
|
|
527
|
-
obj: *
|
|
617
|
+
obj: *PyObject,
|
|
528
618
|
options: StringifyOptions,
|
|
529
619
|
) !void {
|
|
530
620
|
if (c.Py_EnterRecursiveCall(" while serializing to ZON") != 0) return error.PyErr;
|
|
@@ -535,7 +625,7 @@ fn serializeObject(
|
|
|
535
625
|
return;
|
|
536
626
|
}
|
|
537
627
|
|
|
538
|
-
if (
|
|
628
|
+
if (PyBool_Check(obj)) {
|
|
539
629
|
if (c.PyObject_IsTrue(obj) != 0) {
|
|
540
630
|
try serializer.writer.writeAll("true");
|
|
541
631
|
} else {
|
|
@@ -564,22 +654,20 @@ fn serializeObject(
|
|
|
564
654
|
if (use_hex) {
|
|
565
655
|
const hex_str_obj = c.PyNumber_ToBase(obj, 16) orelse return error.PyErr;
|
|
566
656
|
defer c.Py_DecRef(hex_str_obj);
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
try serializer.writer.writeAll(ptr[0..@intCast(len)]);
|
|
657
|
+
const s = PyStr.init(hex_str_obj) orelse return error.PyErr;
|
|
658
|
+
defer s.deinit();
|
|
659
|
+
try serializer.writer.writeAll(s.slice);
|
|
571
660
|
} else {
|
|
572
661
|
const str_obj = c.PyObject_Str(obj) orelse return error.PyErr;
|
|
573
662
|
defer c.Py_DecRef(str_obj);
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
try serializer.writer.writeAll(ptr[0..@intCast(len)]);
|
|
663
|
+
const s = PyStr.init(str_obj) orelse return error.PyErr;
|
|
664
|
+
defer s.deinit();
|
|
665
|
+
try serializer.writer.writeAll(s.slice);
|
|
578
666
|
}
|
|
579
667
|
return;
|
|
580
668
|
}
|
|
581
669
|
|
|
582
|
-
if (
|
|
670
|
+
if (PyFloat_Check(obj)) {
|
|
583
671
|
const d = c.PyFloat_AsDouble(obj);
|
|
584
672
|
if (std.math.isNan(d)) {
|
|
585
673
|
try serializer.writer.writeAll("nan");
|
|
@@ -603,56 +691,46 @@ fn serializeObject(
|
|
|
603
691
|
}
|
|
604
692
|
|
|
605
693
|
if (c.PyObject_IsInstance(obj, BareEnum_Type) == 1) {
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
if (ptr == null) return error.PyErr;
|
|
609
|
-
const s = ptr[0..@intCast(len)];
|
|
694
|
+
const s = PyStr.init(obj) orelse return error.PyErr;
|
|
695
|
+
defer s.deinit();
|
|
610
696
|
try serializer.writer.writeByte('.');
|
|
611
|
-
try serializer.writer.writeAll(s);
|
|
697
|
+
try serializer.writer.writeAll(s.slice);
|
|
612
698
|
return;
|
|
613
699
|
}
|
|
614
700
|
|
|
615
701
|
if (c.PyObject_IsInstance(obj, QuotedEnum_Type) == 1) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
if (ptr == null) return error.PyErr;
|
|
619
|
-
const s = ptr[0..@intCast(len)];
|
|
702
|
+
const s = PyStr.init(obj) orelse return error.PyErr;
|
|
703
|
+
defer s.deinit();
|
|
620
704
|
try serializer.writer.writeAll(".@\"");
|
|
621
|
-
try serializer.writer.print("{f}", .{std.zig.fmtString(s)});
|
|
705
|
+
try serializer.writer.print("{f}", .{std.zig.fmtString(s.slice)});
|
|
622
706
|
try serializer.writer.writeByte('"');
|
|
623
707
|
return;
|
|
624
708
|
}
|
|
625
709
|
|
|
626
710
|
if (c.PyObject_IsInstance(obj, Enum_Type) == 1) {
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
const s = ptr[0..@intCast(len)];
|
|
631
|
-
try serializer.ident(s);
|
|
711
|
+
const s = PyStr.init(obj) orelse return error.PyErr;
|
|
712
|
+
defer s.deinit();
|
|
713
|
+
try serializer.ident(s.slice);
|
|
632
714
|
return;
|
|
633
715
|
}
|
|
634
716
|
|
|
635
717
|
if (c.PyUnicode_Check(obj) != 0) {
|
|
636
|
-
const enc = std.mem.sliceTo(options.encoding, 0);
|
|
718
|
+
const enc = std.mem.sliceTo(&options.encoding, 0);
|
|
637
719
|
const is_utf8 = std.mem.eql(u8, enc, "utf-8") or
|
|
638
720
|
std.mem.eql(u8, enc, "utf_8") or
|
|
639
721
|
std.mem.eql(u8, enc, "UTF-8");
|
|
640
722
|
if (is_utf8) {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const s = ptr[0..@intCast(len)];
|
|
645
|
-
try serializer.string(s);
|
|
723
|
+
const s = PyStr.init(obj) orelse return error.PyErr;
|
|
724
|
+
defer s.deinit();
|
|
725
|
+
try serializer.string(s.slice, .{});
|
|
646
726
|
} else {
|
|
647
|
-
const encoded = c.PyUnicode_AsEncodedString(obj, options.encoding, "strict") orelse return error.PyErr;
|
|
727
|
+
const encoded = c.PyUnicode_AsEncodedString(obj, &options.encoding, "strict") orelse return error.PyErr;
|
|
648
728
|
defer c.Py_DecRef(encoded);
|
|
649
|
-
const decoded = c.PyUnicode_FromEncodedObject(encoded, options.encoding, "strict") orelse return error.PyErr;
|
|
729
|
+
const decoded = c.PyUnicode_FromEncodedObject(encoded, &options.encoding, "strict") orelse return error.PyErr;
|
|
650
730
|
defer c.Py_DecRef(decoded);
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const s = ptr[0..@intCast(len)];
|
|
655
|
-
try serializer.string(s);
|
|
731
|
+
const s = PyStr.init(decoded) orelse return error.PyErr;
|
|
732
|
+
defer s.deinit();
|
|
733
|
+
try serializer.string(s.slice, .{});
|
|
656
734
|
}
|
|
657
735
|
return;
|
|
658
736
|
}
|
|
@@ -662,18 +740,16 @@ fn serializeObject(
|
|
|
662
740
|
var ptr: [*c]u8 = undefined;
|
|
663
741
|
if (c.PyBytes_AsStringAndSize(obj, &ptr, &len) < 0) return error.PyErr;
|
|
664
742
|
const s: []const u8 = @as([*]const u8, @ptrCast(ptr))[0..@intCast(len)];
|
|
665
|
-
try serializer.string(s);
|
|
743
|
+
try serializer.string(s, .{});
|
|
666
744
|
return;
|
|
667
745
|
}
|
|
668
746
|
|
|
669
|
-
if (c.PyBytes_Check(obj) != 0 or
|
|
670
|
-
const text = c.PyUnicode_FromEncodedObject(obj, options.encoding, "strict") orelse return error.PyErr;
|
|
747
|
+
if (c.PyBytes_Check(obj) != 0 or PyByteArray_Check(obj)) {
|
|
748
|
+
const text = c.PyUnicode_FromEncodedObject(obj, &options.encoding, "strict") orelse return error.PyErr;
|
|
671
749
|
defer c.Py_DecRef(text);
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
const s = ptr[0..@intCast(len)];
|
|
676
|
-
try serializer.string(s);
|
|
750
|
+
const s = PyStr.init(text) orelse return error.PyErr;
|
|
751
|
+
defer s.deinit();
|
|
752
|
+
try serializer.string(s.slice, .{});
|
|
677
753
|
return;
|
|
678
754
|
}
|
|
679
755
|
|
|
@@ -686,12 +762,10 @@ fn serializeObject(
|
|
|
686
762
|
});
|
|
687
763
|
for (0..@intCast(count)) |i| {
|
|
688
764
|
const fname_obj = c.PyTuple_GetItem(fields, @intCast(i));
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
if (kptr == null) return error.PyErr;
|
|
692
|
-
const kname = kptr[0..@intCast(klen)];
|
|
765
|
+
const fname = PyStr.init(fname_obj) orelse return error.PyErr;
|
|
766
|
+
defer fname.deinit();
|
|
693
767
|
const fval_obj = c.PyTuple_GetItem(obj, @intCast(i));
|
|
694
|
-
try struct_container.fieldPrefix(
|
|
768
|
+
try struct_container.fieldPrefix(fname.slice);
|
|
695
769
|
try serializeObject(serializer, fval_obj, options);
|
|
696
770
|
}
|
|
697
771
|
try struct_container.end();
|
|
@@ -705,8 +779,8 @@ fn serializeObject(
|
|
|
705
779
|
.whitespace_style = .{ .fields = @intCast(dict_len) },
|
|
706
780
|
});
|
|
707
781
|
var pos: c.Py_ssize_t = 0;
|
|
708
|
-
var key: ?*
|
|
709
|
-
var val: ?*
|
|
782
|
+
var key: ?*PyObject = null;
|
|
783
|
+
var val: ?*PyObject = null;
|
|
710
784
|
while (c.PyDict_Next(obj, &pos, &key, &val) != 0) {
|
|
711
785
|
try outer.fieldPrefix();
|
|
712
786
|
var inner = try serializer.beginTuple(.{
|
|
@@ -724,18 +798,16 @@ fn serializeObject(
|
|
|
724
798
|
.whitespace_style = .{ .fields = @intCast(dict_len) },
|
|
725
799
|
});
|
|
726
800
|
var pos: c.Py_ssize_t = 0;
|
|
727
|
-
var key: ?*
|
|
728
|
-
var val: ?*
|
|
801
|
+
var key: ?*PyObject = null;
|
|
802
|
+
var val: ?*PyObject = null;
|
|
729
803
|
while (c.PyDict_Next(obj, &pos, &key, &val) != 0) {
|
|
730
804
|
if (c.PyUnicode_Check(key.?) == 0) {
|
|
731
805
|
c.PyErr_SetString(c.PyExc_TypeError, "dict keys must be strings when serialized as a struct; use dict_as_tuples=True");
|
|
732
806
|
return error.PyErr;
|
|
733
807
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
const kname = kptr[0..@intCast(klen)];
|
|
738
|
-
try struct_container.fieldPrefix(kname);
|
|
808
|
+
const kstr = PyStr.init(key.?) orelse return error.PyErr;
|
|
809
|
+
defer kstr.deinit();
|
|
810
|
+
try struct_container.fieldPrefix(kstr.slice);
|
|
739
811
|
try serializeObject(serializer, val.?, options);
|
|
740
812
|
}
|
|
741
813
|
try struct_container.end();
|
|
@@ -758,7 +830,7 @@ fn serializeObject(
|
|
|
758
830
|
return;
|
|
759
831
|
}
|
|
760
832
|
|
|
761
|
-
if (
|
|
833
|
+
if (PyAnySet_Check(obj)) {
|
|
762
834
|
const count = c.PySet_Size(obj);
|
|
763
835
|
var container = try serializer.beginTuple(.{
|
|
764
836
|
.whitespace_style = .{ .fields = @intCast(count) },
|
|
@@ -775,67 +847,71 @@ fn serializeObject(
|
|
|
775
847
|
return;
|
|
776
848
|
}
|
|
777
849
|
|
|
778
|
-
|
|
850
|
+
if (PyType_GetName(obj)) |type_name| {
|
|
851
|
+
defer c.Py_DecRef(type_name);
|
|
852
|
+
_ = c.PyErr_Format(c.PyExc_TypeError, "Object of type '%U' is not ZON serializable", type_name);
|
|
853
|
+
} else {
|
|
854
|
+
c.PyErr_SetString(c.PyExc_TypeError, "Object is not ZON serializable");
|
|
855
|
+
}
|
|
779
856
|
return error.PyErr;
|
|
780
857
|
}
|
|
781
858
|
|
|
782
859
|
fn parseStringifyKwargs(
|
|
783
|
-
kw: *
|
|
860
|
+
kw: *PyObject,
|
|
784
861
|
options: *StringifyOptions,
|
|
785
862
|
) bool {
|
|
786
863
|
var pos: c.Py_ssize_t = 0;
|
|
787
|
-
var key: ?*
|
|
788
|
-
var val: ?*
|
|
864
|
+
var key: ?*PyObject = null;
|
|
865
|
+
var val: ?*PyObject = null;
|
|
789
866
|
while (c.PyDict_Next(kw, &pos, &key, &val) != 0) {
|
|
790
867
|
if (c.PyUnicode_Check(key.?) == 0) continue;
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
if (kptr == null) return false;
|
|
794
|
-
const kname = kptr[0..@intCast(klen)];
|
|
868
|
+
const kstr = PyStr.init(key.?) orelse return false;
|
|
869
|
+
defer kstr.deinit();
|
|
795
870
|
|
|
796
|
-
if (std.mem.eql(u8,
|
|
797
|
-
|
|
871
|
+
if (std.mem.eql(u8, kstr.slice, "integer_mode")) {
|
|
872
|
+
const vstr = PyStr.init(val.?) orelse {
|
|
798
873
|
c.PyErr_SetString(c.PyExc_TypeError, "integer_mode must be a string");
|
|
799
874
|
return false;
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
if (vptr == null) return false;
|
|
804
|
-
const vs = vptr[0..@intCast(vlen)];
|
|
805
|
-
if (std.mem.eql(u8, vs, "auto")) {
|
|
875
|
+
};
|
|
876
|
+
defer vstr.deinit();
|
|
877
|
+
if (std.mem.eql(u8, vstr.slice, "auto")) {
|
|
806
878
|
options.integer_mode = .auto;
|
|
807
|
-
} else if (std.mem.eql(u8,
|
|
879
|
+
} else if (std.mem.eql(u8, vstr.slice, "int")) {
|
|
808
880
|
options.integer_mode = .int;
|
|
809
|
-
} else if (std.mem.eql(u8,
|
|
881
|
+
} else if (std.mem.eql(u8, vstr.slice, "hex")) {
|
|
810
882
|
options.integer_mode = .hex;
|
|
811
|
-
} else if (std.mem.eql(u8,
|
|
883
|
+
} else if (std.mem.eql(u8, vstr.slice, "fallback_int")) {
|
|
812
884
|
options.integer_mode = .fallback_int;
|
|
813
|
-
} else if (std.mem.eql(u8,
|
|
885
|
+
} else if (std.mem.eql(u8, vstr.slice, "fallback_hex")) {
|
|
814
886
|
options.integer_mode = .fallback_hex;
|
|
815
|
-
} else if (std.mem.eql(u8,
|
|
887
|
+
} else if (std.mem.eql(u8, vstr.slice, "error")) {
|
|
816
888
|
options.integer_mode = .@"error";
|
|
817
889
|
} else {
|
|
818
|
-
_ = c.PyErr_Format(c.PyExc_ValueError, "invalid integer_mode '%s' (expected 'auto', 'int', 'hex', 'fallback_int', 'fallback_hex', or 'error')",
|
|
890
|
+
_ = c.PyErr_Format(c.PyExc_ValueError, "invalid integer_mode '%s' (expected 'auto', 'int', 'hex', 'fallback_int', 'fallback_hex', or 'error')", vstr.slice.ptr);
|
|
819
891
|
return false;
|
|
820
892
|
}
|
|
821
|
-
} else if (std.mem.eql(u8,
|
|
893
|
+
} else if (std.mem.eql(u8, kstr.slice, "dict_as_tuples")) {
|
|
822
894
|
const is_true = c.PyObject_IsTrue(val.?);
|
|
823
895
|
if (is_true < 0) return false;
|
|
824
896
|
options.dict_as_tuples = is_true != 0;
|
|
825
|
-
} else if (std.mem.eql(u8,
|
|
826
|
-
|
|
897
|
+
} else if (std.mem.eql(u8, kstr.slice, "encoding")) {
|
|
898
|
+
const estr = PyStr.init(val.?) orelse {
|
|
827
899
|
c.PyErr_SetString(c.PyExc_TypeError, "encoding must be a string");
|
|
828
900
|
return false;
|
|
901
|
+
};
|
|
902
|
+
defer estr.deinit();
|
|
903
|
+
if (estr.slice.len >= options.encoding.len) {
|
|
904
|
+
c.PyErr_SetString(c.PyExc_ValueError, "encoding name too long");
|
|
905
|
+
return false;
|
|
829
906
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
} else if (std.mem.eql(u8, kname, "whitespace")) {
|
|
907
|
+
@memset(&options.encoding, 0);
|
|
908
|
+
@memcpy(options.encoding[0..estr.slice.len], estr.slice);
|
|
909
|
+
} else if (std.mem.eql(u8, kstr.slice, "whitespace")) {
|
|
834
910
|
const is_true = c.PyObject_IsTrue(val.?);
|
|
835
911
|
if (is_true < 0) return false;
|
|
836
912
|
options.whitespace = is_true != 0;
|
|
837
913
|
} else {
|
|
838
|
-
_ = c.PyErr_Format(c.PyExc_TypeError, "got an unexpected keyword argument '%s'",
|
|
914
|
+
_ = c.PyErr_Format(c.PyExc_TypeError, "got an unexpected keyword argument '%s'", kstr.slice.ptr);
|
|
839
915
|
return false;
|
|
840
916
|
}
|
|
841
917
|
}
|
|
@@ -843,9 +919,9 @@ fn parseStringifyKwargs(
|
|
|
843
919
|
}
|
|
844
920
|
|
|
845
921
|
fn serializeToPyStr(
|
|
846
|
-
val_obj: *
|
|
922
|
+
val_obj: *PyObject,
|
|
847
923
|
options: StringifyOptions,
|
|
848
|
-
) [*c]
|
|
924
|
+
) [*c]PyObject {
|
|
849
925
|
var arena_instance = std.heap.ArenaAllocator.init(cpython.py_allocator);
|
|
850
926
|
defer arena_instance.deinit();
|
|
851
927
|
const arena = arena_instance.allocator();
|
|
@@ -902,7 +978,7 @@ pub fn py_dump(self: [*]PyObject, args: [*]PyObject, kwargs: ?[*]PyObject) callc
|
|
|
902
978
|
return null;
|
|
903
979
|
}
|
|
904
980
|
const val_obj = c.PyTuple_GetItem(args, 0);
|
|
905
|
-
const file_obj: *
|
|
981
|
+
const file_obj: *PyObject = c.PyTuple_GetItem(args, 1);
|
|
906
982
|
|
|
907
983
|
var options: StringifyOptions = .{};
|
|
908
984
|
if (kwargs) |kw| {
|
|
@@ -10,7 +10,7 @@ A complete runnable example is available in the [`example/`](example/) directory
|
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
|
-
Add `zig-python-sdist` to your project's `build.zig.zon` (requires Zig >= 0.
|
|
13
|
+
Add `zig-python-sdist` to your project's `build.zig.zon` (requires Zig >= 0.17.1):
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
zig fetch --save git+https://codeberg.org/dasimmet/zig-python-sdist
|
|
@@ -273,3 +273,5 @@ zig build test-pip-install
|
|
|
273
273
|
- [Deinventing the Wheel - Loris Cro](https://www.youtube.com/watch?v=HPmefnqirHk)
|
|
274
274
|
- [Python C API - Limited API](https://docs.python.org/3/c-api/stable.html)
|
|
275
275
|
- [PEP 384 – Defining a Stable ABI](https://peps.python.org/pep-0384/)
|
|
276
|
+
- [Zig Zon Parser library](https://codeberg.org/dasimmet/python-zig-zon)
|
|
277
|
+
- [Minisign Python bindings](https://codeberg.org/dasimmet/minizign)
|
|
@@ -123,7 +123,6 @@ pub fn build(b: *Build) void {
|
|
|
123
123
|
},
|
|
124
124
|
},
|
|
125
125
|
.install_requires = &.{"ruamel.yaml.clibz"},
|
|
126
|
-
.zig_version = std.SemanticVersion.parse("0.16.0") catch unreachable,
|
|
127
126
|
.compression = .best,
|
|
128
127
|
});
|
|
129
128
|
const example_cpython_install = example_cpython_sdist.install();
|
|
@@ -210,7 +209,6 @@ pub fn build(b: *Build) void {
|
|
|
210
209
|
.directories = &.{
|
|
211
210
|
.{ .src = b.path("src/build"), .dest = "src/build" },
|
|
212
211
|
},
|
|
213
|
-
.zig_version = std.SemanticVersion.parse("0.16.0") catch unreachable,
|
|
214
212
|
.compression = .best,
|
|
215
213
|
});
|
|
216
214
|
const example_ctypes_install = example_ctypes_sdist.install();
|
|
@@ -11,7 +11,7 @@ pub const CPython = @This();
|
|
|
11
11
|
|
|
12
12
|
pub const LinkOptions = struct {
|
|
13
13
|
target: Build.ResolvedTarget,
|
|
14
|
-
optimize: std.
|
|
14
|
+
optimize: std.lang.Optimize,
|
|
15
15
|
version: ?[]const u8 = null,
|
|
16
16
|
header_file: ?LazyPath = null,
|
|
17
17
|
// pass in the python executable for finding headers. use "python" from PATH if omitted.
|
|
@@ -76,9 +76,7 @@ pub fn wrapMain(wrap_main: anytype, comptime options: WrapMainOptions) fn ([*]c.
|
|
|
76
76
|
defer arena_impl.deinit();
|
|
77
77
|
const arena = arena_impl.allocator();
|
|
78
78
|
|
|
79
|
-
var gpa_impl: std.heap.
|
|
80
|
-
.backing_allocator = py_allocator,
|
|
81
|
-
};
|
|
79
|
+
var gpa_impl: std.heap.SafeAllocator = .init(py_allocator, .{});
|
|
82
80
|
defer _ = gpa_impl.deinit();
|
|
83
81
|
const gpa = gpa_impl.allocator();
|
|
84
82
|
|
|
@@ -322,7 +320,7 @@ pub fn getZigBuitin() *c.PyObject {
|
|
|
322
320
|
assert(0 <= c.PyDict_SetItemString(cpu_dict, "features", features));
|
|
323
321
|
|
|
324
322
|
inline for (comptime builtin.target.cpu.arch.allFeaturesList()) |feature| {
|
|
325
|
-
if (builtin.target.cpu.has(builtin.target.cpu.arch.family(), @
|
|
323
|
+
if (builtin.target.cpu.has(builtin.target.cpu.arch.family(), @fromBackingInt(@intCast(feature.index)))) {
|
|
326
324
|
const feature_str = c.Py_BuildValue("s#", feature.name.ptr, feature.name.len);
|
|
327
325
|
defer c.Py_DecRef(feature_str);
|
|
328
326
|
assert(c.PyList_Append(features, feature_str) >= 0);
|
zig-zon-0.3.1/setup.zig.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"name":"zig_zon","version":{"major":0,"minor":3,"patch":1},"zig_version":{"major":0,"minor":16,"patch":0},"build":{"args":["--fork=zig-pkg/zig_python_sdist-0.0.8","bdist-wheel","-Doptimize=ReleaseSmall"],"requires":[],"packages":[],"copy_patterns":[{"pattern":"zig-out/lib/*","package_dir":false}]},"py_limited_api":"cp39","has_ext_modules":true}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|