@@ -4,7 +4,6 @@ pub const c = @cImport({
44});
55const std = @import ("std" );
66
7-
87const VER_312 = 0x030C0000 ;
98const VER_313 = 0x030D0000 ;
109
@@ -170,7 +169,7 @@ pub inline fn clear(obj: anytype) void {
170169
171170// Clear all
172171pub inline fn clearAll (objs : anytype ) void {
173- inline for (objs ) | obj | {
172+ inline for (objs ) | obj | {
174173 clear (obj );
175174 }
176175}
@@ -179,15 +178,15 @@ pub inline fn None() *Object {
179178 return @ptrCast (& c ._Py_NoneStruct );
180179}
181180
182- pub inline fn True () * Object {
181+ pub inline fn True () * Object {
183182 return @ptrCast (& c ._Py_TrueStruct );
184183}
185184
186- pub inline fn False () * Object {
185+ pub inline fn False () * Object {
187186 return @ptrCast (& c ._Py_FalseStruct );
188187}
189188
190- pub inline fn NotImplemented () * Object {
189+ pub inline fn NotImplemented () * Object {
191190 return @ptrCast (& c ._Py_NotImplementedStruct );
192191}
193192
@@ -227,7 +226,6 @@ pub inline fn returnNotImplemented() *Object {
227226 return NotImplemented ().newref ();
228227}
229228
230-
231229// Only returns true if the object not null and not None
232230pub inline fn notNone (obj : anytype ) bool {
233231 const T = @TypeOf (obj );
@@ -239,9 +237,7 @@ pub inline fn notNone(obj: anytype) bool {
239237 }
240238 return false ;
241239 } else {
242- @compileError (std .fmt .comptimePrint (
243- "py.notNone must be called with a *Object or ?*Object: got {s}" , .{T }
244- ));
240+ @compileError (std .fmt .comptimePrint ("py.notNone must be called with a *Object or ?*Object: got {s}" , .{T }));
245241 }
246242}
247243
@@ -300,33 +296,31 @@ pub inline fn parseTupleAndKeywords(args: *Tuple, kwargs: ?*Dict, format: [:0]co
300296
301297// Check that the given type can be safely casted to a *Object
302298pub inline fn canCastToObject (comptime T : type ) bool {
303- return switch (@typeInfo (T )) {
299+ return switch (@typeInfo (T )) {
304300 .Pointer = > | info | @hasDecl (info .child , "IS_PYOBJECT" ),
305301 else = > false ,
306302 };
307303}
308304
309-
310305// Check that the given type can be safely casted to a ?*Object
311306pub inline fn canCastToOptionalObject (comptime T : type ) bool {
312- return switch (@typeInfo (T )) {
307+ return switch (@typeInfo (T )) {
313308 .Optional = > | info | canCastToObject (info .child ),
314309 else = > false ,
315310 };
316311}
317312
318313// Check that the given type can be safely casted to a **Object
319314pub inline fn canCastToObjectPtr (comptime T : type ) bool {
320- return switch (@typeInfo (T )) {
315+ return switch (@typeInfo (T )) {
321316 .Pointer = > | info | canCastToObject (info .child ),
322317 else = > false ,
323318 };
324319}
325320
326-
327321// Check that the given type can be safely casted to a *?*Object
328322pub inline fn canCastToOptionalObjectPtr (comptime T : type ) bool {
329- return switch (@typeInfo (T )) {
323+ return switch (@typeInfo (T )) {
330324 .Pointer = > | info | canCastToOptionalObject (info .child ),
331325 else = > false ,
332326 };
@@ -476,7 +470,7 @@ pub inline fn ObjectProtocol(comptime T: type) type {
476470 pub inline fn iter (self : * T ) ! * Iter {
477471 if (self .iterUnchecked ()) | r | {
478472 if (! Iter .check (r )) {
479- return typeError ("iter did not return an iterator" , .{});
473+ try typeError ("iter did not return an iterator" , .{});
480474 }
481475 return @ptrCast (r );
482476 }
@@ -494,7 +488,7 @@ pub inline fn ObjectProtocol(comptime T: type) type {
494488 return @ptrCast (s );
495489 }
496490 // Set an error message
497- return typeError ("str did not return a str" , .{});
491+ try typeError ("str did not return a str" , .{});
498492 }
499493 return error .PyError ;
500494 }
@@ -819,7 +813,6 @@ pub fn SequenceProtocol(comptime T: type) type {
819813 return c .PySequence_Index (@ptrCast (self ), @ptrCast (obj ));
820814 }
821815
822-
823816 // Determine if o contains value. If an item in o is equal to value, return 1,
824817 // otherwise return 0. On error, return -1.
825818 // This is equivalent to the Python expression value in o.
@@ -882,7 +875,6 @@ pub const Object = extern struct {
882875 }
883876};
884877
885-
886878pub const Iter = struct {
887879 // The underlying python structure
888880 impl : c.PyObject ,
@@ -896,7 +888,6 @@ pub const Iter = struct {
896888 pub fn check (obj : * const Object ) bool {
897889 return c .PyIter_Check (@constCast (@ptrCast (obj ))) != 0 ;
898890 }
899-
900891};
901892
902893pub const TypeSlot = c .PyType_Slot ;
@@ -1075,7 +1066,7 @@ pub const Int = extern struct {
10751066 usize = > return @ptrCast (c .PyLong_FromSize_t (value )),
10761067 c_uint , c_ulong = > return @ptrCast (c .PyLong_FromUnsignedLong (value )),
10771068 c_int , c_long = > return @ptrCast (c .PyLong_FromLong (value )),
1078- comptime_int ,c_longlong = > return @ptrCast (c .PyLong_FromLongLong (value )),
1069+ comptime_int , c_longlong = > return @ptrCast (c .PyLong_FromLongLong (value )),
10791070 c_ulonglong = > return @ptrCast (c .PyLong_FromUnsignedLongLong (value )),
10801071 else = > {}, // Might be a float another zig int size
10811072 }
@@ -1109,7 +1100,6 @@ pub const Int = extern struct {
11091100 // Alias
11101101 pub const fromNumber = new ;
11111102 pub const fromNumberUnchecked = newUnchecked ;
1112-
11131103};
11141104
11151105pub const Float = extern struct {
@@ -1266,7 +1256,6 @@ pub const Str = extern struct {
12661256 pub const data = asString ;
12671257};
12681258
1269-
12701259pub const Bytes = extern struct {
12711260 // The underlying python structure
12721261 impl : c.PyBytesObject ,
@@ -1305,19 +1294,19 @@ pub const Tuple = extern struct {
13051294 pub inline fn parseTyped (self : * Tuple , args : anytype ) ! void {
13061295 const n = try self .size ();
13071296 if (n != args .len ) {
1308- return typeError ("Expected {} arguments got {}" , .{args .len , n }); // TODO: Better message
1297+ return typeError ("Expected {} arguments got {}" , .{ args .len , n }); // TODO: Better message
13091298 }
1310- inline for (args , 0.. ) | arg , i | {
1299+ inline for (args , 0.. ) | arg , i | {
13111300 const T = @TypeOf (arg );
13121301 if (comptime ! canCastToObjectPtr (T )) {
1313- @compileError (std .fmt .comptimePrint ("parseTyped args must be *Object or subclasses: got {}" ,.{T }));
1302+ @compileError (std .fmt .comptimePrint ("parseTyped args must be *Object or subclasses: got {}" , .{T }));
13141303 }
13151304 // Eg var arg: *Str: undefined
13161305 // then &arg is **Str
13171306 const ArgType = @typeInfo (@typeInfo (T ).Pointer .child ).Pointer .child ;
13181307 const obj = try self .get (i );
13191308 if (! ArgType .check (obj )) {
1320- return typeError ("Argument at {} must be {}" , .{i , ArgType });
1309+ return typeError ("Argument at {} must be {}" , .{ i , ArgType });
13211310 }
13221311 arg .* = @ptrCast (obj );
13231312 }
@@ -1392,7 +1381,7 @@ pub const Tuple = extern struct {
13921381 try r .set (i , a .getUnsafe (i ).? .newref ());
13931382 }
13941383 for (0.. n2 ) | i | {
1395- try r .set (i + n1 , b .getUnsafe (i ).? .newref ());
1384+ try r .set (i + n1 , b .getUnsafe (i ).? .newref ());
13961385 }
13971386 return r ;
13981387 }
@@ -1403,11 +1392,11 @@ pub const Tuple = extern struct {
14031392 // Returns new reference
14041393 pub inline fn prepend (self : * Tuple , obj : * Object ) ! * Tuple {
14051394 const n = try self .size ();
1406- const r = try Tuple .new (n + 1 );
1395+ const r = try Tuple .new (n + 1 );
14071396 errdefer r .decref ();
14081397 try r .set (0 , obj .newref ());
14091398 for (0.. n ) | i | {
1410- try r .set (i + 1 , self .getUnsafe (i ).? .newref ());
1399+ try r .set (i + 1 , self .getUnsafe (i ).? .newref ());
14111400 }
14121401 return r ;
14131402 }
@@ -1417,7 +1406,7 @@ pub const Tuple = extern struct {
14171406 // Returns new reference
14181407 pub inline fn append (self : * Tuple , obj : * Object ) ! * Tuple {
14191408 const n = try self .size ();
1420- const r = try Tuple .new (n + 1 );
1409+ const r = try Tuple .new (n + 1 );
14211410 errdefer r .decref ();
14221411 for (0.. n ) | i | {
14231412 try r .set (i , self .getUnsafe (i ).? .newref ());
@@ -1498,8 +1487,6 @@ pub const Tuple = extern struct {
14981487 const end = try self .size ();
14991488 return try self .slice (0 , end );
15001489 }
1501-
1502-
15031490};
15041491
15051492// TODO: Create a ListProtocol()
@@ -1719,7 +1706,6 @@ pub const List = extern struct {
17191706 }
17201707};
17211708
1722-
17231709// TODO: Create a DictProtocol()?
17241710
17251711pub const Dict = extern struct {
@@ -1942,7 +1928,6 @@ pub const Dict = extern struct {
19421928 }
19431929};
19441930
1945-
19461931pub const Set = extern struct {
19471932 impl : c.PySetObject ,
19481933
@@ -2020,7 +2005,6 @@ pub const Set = extern struct {
20202005 return r == 1 ;
20212006 }
20222007
2023-
20242008 // Same as contains with no error checking
20252009 pub fn containsUnchecked (self : * Set , key : * Object ) c_int {
20262010 return c .PySet_Contains (@ptrCast (self ), @ptrCast (key ));
@@ -2087,10 +2071,8 @@ pub const Set = extern struct {
20872071 pub fn clearUnchecked (self : * Set ) c_int {
20882072 return c .PySet_Clear (@ptrCast (self ));
20892073 }
2090-
20912074};
20922075
2093-
20942076pub const Code = extern struct {
20952077 impl : c.PyTypeObject ,
20962078
@@ -2101,7 +2083,6 @@ pub const Code = extern struct {
21012083 pub fn check (obj : * const Object ) bool {
21022084 return c .PyCode_Check (@constCast (@ptrCast (obj ))) != 0 ;
21032085 }
2104-
21052086};
21062087
21072088pub const Function = extern struct {
@@ -2219,7 +2200,7 @@ pub const Method = extern struct {
22192200 // Return true if o is a method object (has type PyMethod_Type).
22202201 // The parameter must not be NULL. This function always succeeds.
22212202 pub fn check (obj : * Object ) bool {
2222- return c .PyMethod_Check (@constCast (@ptrCast (obj ))) != 0 ;
2203+ return c .PyMethod_Check (@constCast (@ptrCast (obj ))) != 0 ;
22232204 }
22242205
22252206 // Return a new method object, with func being any callable object and self the
@@ -2241,10 +2222,10 @@ pub const Method = extern struct {
22412222 // Return the instance associated with the method meth.
22422223 // Returns borrowed reference
22432224 pub fn getSelf (self : * Method ) ! * Object {
2244- if (c .PyMethod_Self (@ptrCast (self ))) | r | {
2245- return @ptrCast (r );
2246- }
2247- return error .PyError ;
2225+ if (c .PyMethod_Self (@ptrCast (self ))) | r | {
2226+ return @ptrCast (r );
2227+ }
2228+ return error .PyError ;
22482229 }
22492230
22502231 // Return the function associated with the method meth.
@@ -2257,7 +2238,6 @@ pub const Method = extern struct {
22572238 }
22582239};
22592240
2260-
22612241pub const Module = extern struct {
22622242 // https://docs.python.org/3/c-api/module.html
22632243 // The underlying python structure
0 commit comments