@@ -659,3 +659,45 @@ def test_is(self):
659
659
a = object ()
660
660
assert mod .f (a , a )
661
661
assert not mod .f (a , None )
662
+
663
+ def test_is_ctx_constant (self ):
664
+ mod = self .make_module ("""
665
+ HPyDef_METH(f, "f", f_impl, HPyFunc_VARARGS)
666
+ static HPy f_impl(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs)
667
+ {
668
+ HPy obj;
669
+ int constant;
670
+ if (!HPyArg_Parse(ctx, NULL, args, nargs, "iO", &constant, &obj))
671
+ return HPy_NULL;
672
+ HPy h_constant;
673
+ switch (constant) {
674
+ case 0:
675
+ h_constant = ctx->h_None;
676
+ break;
677
+ case 1:
678
+ h_constant = ctx->h_True;
679
+ break;
680
+ case 2:
681
+ h_constant = ctx->h_False;
682
+ break;
683
+ case 3:
684
+ h_constant = ctx->h_NotImplemented;
685
+ break;
686
+ case 4:
687
+ h_constant = ctx->h_Ellipsis;
688
+ break;
689
+ default:
690
+ HPyErr_SetString(ctx, ctx->h_ValueError, "invalid choice");
691
+ return HPy_NULL;
692
+ }
693
+ int res = HPy_Is(ctx, obj, h_constant);
694
+ return HPyBool_FromLong(ctx, res);
695
+ }
696
+ @EXPORT(f)
697
+ @INIT
698
+ """ )
699
+ ctx_constants = [None , True , False , NotImplemented , Ellipsis ]
700
+ for idx , const in enumerate (ctx_constants ):
701
+ for other_idx in range (len (ctx_constants )):
702
+ expected = (idx == other_idx )
703
+ assert mod .f (other_idx , const ) == expected , "{}, {}, {}" .format (other_idx , const , expected )
0 commit comments