numo-narray-alt 0.10.7 → 0.10.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9897dbc5b94aa16af082880fe4085b341b628664ae6abc27e2ffa9e989e88451
4
- data.tar.gz: d1d53a63934a8a51ee74cf64093b2b3869e03e75bea6070830230e14d9e91b91
3
+ metadata.gz: 2a43b0064dadb89044a9c076fb52ef4bd813a58b0061e54637e9ecdd86e2039f
4
+ data.tar.gz: dbdd293ea1dc644c73b68c5b8bf673fa1f61e6d02dff07eba35d2bc0090ccfeb
5
5
  SHA512:
6
- metadata.gz: 165941e9d06b5cd9e59ae591ab5666f9cb9924786d5253ce889da809de69b72c1893faef2a4c1e8f2d875021596415496802b8fb1bcf0b523b82c4cde231fb32
7
- data.tar.gz: fd52d794a7c41344534a07e726b1f13bf0af8003d8e428a0c4f133a17efd5c9721e029a2365fc4a0f4cc04b21f9255897d4927f19500eeaeb51e2cdeb94018f1
6
+ metadata.gz: 6b72e8e2503c8d0e2801656ed6fa1f4be291dd38f4b905907a2937a1e34b442be4e5c8bf4b6317df25e241de16bc1ec407b83dbd6454951d3a4bfbac54904870
7
+ data.tar.gz: 1cc9c4fe782331324f18119f589d5a66a011886fa6000ea637ddcf7e319d44bb5b0e4a1b6842ad5b687e39ee27e440bf5223c0071e5fa6337fa95029de77dd97
@@ -8,6 +8,7 @@
8
8
  #include "numo/narray.h"
9
9
  #include "numo/template.h"
10
10
 
11
+ static ID id_copy;
11
12
  static ID id_mulsum;
12
13
  static ID id_store;
13
14
  static ID id_swap_byte;
@@ -105,7 +106,14 @@ static VALUE nary_swap_byte(VALUE self) {
105
106
  ndfunc_arg_out_t aout[1] = { { INT2FIX(0), 0 } };
106
107
  ndfunc_t ndf = { iter_swap_byte, FULL_LOOP | NDF_ACCEPT_BYTESWAP, 1, 1, ain, aout };
107
108
 
108
- v = na_ndloop(&ndf, 1, self);
109
+ if (RTEST(rb_obj_is_kind_of(self, numo_cRObject))) {
110
+ rb_raise(rb_eTypeError, "cannot swap bytes of %s", rb_obj_classname(self));
111
+ }
112
+ if (RTEST(rb_obj_is_kind_of(self, numo_cBit))) {
113
+ v = TEST_INPLACE(self) ? self : rb_funcall(self, id_copy, 0);
114
+ } else {
115
+ v = na_ndloop(&ndf, 1, self);
116
+ }
109
117
  if (self != v) {
110
118
  na_copy_flags(self, v);
111
119
  }
@@ -359,6 +367,10 @@ static void na_check_reshape(int argc, VALUE* argv, VALUE self, size_t* shape) {
359
367
  } else if (total != NA_SIZE(na)) {
360
368
  rb_raise(rb_eArgError, "Total size must be same");
361
369
  }
370
+
371
+ if (argc > NA_MAX_DIMENSION) {
372
+ rb_raise(nary_eDimensionError, "ndim=%d is too many", argc);
373
+ }
362
374
  }
363
375
 
364
376
  /*
@@ -376,11 +388,15 @@ static VALUE na_reshape_bang(int argc, VALUE* argv, VALUE self) {
376
388
  ssize_t stride;
377
389
  stridx_t* stridx;
378
390
  int i;
391
+ VALUE tmp;
379
392
 
393
+ if (OBJ_FROZEN(self)) {
394
+ rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
395
+ }
380
396
  if (na_check_contiguous(self) == Qfalse) {
381
397
  rb_raise(rb_eStandardError, "cannot change shape of non-contiguous NArray");
382
398
  }
383
- shape = ALLOCA_N(size_t, argc);
399
+ shape = RB_ALLOCV_N(size_t, tmp, argc);
384
400
  na_check_reshape(argc, argv, self, shape);
385
401
 
386
402
  GetNArray(self, na);
@@ -391,7 +407,11 @@ static VALUE na_reshape_bang(int argc, VALUE* argv, VALUE self) {
391
407
  } else {
392
408
  stridx = na2->stridx;
393
409
  }
394
- stride = SDX_GET_STRIDE(na2->stridx[na->ndim - 1]);
410
+ if (na->ndim == 0) {
411
+ stride = nary_element_stride(self);
412
+ } else {
413
+ stride = SDX_GET_STRIDE(na2->stridx[na->ndim - 1]);
414
+ }
395
415
  for (i = argc; i--;) {
396
416
  SDX_SET_STRIDE(stridx[i], stride);
397
417
  stride *= shape[i];
@@ -402,6 +422,7 @@ static VALUE na_reshape_bang(int argc, VALUE* argv, VALUE self) {
402
422
  }
403
423
  }
404
424
  na_setup_shape(na, argc, shape);
425
+ RB_ALLOCV_END(tmp);
405
426
  return self;
406
427
  }
407
428
 
@@ -416,14 +437,15 @@ static VALUE na_reshape_bang(int argc, VALUE* argv, VALUE self) {
416
437
  static VALUE na_reshape(int argc, VALUE* argv, VALUE self) {
417
438
  size_t* shape;
418
439
  narray_t* na;
419
- VALUE copy;
440
+ VALUE copy, tmp;
420
441
 
421
- shape = ALLOCA_N(size_t, argc);
442
+ shape = RB_ALLOCV_N(size_t, tmp, argc);
422
443
  na_check_reshape(argc, argv, self, shape);
423
444
 
424
445
  copy = rb_funcall(self, rb_intern("dup"), 0);
425
446
  GetNArray(copy, na);
426
447
  na_setup_shape(na, argc, shape);
448
+ RB_ALLOCV_END(tmp);
427
449
  return copy;
428
450
  }
429
451
 
@@ -505,6 +527,9 @@ na_flatten_dim(VALUE self, int sd) {
505
527
  // set index
506
528
  idx2 = ALLOC_N(size_t, (shape[sd] == 0) ? 1 : shape[sd]);
507
529
  SDX_SET_INDEX(na2->stridx[sd], idx2);
530
+ if (shape[sd] == 0) {
531
+ return view;
532
+ }
508
533
  // init for md-loop
509
534
  fd = nd - sd;
510
535
  c = ALLOCA_N(size_t, fd);
@@ -965,6 +990,7 @@ void Init_nary_data(void) {
965
990
 
966
991
  // rb_define_method(cNArray, "dot", numo_na_dot, 1);
967
992
 
993
+ id_copy = rb_intern("copy");
968
994
  id_mulsum = rb_intern("mulsum");
969
995
  id_store = rb_intern("store");
970
996
  id_swap_byte = rb_intern("swap_byte");
@@ -439,7 +439,10 @@ static size_t na_index_parse_args(VALUE args, narray_t* na, na_index_arg_t* q, i
439
439
  }
440
440
  // new dimension
441
441
  else if (v == sym_new) {
442
- na_index_parse_each(v, 1, k, &q[j]);
442
+ // The aref functions below take an axis past the last dimension with no
443
+ // index array as a new axis. On a 0-d array a range or true is one too,
444
+ // which works because it picks the only element.
445
+ na_index_parse_each(v, 1, na->ndim, &q[j]);
443
446
  j++;
444
447
  }
445
448
  // other dimension
@@ -465,6 +468,25 @@ static void na_get_strides_nadata(const narray_data_t* na, ssize_t* strides, ssi
465
468
  }
466
469
  }
467
470
 
471
+ // A new axis holds one element, so any stride addresses it. Give it the size of
472
+ // the block below it, to keep the chain na_check_ladder() walks: a break there
473
+ // costs contiguous?, and na_flatten_dim() takes the last dimension's stride as
474
+ // the flat one on the strength of that same chain.
475
+ static void na_index_set_newaxis_strides(
476
+ narray_view_t* na2, const int* newaxis, int n_newaxes, int ndim_new
477
+ ) {
478
+ int i, j;
479
+
480
+ for (i = n_newaxes - 1; i >= 0; i--) {
481
+ j = newaxis[i];
482
+ if (j + 1 < ndim_new && SDX_IS_STRIDE(na2->stridx[j + 1])) {
483
+ SDX_SET_STRIDE(
484
+ na2->stridx[j], SDX_GET_STRIDE(na2->stridx[j + 1]) * (ssize_t)na2->base.shape[j + 1]
485
+ );
486
+ }
487
+ }
488
+ }
489
+
468
490
  static void na_index_aref_nadata(
469
491
  narray_data_t* na1, narray_view_t* na2, na_index_arg_t* q, ssize_t elmsz, int ndim,
470
492
  int keep_dim
@@ -475,8 +497,11 @@ static void na_index_aref_nadata(
475
497
  ssize_t* strides_na1;
476
498
  size_t* index;
477
499
  ssize_t beg, step;
500
+ int* newaxis;
501
+ int n_newaxes = 0;
478
502
  VALUE m;
479
503
 
504
+ newaxis = ALLOCA_N(int, ndim);
480
505
  strides_na1 = ALLOCA_N(ssize_t, na1->base.ndim);
481
506
  na_get_strides_nadata(na1, strides_na1, elmsz);
482
507
 
@@ -501,8 +526,9 @@ static void na_index_aref_nadata(
501
526
  na2->base.reduce = rb_funcall(m, '|', 1, na2->base.reduce);
502
527
  }
503
528
 
504
- if (qi_orig_dim >= na1->base.ndim) {
529
+ if (qi_orig_dim >= na1->base.ndim && q[i].idx == NULL) {
505
530
  // new dimension
531
+ newaxis[n_newaxes++] = j;
506
532
  SDX_SET_STRIDE(na2->stridx[j], elmsz);
507
533
  } else if (q[i].idx != NULL) {
508
534
  // array index
@@ -521,6 +547,7 @@ static void na_index_aref_nadata(
521
547
  j++;
522
548
  total *= size;
523
549
  }
550
+ na_index_set_newaxis_strides(na2, newaxis, n_newaxes, j);
524
551
  na2->base.size = total;
525
552
  }
526
553
 
@@ -530,11 +557,14 @@ static void na_index_aref_naview(
530
557
  ) {
531
558
  int i, j;
532
559
  ssize_t total = 1;
560
+ int* newaxis;
561
+ int n_newaxes = 0;
562
+
563
+ newaxis = ALLOCA_N(int, ndim);
533
564
 
534
565
  for (i = j = 0; i < ndim; i++) {
535
566
  stridx_t sdx1;
536
- sdx1.stride = 0;
537
- sdx1.index = NULL;
567
+ SDX_SET_STRIDE(sdx1, 0);
538
568
  const int qi_orig_dim = q[i].orig_dim;
539
569
  if (qi_orig_dim < na1->base.ndim) {
540
570
  sdx1 = na1->stridx[qi_orig_dim];
@@ -558,8 +588,9 @@ static void na_index_aref_naview(
558
588
  na2->base.reduce = rb_funcall(m, '|', 1, na2->base.reduce);
559
589
  }
560
590
 
561
- if (qi_orig_dim >= na1->base.ndim) {
591
+ if (qi_orig_dim >= na1->base.ndim && q[i].idx == NULL) {
562
592
  // new dimension
593
+ newaxis[n_newaxes++] = j;
563
594
  SDX_SET_STRIDE(na2->stridx[j], elmsz);
564
595
  } else if (q[i].idx != NULL && SDX_IS_INDEX(sdx1)) {
565
596
  // index <- index
@@ -618,6 +649,7 @@ static void na_index_aref_naview(
618
649
  j++;
619
650
  total *= size;
620
651
  }
652
+ na_index_set_newaxis_strides(na2, newaxis, n_newaxes, j);
621
653
  na2->base.size = total;
622
654
  }
623
655
 
@@ -838,7 +870,10 @@ int na_get_result_dimension(
838
870
  }
839
871
 
840
872
  if (j != argc) {
841
- return check_index_count(argc, na->ndim, count_new, count_rest);
873
+ int nd = check_index_count(argc, na->ndim, count_new, count_rest);
874
+ if (nd != 0) {
875
+ return nd;
876
+ }
842
877
  }
843
878
 
844
879
  switch (na->type) {
@@ -275,7 +275,18 @@ void na_array_to_internal_shape(VALUE self, VALUE ary, size_t* shape) {
275
275
  }
276
276
  }
277
277
 
278
+ static void na_check_ndim(int ndim) {
279
+ if (ndim < 0) {
280
+ rb_raise(nary_eDimensionError, "ndim=%d is negative", ndim);
281
+ }
282
+ if (ndim > NA_MAX_DIMENSION) {
283
+ rb_raise(nary_eDimensionError, "ndim=%d is too many", ndim);
284
+ }
285
+ }
286
+
278
287
  void na_alloc_shape(narray_t* na, int ndim) {
288
+ na_check_ndim(ndim);
289
+
279
290
  na->ndim = ndim;
280
291
  na->size = 0;
281
292
  if (na->shape != NULL && na->shape != &(na->size)) {
@@ -288,12 +299,6 @@ void na_alloc_shape(narray_t* na, int ndim) {
288
299
  na->shape = &(na->size);
289
300
  break;
290
301
  default:
291
- if (ndim < 0) {
292
- rb_raise(nary_eDimensionError, "ndim=%d is negative", ndim);
293
- }
294
- if (ndim > NA_MAX_DIMENSION) {
295
- rb_raise(nary_eDimensionError, "ndim=%d is too many", ndim);
296
- }
297
302
  na->shape = ALLOC_N(size_t, ndim);
298
303
  }
299
304
  }
@@ -302,19 +307,23 @@ void na_setup_shape(narray_t* na, int ndim, size_t* shape) {
302
307
  int i;
303
308
  size_t size;
304
309
 
310
+ na_check_ndim(ndim);
311
+
312
+ for (i = 0, size = 1; i < ndim; i++) {
313
+ if (shape[i] != 0 && size > SIZE_MAX / shape[i]) {
314
+ rb_raise(rb_eRangeError, "total number of elements is too large");
315
+ }
316
+ size *= shape[i];
317
+ }
318
+
305
319
  na_alloc_shape(na, ndim);
306
320
 
307
- if (ndim == 0) {
308
- na->size = 1;
309
- } else if (ndim == 1) {
310
- na->size = shape[0];
311
- } else {
312
- for (i = 0, size = 1; i < ndim; i++) {
321
+ if (ndim > 1) {
322
+ for (i = 0; i < ndim; i++) {
313
323
  na->shape[i] = shape[i];
314
- size *= shape[i];
315
324
  }
316
- na->size = size;
317
325
  }
326
+ na->size = (ndim == 0) ? 1 : size;
318
327
  }
319
328
 
320
329
  static void na_setup(VALUE self, int ndim, size_t* shape) {
@@ -772,7 +781,9 @@ static VALUE na_free(VALUE self) {
772
781
  ptr = NA_DATA_PTR(na);
773
782
  if (ptr != NULL) {
774
783
  NA_DATA_PTR(na) = NULL;
775
- xfree(ptr);
784
+ if (NA_DATA_OWNED(na)) {
785
+ xfree(ptr);
786
+ }
776
787
  }
777
788
  break;
778
789
  case NARRAY_VIEW_T:
@@ -876,6 +887,9 @@ na_check_ladder(VALUE self, int start_dim) {
876
887
  narray_t* na;
877
888
  GetNArray(self, na);
878
889
 
890
+ if (na->ndim == 0) {
891
+ return Qtrue;
892
+ }
879
893
  if (start_dim < -na->ndim || start_dim >= na->ndim) {
880
894
  rb_bug("start_dim (%d) out of range", start_dim);
881
895
  }
@@ -920,6 +934,9 @@ na_check_contiguous(VALUE self) {
920
934
  if (NA_VIEW_STRIDX(na) == 0) {
921
935
  return Qtrue;
922
936
  }
937
+ if (NA_NDIM(na) == 0) {
938
+ return Qtrue;
939
+ }
923
940
  if (na_check_ladder(self, 0) == Qtrue) {
924
941
  elmsz = nary_element_stride(self);
925
942
  if (elmsz == NA_STRIDE_AT(na, NA_NDIM(na) - 1)) {
@@ -1043,6 +1060,7 @@ static VALUE na_expand_dims(VALUE self, VALUE vdim) {
1043
1060
 
1044
1061
  GetNArray(self, na);
1045
1062
  nd = na->ndim;
1063
+ na_check_ndim(nd + 1);
1046
1064
 
1047
1065
  dim = NUM2INT(vdim);
1048
1066
  if (dim < -nd - 1 || dim > nd) {
@@ -1272,7 +1290,11 @@ static VALUE nary_s_from_binary(int argc, VALUE* argv, VALUE type) {
1272
1290
  rb_raise(rb_eArgError, "second argument must be size or shape");
1273
1291
  }
1274
1292
  if (FIXNUM_P(velmsz)) {
1275
- byte_size = len * NUM2SIZET(velmsz);
1293
+ size_t elmsz = NUM2SIZET(velmsz);
1294
+ if (elmsz != 0 && len > SIZE_MAX / elmsz) {
1295
+ rb_raise(rb_eArgError, "specified size is too large");
1296
+ }
1297
+ byte_size = len * elmsz;
1276
1298
  } else {
1277
1299
  byte_size = ceil(len * NUM2DBL(velmsz));
1278
1300
  }
@@ -1315,7 +1337,7 @@ static VALUE nary_s_from_binary(int argc, VALUE* argv, VALUE type) {
1315
1337
  @return [Integer] stored length.
1316
1338
  */
1317
1339
  static VALUE nary_store_binary(int argc, VALUE* argv, VALUE self) {
1318
- size_t size, str_len, byte_size, offset;
1340
+ size_t size, str_len, byte_size, offset, view_offset = 0;
1319
1341
  int narg;
1320
1342
  VALUE vstr, voffset;
1321
1343
  VALUE velmsz;
@@ -1338,7 +1360,11 @@ static VALUE nary_store_binary(int argc, VALUE* argv, VALUE self) {
1338
1360
  size = NA_SIZE(na);
1339
1361
  velmsz = rb_const_get(rb_obj_class(self), id_element_byte_size);
1340
1362
  if (FIXNUM_P(velmsz)) {
1341
- byte_size = size * NUM2SIZET(velmsz);
1363
+ size_t elmsz = NUM2SIZET(velmsz);
1364
+ if (elmsz != 0 && size > SIZE_MAX / elmsz) {
1365
+ rb_raise(rb_eArgError, "string is too short to store");
1366
+ }
1367
+ byte_size = size * elmsz;
1342
1368
  } else {
1343
1369
  byte_size = ceil(size * NUM2DBL(velmsz));
1344
1370
  }
@@ -1346,12 +1372,34 @@ static VALUE nary_store_binary(int argc, VALUE* argv, VALUE self) {
1346
1372
  rb_raise(rb_eArgError, "string is too short to store");
1347
1373
  }
1348
1374
 
1349
- if (OBJ_FROZEN(vstr)) {
1375
+ if (na->type == NARRAY_VIEW_T) {
1376
+ narray_t* nab;
1377
+ size_t data_byte_size;
1378
+
1379
+ if (na_check_contiguous(self) == Qfalse) {
1380
+ rb_raise(rb_eStandardError, "cannot store binary data to non-contiguous NArray");
1381
+ }
1382
+ view_offset = NA_VIEW_OFFSET(na);
1383
+ if (!FIXNUM_P(velmsz) && view_offset != 0) {
1384
+ rb_raise(rb_eStandardError, "cannot store binary data to NArray view offset in bits");
1385
+ }
1386
+ GetNArray(NA_VIEW_DATA(na), nab);
1387
+ if (FIXNUM_P(velmsz)) {
1388
+ data_byte_size = NA_SIZE(nab) * NUM2SIZET(velmsz);
1389
+ } else {
1390
+ data_byte_size = ceil(NA_SIZE(nab) * NUM2DBL(velmsz));
1391
+ }
1392
+ if (view_offset > data_byte_size || byte_size > data_byte_size - view_offset) {
1393
+ rb_raise(rb_eArgError, "string is too long to store");
1394
+ }
1395
+ }
1396
+
1397
+ if (OBJ_FROZEN(vstr) && na->type != NARRAY_VIEW_T) {
1350
1398
  na_set_pointer(self, RSTRING_PTR(vstr) + offset, byte_size);
1351
1399
  rb_ivar_set(self, id_source, vstr);
1352
1400
  } else {
1353
- void* ptr = na_get_pointer_for_write(self);
1354
- memcpy(ptr, RSTRING_PTR(vstr) + offset, byte_size);
1401
+ char* ptr = na_get_pointer_for_write(self);
1402
+ memcpy(ptr + view_offset, RSTRING_PTR(vstr) + offset, byte_size);
1355
1403
  }
1356
1404
 
1357
1405
  return SIZET2NUM(byte_size);
@@ -1367,10 +1415,12 @@ static VALUE nary_to_binary(VALUE self) {
1367
1415
  char* ptr;
1368
1416
  VALUE str;
1369
1417
  narray_t* na;
1418
+ int offset_in_bits;
1370
1419
 
1371
1420
  GetNArray(self, na);
1372
1421
  if (na->type == NARRAY_VIEW_T) {
1373
- if (na_check_contiguous(self) == Qtrue) {
1422
+ offset_in_bits = RTEST(rb_obj_is_kind_of(self, numo_cBit)) && NA_VIEW_OFFSET(na) != 0;
1423
+ if (!offset_in_bits && na_check_contiguous(self) == Qtrue) {
1374
1424
  offset = NA_VIEW_OFFSET(na);
1375
1425
  } else {
1376
1426
  self = rb_funcall(self, id_dup, 0);
@@ -1379,6 +1429,15 @@ static VALUE nary_to_binary(VALUE self) {
1379
1429
  len = NUM2SIZET(nary_byte_size(self));
1380
1430
  ptr = na_get_pointer_for_read(self);
1381
1431
  str = rb_usascii_str_new(ptr + offset, len);
1432
+ // The last byte of a Bit array also carries the bits past its end. They hold
1433
+ // uninitialized memory, the input of from_binary, or the base of a view.
1434
+ // Count them on self, the same array that len came from.
1435
+ if (len > 0 && RTEST(rb_obj_is_kind_of(self, numo_cBit))) {
1436
+ GetNArray(self, na);
1437
+ if (na->size % 8 != 0) {
1438
+ RSTRING_PTR(str)[len - 1] &= (char)((1u << (na->size % 8)) - 1);
1439
+ }
1440
+ }
1382
1441
  RB_GC_GUARD(self);
1383
1442
  return str;
1384
1443
  }
@@ -1402,7 +1461,7 @@ static VALUE nary_marshal_dump(VALUE self) {
1402
1461
  GetNArray(self, na);
1403
1462
  if (na->type == NARRAY_VIEW_T) {
1404
1463
  if (na_check_contiguous(self) == Qtrue) {
1405
- offset = NA_VIEW_OFFSET(na);
1464
+ offset = NA_VIEW_OFFSET(na) / sizeof(VALUE);
1406
1465
  } else {
1407
1466
  self = rb_funcall(self, id_dup, 0);
1408
1467
  }
@@ -1426,6 +1485,9 @@ static VALUE na_inplace(VALUE self);
1426
1485
  static VALUE nary_marshal_load(VALUE self, VALUE a) {
1427
1486
  VALUE v;
1428
1487
 
1488
+ if (OBJ_FROZEN(self)) {
1489
+ rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
1490
+ }
1429
1491
  if (TYPE(a) != T_ARRAY) {
1430
1492
  rb_raise(rb_eArgError, "marshal argument should be array");
1431
1493
  }
@@ -1440,6 +1502,9 @@ static VALUE nary_marshal_load(VALUE self, VALUE a) {
1440
1502
  NUM2INT(RARRAY_AREF(a, 0))
1441
1503
  );
1442
1504
  }
1505
+ if (TYPE(RARRAY_AREF(a, 1)) != T_ARRAY) {
1506
+ rb_raise(rb_eArgError, "marshal shape should be array");
1507
+ }
1443
1508
  na_initialize(self, RARRAY_AREF(a, 1));
1444
1509
  NA_FL0_SET(self, FIX2INT(RARRAY_AREF(a, 2)));
1445
1510
  v = RARRAY_AREF(a, 3);
@@ -1456,6 +1521,7 @@ static VALUE nary_marshal_load(VALUE self, VALUE a) {
1456
1521
  ptr = na_get_pointer_for_write(self);
1457
1522
  memcpy(ptr, RARRAY_PTR(v), NA_SIZE(na) * sizeof(VALUE));
1458
1523
  } else {
1524
+ Check_Type(v, T_STRING);
1459
1525
  rb_str_freeze(v);
1460
1526
  nary_store_binary(1, &v, self);
1461
1527
  if (TEST_BYTE_SWAPPED(self)) {