Skip to content

Commit 6a01dfc

Browse files
committed
Make type checks use *const
1 parent ff875a4 commit 6a01dfc

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

py.zig

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,13 @@ pub inline fn ObjectProtocol(comptime T: type) type {
352352
return @ptrCast(c.Py_NewRef(@ptrCast(self)));
353353
}
354354

355-
// Returns a new reference to the type
356-
pub inline fn typenewref(self: *T) *Type {
357-
return @ptrCast(c.PyObject_Type(@ptrCast(self)));
358-
}
359-
360355
// Returns a borrwed reference to the type
361-
pub inline fn typeref(self: *T) *Type {
362-
return @ptrCast(c.Py_TYPE(@ptrCast(self)));
356+
pub inline fn typeref(self: *const T) *Type {
357+
return @ptrCast(c.Py_TYPE(@constCast(@ptrCast(self))));
363358
}
364359

365360
// Return the type name of the object as a [:0]const u8
366-
pub inline fn typeName(self: *T) [:0]const u8 {
361+
pub inline fn typeName(self: *const T) [:0]const u8 {
367362
return self.typeref().className();
368363
}
369364

@@ -523,21 +518,21 @@ pub inline fn ObjectProtocol(comptime T: type) type {
523518

524519
// Return non-zero if the object o is of type type or a subtype of type,
525520
// and 0 otherwise. Both parameters must be non-NULL.
526-
pub inline fn typeCheck(self: *T, tp: *Type) bool {
527-
return c.PyObject_TypeCheck(@ptrCast(self), @ptrCast(tp)) != 0;
521+
pub inline fn typeCheck(self: *const T, tp: *const Type) bool {
522+
return c.PyObject_TypeCheck(@constCast(@ptrCast(self)), @constCast(@ptrCast(tp))) != 0;
528523
}
529524

530525
// Shortcut to check that the given pointer for correctly typed
531526
// This is equivalent to `T.check(@ptrCast(self))`
532527
// If this returns false it means *T was incorrectly casted or the assumed type is wrong
533-
pub inline fn typeCheckSelf(self: *T) bool {
528+
pub inline fn typeCheckSelf(self: *const T) bool {
534529
return T.check(@ptrCast(self));
535530
}
536531

537532
// Shortcut to check that the given pointer for correctly typed
538533
// This is equivalent to `T.checkExact(@ptrCast(self))`
539534
// If this returns false it means *T was incorrectly casted or the assumed type is wrong
540-
pub inline fn typeCheckExactSelf(self: *T) bool {
535+
pub inline fn typeCheckExactSelf(self: *const T) bool {
541536
return T.checkExact(@ptrCast(self));
542537
}
543538

@@ -898,8 +893,8 @@ pub const Iter = struct {
898893
// Import the iterarator
899894
pub usingnamespace IteratorProtocol(@This());
900895

901-
pub fn check(obj: *Object) bool {
902-
return c.PyIter_Check(@ptrCast(obj)) != 0;
896+
pub fn check(obj: *const Object) bool {
897+
return c.PyIter_Check(@constCast(@ptrCast(obj))) != 0;
903898
}
904899

905900
};
@@ -941,18 +936,18 @@ pub const Type = extern struct {
941936

942937
// Return true if the object o is a type object, including instances of types derived
943938
// from the standard type object. Return 0 in all other cases. This function always succeeds.
944-
pub inline fn check(obj: *Object) bool {
945-
return c.PyType_Check(@ptrCast(obj)) != 0;
939+
pub inline fn check(obj: *const Object) bool {
940+
return c.PyType_Check(@constCast(@ptrCast(obj))) != 0;
946941
}
947942

948943
// Return non-zero if the object o is a type object, but not a subtype of the standard type object.
949944
// Return 0 in all other cases. This function always succeeds.
950-
pub inline fn checkExact(obj: *Object) bool {
951-
return c.PyType_CheckExact(@ptrCast(obj)) != 0;
945+
pub inline fn checkExact(obj: *const Object) bool {
946+
return c.PyType_CheckExact(@constCast(@ptrCast(obj))) != 0;
952947
}
953948

954949
// Return the name of this type as a [:0]const u8
955-
pub inline fn className(self: *Type) [:0]const u8 {
950+
pub inline fn className(self: *const Type) [:0]const u8 {
956951
return std.mem.span(self.impl.tp_name);
957952
}
958953

@@ -1002,8 +997,8 @@ pub const Bool = extern struct {
1002997
// Import the object protocol
1003998
pub usingnamespace ObjectProtocol(@This());
1004999

1005-
pub inline fn check(obj: *Object) bool {
1006-
return c.PyBool_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1000+
pub inline fn check(obj: *const Object) bool {
1001+
return c.PyBool_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
10071002
}
10081003
pub const checkExact = check;
10091004

@@ -1024,13 +1019,13 @@ pub const Int = extern struct {
10241019
pub usingnamespace ObjectProtocol(@This());
10251020

10261021
// Return true if its argument is a PyLongObject or a subtype of PyLongObject. This function always succeeds.
1027-
pub inline fn check(obj: *Object) bool {
1028-
return c.PyLong_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1022+
pub inline fn check(obj: *const Object) bool {
1023+
return c.PyLong_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
10291024
}
10301025

10311026
// Return true if its argument is a PyLongObject, but not a subtype of PyLongObject. This function always succeeds.
1032-
pub inline fn checkExact(obj: *Object) bool {
1033-
return c.PyLong_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1027+
pub inline fn checkExact(obj: *const Object) bool {
1028+
return c.PyLong_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
10341029
}
10351030

10361031
// Convert to the given zig type
@@ -1124,13 +1119,13 @@ pub const Float = extern struct {
11241119
pub usingnamespace ObjectProtocol(@This());
11251120

11261121
// Return true if its argument is a PyFloatObject or a subtype of PyFloatObject. This function always succeeds.
1127-
pub inline fn check(obj: *Object) bool {
1128-
return c.PyFloat_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1122+
pub inline fn check(obj: *const Object) bool {
1123+
return c.PyFloat_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
11291124
}
11301125

11311126
// Return true if its argument is a PyFloatObject, but not a subtype of PyFloatObject. This function always succeeds.
1132-
pub inline fn checkExact(obj: *Object) bool {
1133-
return c.PyFloat_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1127+
pub inline fn checkExact(obj: *const Object) bool {
1128+
return c.PyFloat_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
11341129
}
11351130

11361131
// Get the value of the Float object as the given type with error checking.
@@ -1206,14 +1201,14 @@ pub const Str = extern struct {
12061201

12071202
// Return true if the object obj is a Unicode object or an instance of a Unicode subtype.
12081203
// This function always succeeds.
1209-
pub inline fn check(obj: *Object) bool {
1210-
return c.PyUnicode_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1204+
pub inline fn check(obj: *const Object) bool {
1205+
return c.PyUnicode_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
12111206
}
12121207

12131208
// Return true if the object obj is a Unicode object, but not an instance of a subtype.
12141209
// This function always succeeds.
1215-
pub inline fn checkExact(obj: *Object) bool {
1216-
return c.PyUnicode_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1210+
pub inline fn checkExact(obj: *const Object) bool {
1211+
return c.PyUnicode_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
12171212
}
12181213

12191214
// Return the length of the Unicode string, in code points. unicode has to be a
@@ -1280,13 +1275,13 @@ pub const Bytes = extern struct {
12801275
pub usingnamespace ObjectProtocol(@This());
12811276

12821277
// Return true if the object o is a bytes object or an instance of a subtype of the bytes type. This function always succeeds.
1283-
pub inline fn check(obj: *Object) bool {
1284-
return c.PyBytes_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1278+
pub inline fn check(obj: *const Object) bool {
1279+
return c.PyBytes_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
12851280
}
12861281

12871282
// Return true if the object o is a bytes object, but not an instance of a subtype of the bytes type. This function always succeeds.
1288-
pub inline fn checkExact(obj: *Object) bool {
1289-
return c.PyBytes_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1283+
pub inline fn checkExact(obj: *const Object) bool {
1284+
return c.PyBytes_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
12901285
}
12911286

12921287
// TODO: finish
@@ -1330,24 +1325,28 @@ pub const Tuple = extern struct {
13301325

13311326
// Return true if p is a tuple object or an instance of a subtype of the tuple type.
13321327
// This function always succeeds.
1333-
pub inline fn check(obj: *Object) bool {
1334-
return c.PyTuple_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1328+
pub inline fn check(obj: *const Object) bool {
1329+
return c.PyTuple_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
13351330
}
13361331

13371332
// Return true if p is a tuple object, but not an instance of a subtype of the tuple type.
13381333
// This function always succeeds.
1339-
pub inline fn checkExact(obj: *Object) bool {
1340-
return c.PyTuple_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1334+
pub inline fn checkExact(obj: *const Object) bool {
1335+
return c.PyTuple_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
13411336
}
13421337

13431338
// Return a new tuple object of size len, or NULL with an exception set on failure.
13441339
pub inline fn new(len: usize) !*Tuple {
1345-
if (c.PyTuple_New(@intCast(len))) |r| {
1340+
if (newUnchecked(len)) |r| {
13461341
return @ptrCast(r);
13471342
}
13481343
return error.PyError;
13491344
}
13501345

1346+
pub inline fn newUnchecked(len: usize) ?*Object {
1347+
return @ptrCast(c.PyTuple_New(@intCast(len)));
1348+
}
1349+
13511350
// Return a new tuple filled with the provided values from a zig tuple.
13521351
// This steals a reference to every item in args.
13531352
// Returns new reference
@@ -1516,13 +1515,13 @@ pub const List = extern struct {
15161515
pub usingnamespace SequenceProtocol(@This());
15171516

15181517
// Return true if p is a list object or an instance of a subtype of the list type. This function always succeeds.
1519-
pub inline fn check(obj: *Object) bool {
1520-
return c.PyList_Check(@ptrCast(obj)) != 0;
1518+
pub inline fn check(obj: *const Object) bool {
1519+
return c.PyList_Check(@constCast(@ptrCast(obj))) != 0;
15211520
}
15221521

15231522
// Return true if p is a list object, but not an instance of a subtype of the list type. This function always succeeds.
1524-
pub inline fn checkExact(obj: *Object) bool {
1525-
return c.PyList_Check(@ptrCast(obj)) != 0;
1523+
pub inline fn checkExact(obj: *const Object) bool {
1524+
return c.PyList_Check(@constCast(@ptrCast(obj))) != 0;
15261525
}
15271526

15281527
// Return a new empty dictionary, or NULL on failure.
@@ -1734,13 +1733,13 @@ pub const Dict = extern struct {
17341733
pub usingnamespace ObjectProtocol(@This());
17351734

17361735
// Return true if p is a dict object or an instance of a subtype of the dict type. This function always succeeds.
1737-
pub inline fn check(obj: *Object) bool {
1738-
return c.PyDict_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1736+
pub inline fn check(obj: *const Object) bool {
1737+
return c.PyDict_Check(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
17391738
}
17401739

17411740
// Return true if p is a dict object, but not an instance of a subtype of the dict type. This function always succeeds.
1742-
pub inline fn checkExact(obj: *Object) bool {
1743-
return c.PyDict_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
1741+
pub inline fn checkExact(obj: *const Object) bool {
1742+
return c.PyDict_CheckExact(@as([*c]c.PyObject, @constCast(@ptrCast(obj)))) != 0;
17441743
}
17451744

17461745
// Return a new empty dictionary, or NULL on failure.
@@ -1951,12 +1950,12 @@ pub const Set = extern struct {
19511950
pub usingnamespace ObjectProtocol(@This());
19521951

19531952
// Return true if p is a set object or an instance of a subtype. This function always succeeds.
1954-
pub inline fn check(obj: *Object) bool {
1953+
pub inline fn check(obj: *const Object) bool {
19551954
return c.PySet_Check(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
19561955
}
19571956

19581957
// Return true if p is a set object but not an instance of a subtype. This function always succeeds.
1959-
pub inline fn checkExact(obj: *Object) bool {
1958+
pub inline fn checkExact(obj: *const Object) bool {
19601959
return c.PySet_CheckExact(@as([*c]c.PyObject, @ptrCast(obj))) != 0;
19611960
}
19621961

@@ -2099,8 +2098,8 @@ pub const Code = extern struct {
20992098
pub usingnamespace ObjectProtocol(@This());
21002099

21012100
// Return true if co is a code object. This function always succeeds.
2102-
pub fn check(obj: *Object) bool {
2103-
return c.PyCode_Check(@ptrCast(obj)) != 0;
2101+
pub fn check(obj: *const Object) bool {
2102+
return c.PyCode_Check(@constCast(@ptrCast(obj))) != 0;
21042103
}
21052104

21062105
};
@@ -2113,8 +2112,8 @@ pub const Function = extern struct {
21132112

21142113
// Return true if o is a function object (has type PyFunction_Type).
21152114
// The parameter must not be NULL. This function always succeeds.
2116-
pub fn check(obj: *Object) bool {
2117-
return c.PyFunction_Check(@ptrCast(obj)) != 0;
2115+
pub fn check(obj: *const Object) bool {
2116+
return c.PyFunction_Check(@constCast(@ptrCast(obj))) != 0;
21182117
}
21192118

21202119
// Return a new function object associated with the code object code.
@@ -2220,7 +2219,7 @@ pub const Method = extern struct {
22202219
// Return true if o is a method object (has type PyMethod_Type).
22212220
// The parameter must not be NULL. This function always succeeds.
22222221
pub fn check(obj: *Object) bool {
2223-
return c.PyMethod_Check(@ptrCast(obj)) != 0;
2222+
return c.PyMethod_Check(@constCast(@ptrCast(obj))) != 0;
22242223
}
22252224

22262225
// Return a new method object, with func being any callable object and self the
@@ -2269,14 +2268,14 @@ pub const Module = extern struct {
22692268

22702269
// Return true if p is a module object, or a subtype of a module object.
22712270
// This function always succeeds.
2272-
pub inline fn check(obj: *Object) bool {
2273-
return c.PyModule_Check(@ptrCast(obj)) == 1;
2271+
pub inline fn check(obj: *const Object) bool {
2272+
return c.PyModule_Check(@constCast(@ptrCast(obj))) == 1;
22742273
}
22752274

22762275
// Return true if p is a module object, but not a subtype of PyModule_Type.
22772276
// This function always succeeds.
2278-
pub inline fn checkExact(obj: *Object) bool {
2279-
return c.PyModule_CheckExact(@ptrCast(obj)) == 1;
2277+
pub inline fn checkExact(obj: *const Object) bool {
2278+
return c.PyModule_CheckExact(@constCast(@ptrCast(obj))) == 1;
22802279
}
22812280

22822281
// Add an object to module as name. This is a convenience function which can be used
@@ -2315,6 +2314,7 @@ pub inline fn importModule(name: [:0]const u8) !*Module {
23152314
}
23162315

23172316
pub const MethodDef = c.PyMethodDef;
2317+
pub const MemberDef = c.PyMemberDef;
23182318
pub const GetSetDef = c.PyGetSetDef;
23192319
pub const SlotDef = c.PyModuleDef_Slot;
23202320

0 commit comments

Comments
 (0)