1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
from .support import HPyTest, DefaultExtensionTemplate
class HPyLongTemplate(DefaultExtensionTemplate):
def DEFINE_Long_From(self, type, api_suffix, val):
self.EXPORT("from_{type}_{val}".format(type=type, val=val))
return """
HPyDef_METH(from_{type}_{val}, "from_{type}_{val}", HPyFunc_NOARGS)
static HPy from_{type}_{val}_impl(HPyContext *ctx, HPy self)
{{
{type} a = {val};
return HPyLong_From{api_suffix}(ctx, a);
}}
""".format(type=type, val=val, api_suffix=api_suffix)
def DEFINE_Long_As(self, type, api_suffix, from_suffix=None):
self.EXPORT("as_{api_suffix}".format(api_suffix=api_suffix))
if not from_suffix:
from_suffix = api_suffix
return """
HPyDef_METH(as_{api_suffix}, "as_{api_suffix}", HPyFunc_O)
static HPy as_{api_suffix}_impl(HPyContext *ctx, HPy self, HPy arg)
{{
{type} a = HPyLong_As{api_suffix}(ctx, arg);
if (a == (({type})-1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_From{from_suffix}(ctx, a * 2);
}}
""".format(type=type, api_suffix=api_suffix, from_suffix=from_suffix)
class TestLong(HPyTest):
ExtensionTemplate = HPyLongTemplate
def unsigned_long_bits(self):
""" Return the number of bits in an unsigned long. """
import struct
unsigned_long_bytes = len(struct.pack('l', 0))
return 8 * unsigned_long_bytes
def magic_int(self, v):
""" Return an instance of a class that implements __int__
and returns value v.
"""
class MagicInt(object):
def __int__(self):
return v
return MagicInt()
def magic_index(self, v):
""" Return an instance of a class that implements __index__
and returns value v.
"""
class MagicIndex(object):
def __index__(self):
return v
return MagicIndex()
def python_supports_magic_index(self):
""" Return True if the Python version is 3.8 or later and thus
should support calling __index__ in the various HPyLong_As...
methods.
"""
import sys
vi = sys.version_info
return (vi.major > 3 or (vi.major == 3 and vi.minor >= 8))
def python_supports_magic_int(self):
""" Return True if the Python version is 3.9 or earlier and thus
should support calling __int__ on non-int based types in some
HPyLong_As... methods.
"""
import sys
vi = sys.version_info
assert vi.major >= 3
return (vi.major == 3 and vi.minor <= 9)
def test_Long_FromFixedWidth(self):
mod = self.make_module("""
@DEFINE_Long_From(int32_t, Int32_t, INT32_MAX)
@DEFINE_Long_From(int32_t, Int32_t, INT32_MIN)
@DEFINE_Long_From(uint32_t, UInt32_t, UINT32_MAX)
@DEFINE_Long_From(int64_t, Int64_t, INT64_MAX)
@DEFINE_Long_From(int64_t, Int64_t, INT64_MIN)
@DEFINE_Long_From(uint64_t, UInt64_t, UINT64_MAX)
@INIT
""")
assert mod.from_int32_t_INT32_MAX() == 2147483647
assert mod.from_int32_t_INT32_MIN() == -2147483648
assert mod.from_uint32_t_UINT32_MAX() == 4294967295
assert mod.from_int64_t_INT64_MAX() == 9223372036854775807
assert mod.from_int64_t_INT64_MIN() == -9223372036854775808
assert mod.from_uint64_t_UINT64_MAX() == 18446744073709551615
def test_Long_FromLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
long a = 500;
return HPyLong_FromLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 500
def test_Long_AsFixedWidth(self):
import pytest
mod = self.make_module("""
@DEFINE_Long_As(int32_t, Int32_t)
@DEFINE_Long_As(uint32_t, UInt32_t)
@DEFINE_Long_As(uint32_t, UInt32_tMask, UInt32_t)
@DEFINE_Long_As(int64_t, Int64_t)
@DEFINE_Long_As(uint64_t, UInt64_t)
@DEFINE_Long_As(uint64_t, UInt64_tMask, UInt64_t)
@INIT
""")
assert mod.as_Int32_t(45) == 90
assert mod.as_Int32_t(-45) == -90
# doubling INT32_MAX is like a left-shift and will result in a negative value
assert mod.as_Int32_t(2147483647) < 0
with pytest.raises(TypeError):
mod.as_Int32_t("this is not a number")
if self.python_supports_magic_int():
assert mod.as_Int32_t(self.magic_int(2)) == 4
if self.python_supports_magic_index():
assert mod.as_Int32_t(self.magic_index(2)) == 4
assert mod.as_UInt32_t(45) == 90
with pytest.raises(OverflowError):
mod.as_UInt32_t(-45)
assert mod.as_UInt32_t(2147483647) == 4294967294
assert mod.as_UInt32_tMask(0xffffffffffffffff) == 0xfffffffe
assert mod.as_Int64_t(45) == 90
assert mod.as_Int64_t(-45) == -90
# doubling INT32_MAX is like a left-shift and will result in a negative value
assert mod.as_Int64_t(2147483647) == 4294967294
assert mod.as_Int64_t(9223372036854775807) < 0
with pytest.raises(TypeError):
mod.as_Int64_t("this is not a number")
assert mod.as_UInt64_t(45) == 90
with pytest.raises(OverflowError):
mod.as_UInt64_t(-45)
assert mod.as_UInt64_t(9223372036854775807) == 18446744073709551614
assert mod.as_UInt64_tMask(0xffffffffffffffffff) == 0xfffffffffffffffe
def test_Long_AsLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
long a = HPyLong_AsLong(ctx, arg);
if (a == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromLong(ctx, a * 2);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 90
with pytest.raises(TypeError):
mod.f("this is not a number")
if self.python_supports_magic_int():
assert mod.f(self.magic_int(2)) == 4
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 4
def test_Long_FromUnsignedLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
unsigned long a = 500;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 500
def test_Long_AsUnsignedLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long a = HPyLong_AsUnsignedLong(ctx, arg);
if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
with pytest.raises(OverflowError):
mod.f(-91)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsUnsignedLongMask(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long a = HPyLong_AsUnsignedLongMask(ctx, arg);
if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
assert mod.f(-1) == 2**self.unsigned_long_bits() - 1
with pytest.raises(TypeError):
mod.f("this is not a number")
if self.python_supports_magic_int():
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromLongLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in 32 bit
long long val = 2147483648;
return HPyLong_FromLongLong(ctx, val);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 2147483648
def test_Long_AsLongLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
long long a = HPyLong_AsLongLong(ctx, arg);
if ((a == (long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(2147483648) == 2147483648
assert mod.f(-2147483648) == -2147483648
with pytest.raises(TypeError):
mod.f("this is not a number")
if self.python_supports_magic_int():
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromUnsignedLongLong(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in unsigned 32 bit
unsigned long long val = 4294967296;
return HPyLong_FromUnsignedLongLong(ctx, val);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 4294967296
def test_Long_AsUnsignedLongLong(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long long a = HPyLong_AsUnsignedLongLong(ctx, arg);
if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(4294967296) == 4294967296
with pytest.raises(OverflowError):
mod.f(-4294967296)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsUnsignedLongLongMask(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
unsigned long long a = HPyLong_AsUnsignedLongLongMask(ctx, arg);
if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromUnsignedLongLong(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45
assert mod.f(-1) == 2**64 - 1
with pytest.raises(TypeError):
mod.f("this is not a number")
if self.python_supports_magic_int():
assert mod.f(self.magic_int(2)) == 2
if self.python_supports_magic_index():
assert mod.f(self.magic_index(2)) == 2
def test_Long_FromSize_t(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
// take a value which doesn't fit in 32 bit
size_t a = 2147483648;
return HPyLong_FromSize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == 2147483648
def test_Long_AsSize_t(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
size_t a = HPyLong_AsSize_t(ctx, arg);
if ((a == (size_t) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromSize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(2147483648) == 2147483648
with pytest.raises(OverflowError):
mod.f(-2147483648)
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_FromSsize_t(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_NOARGS)
static HPy f_impl(HPyContext *ctx, HPy self)
{
HPy_ssize_t a = -42;
return HPyLong_FromSsize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f() == -42
def test_Long_AsSsize_t(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
HPy_ssize_t a = HPyLong_AsSsize_t(ctx, arg);
if ((a == (HPy_ssize_t) -1) && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyLong_FromSsize_t(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(41) == 41
assert mod.f(-41) == -41
with pytest.raises(TypeError):
mod.f("this is not a number")
with pytest.raises(TypeError):
mod.f(self.magic_int(2))
with pytest.raises(TypeError):
mod.f(self.magic_index(2))
def test_Long_AsVoidPtr(self):
mod = self.make_module("""
HPyDef_METH(f, "is_null", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy val)
{
void* ptr = HPyLong_AsVoidPtr(ctx, val);
if (!ptr) {
return HPy_Dup(ctx, ctx->h_True);
} else {
return HPy_Dup(ctx, ctx->h_False);
}
}
@EXPORT(f)
@INIT
""")
assert mod.is_null(0) == True
assert mod.is_null(10) == False
def test_Long_AsDouble(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
double a = HPyLong_AsDouble(ctx, arg);
if (a == -1.0 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyFloat_FromDouble(ctx, a);
}
@EXPORT(f)
@INIT
""")
assert mod.f(45) == 45.0
with pytest.raises(TypeError):
mod.f("this is not a number")
|