Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

""" 

Library interface to Embeddable Common Lisp (ECL) 

""" 

#***************************************************************************** 

# Copyright (C) 2009 Nils Bruin <nbruin@sfu.ca> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function, absolute_import 

  

#This version of the library interface prefers to convert ECL integers and 

#rationals to SAGE types Integer and Rational. These parts could easily be 

#adapted to work with pure Python types. 

  

from libc.stdlib cimport abort 

from libc.signal cimport SIGINT, SIGBUS, SIGSEGV, SIGCHLD 

from libc.signal cimport raise_ as signal_raise 

from posix.signal cimport sigaction, sigaction_t 

cimport cysignals.signals 

  

from sage.libs.gmp.types cimport mpz_t 

from sage.misc.misc import ECL_TMP 

from sage.cpython.string cimport str_to_bytes, char_to_str 

from sage.rings.integer cimport Integer 

from sage.rings.rational cimport Rational 

from cpython.object cimport Py_EQ, Py_NE 

  

#it would be preferrable to let bint_symbolp wrap an efficient macro 

#but the macro provided in object.h doesn't seem to work 

cdef bint bint_symbolp(cl_object obj): 

return not(cl_symbolp(obj) == Cnil) 

  

#these type predicates are only provided in "cl_*" form, so we wrap them 

#with the proper type cast. 

  

cdef bint bint_numberp(cl_object obj): 

return not(cl_numberp(obj) == Cnil) 

cdef bint bint_integerp(cl_object obj): 

return not(cl_integerp(obj) == Cnil) 

cdef bint bint_rationalp(cl_object obj): 

return not(cl_rationalp(obj) == Cnil) 

  

cdef extern from "eclsig.h": 

int ecl_sig_on() except 0 

void ecl_sig_off() 

cdef sigaction_t ecl_sigint_handler 

cdef sigaction_t ecl_sigbus_handler 

cdef sigaction_t ecl_sigsegv_handler 

cdef mpz_t ecl_mpz_from_bignum(cl_object obj) 

cdef cl_object ecl_bignum_from_mpz(mpz_t num) 

  

cdef cl_object string_to_object(char * s): 

return ecl_read_from_cstring(s) 

  

# We need to keep a list of objects bound to python, to protect them from being 

# garbage collected. We want a list in which we can quickly add and remove 

# elements. Lookup is not necessary. A doubly linked list seems 

# most appropriate. A node looks like 

# N = ( value next . prev) 

# so that car(N)=value, cadr(N)=next, cddr(N)=prev. 

# we write routines to insert a node after a given node 

# and to delete a given node. This can all be done with modifying pointers. 

# note that circular structures are unpleasant for most lisp routines. 

# perhaps this even puts a strain on the garbage collector? 

# an alternative data structure would be an array where the free nodes get 

# chained in a "free list" for quick allocation (and if the free list is empty 

# upon allocating a node, the array needs to be extended) 

  

cdef cl_object insert_node_after(cl_object node,cl_object value): 

cdef cl_object next,newnode 

  

next=cl_cadr(node) 

newnode=cl_cons(value,cl_cons(next,node)) 

cl_rplaca(cl_cdr(node),newnode) 

if next != Cnil: 

cl_rplacd(cl_cdr(next),newnode) 

return newnode 

  

cdef void remove_node(cl_object node): 

cdef cl_object next, prev 

next=cl_cadr(node) 

prev=cl_cddr(node) 

if next != Cnil: 

cl_rplacd(cl_cdr(next),prev) 

if prev != Cnil: 

cl_rplaca(cl_cdr(prev),next) 

  

# our global list of pointers. This will be a pointer to a sentinel node, 

# after which all new nodes can be inserted. list_of_object gets initialised 

# by init_ecl() and bound to the global ECL variable *SAGE-LIST-OF-OBJECTS* 

  

cdef cl_object list_of_objects 

  

cdef cl_object safe_eval_clobj #our own error catching eval 

cdef cl_object safe_apply_clobj #our own error catching apply 

cdef cl_object safe_funcall_clobj #our own error catching funcall 

cdef cl_object read_from_string_clobj #our own error catching reader 

  

cdef bint ecl_has_booted = 0 

  

# ECL signal handling 

  

def test_sigint_before_ecl_sig_on(): 

""" 

TESTS: 

  

If an interrupt arrives *before* ecl_sig_on(), we should get an 

ordinary KeyboardInterrupt:: 

  

sage: from sage.libs.ecl import test_sigint_before_ecl_sig_on 

sage: test_sigint_before_ecl_sig_on() 

Traceback (most recent call last): 

... 

KeyboardInterrupt 

""" 

# Raise a SIGINT *now*. Since we are outside of sig_on() at this 

# point, this SIGINT will not be seen yet. 

signal_raise(SIGINT) 

# An ordinary KeyboardInterrupt should be raised by ecl_sig_on() 

# since ecl_sig_on() calls sig_on() before anything else. This 

# will catch the pending SIGINT. 

ecl_sig_on() 

# We should never get here. 

abort() 

  

def test_ecl_options(): 

""" 

Print an overview of the ECL options 

  

TESTS:: 

  

sage: from sage.libs.ecl import test_ecl_options 

sage: test_ecl_options() 

ECL_OPT_INCREMENTAL_GC = 0 

ECL_OPT_TRAP_SIGSEGV = 1 

ECL_OPT_TRAP_SIGFPE = 1 

ECL_OPT_TRAP_SIGINT = 1 

ECL_OPT_TRAP_SIGILL = 1 

ECL_OPT_TRAP_SIGBUS = 1 

ECL_OPT_TRAP_SIGCHLD = 0 

ECL_OPT_TRAP_SIGPIPE = 1 

ECL_OPT_TRAP_INTERRUPT_SIGNAL = 1 

ECL_OPT_SIGNAL_HANDLING_THREAD = 0 

ECL_OPT_SIGNAL_QUEUE_SIZE = 16 

ECL_OPT_BOOTED = 1 

ECL_OPT_BIND_STACK_SIZE = ... 

ECL_OPT_BIND_STACK_SAFETY_AREA = ... 

ECL_OPT_FRAME_STACK_SIZE = ... 

ECL_OPT_FRAME_STACK_SAFETY_AREA = ... 

ECL_OPT_LISP_STACK_SIZE = ... 

ECL_OPT_LISP_STACK_SAFETY_AREA = ... 

ECL_OPT_C_STACK_SIZE = ... 

ECL_OPT_C_STACK_SAFETY_AREA = ... 

ECL_OPT_SIGALTSTACK_SIZE = 1 

ECL_OPT_HEAP_SIZE = ... 

ECL_OPT_HEAP_SAFETY_AREA = ... 

ECL_OPT_THREAD_INTERRUPT_SIGNAL = 0 

ECL_OPT_SET_GMP_MEMORY_FUNCTIONS = 0 

""" 

print('ECL_OPT_INCREMENTAL_GC = {0}'.format( 

ecl_get_option(ECL_OPT_INCREMENTAL_GC))) 

print('ECL_OPT_TRAP_SIGSEGV = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGSEGV))) 

print('ECL_OPT_TRAP_SIGFPE = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGFPE))) 

print('ECL_OPT_TRAP_SIGINT = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGINT))) 

print('ECL_OPT_TRAP_SIGILL = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGILL))) 

print('ECL_OPT_TRAP_SIGBUS = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGBUS))) 

print('ECL_OPT_TRAP_SIGCHLD = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGCHLD))) 

print('ECL_OPT_TRAP_SIGPIPE = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_SIGPIPE))) 

print('ECL_OPT_TRAP_INTERRUPT_SIGNAL = {0}'.format( 

ecl_get_option(ECL_OPT_TRAP_INTERRUPT_SIGNAL))) 

print('ECL_OPT_SIGNAL_HANDLING_THREAD = {0}'.format( 

ecl_get_option(ECL_OPT_SIGNAL_HANDLING_THREAD))) 

print('ECL_OPT_SIGNAL_QUEUE_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_SIGNAL_QUEUE_SIZE))) 

print('ECL_OPT_BOOTED = {0}'.format( 

ecl_get_option(ECL_OPT_BOOTED))) 

print('ECL_OPT_BIND_STACK_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_BIND_STACK_SIZE))) 

print('ECL_OPT_BIND_STACK_SAFETY_AREA = {0}'.format( 

ecl_get_option(ECL_OPT_BIND_STACK_SAFETY_AREA))) 

print('ECL_OPT_FRAME_STACK_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_FRAME_STACK_SIZE))) 

print('ECL_OPT_FRAME_STACK_SAFETY_AREA = {0}'.format( 

ecl_get_option(ECL_OPT_FRAME_STACK_SAFETY_AREA))) 

print('ECL_OPT_LISP_STACK_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_LISP_STACK_SIZE))) 

print('ECL_OPT_LISP_STACK_SAFETY_AREA = {0}'.format( 

ecl_get_option(ECL_OPT_LISP_STACK_SAFETY_AREA))) 

print('ECL_OPT_C_STACK_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_C_STACK_SIZE))) 

print('ECL_OPT_C_STACK_SAFETY_AREA = {0}'.format( 

ecl_get_option(ECL_OPT_C_STACK_SAFETY_AREA))) 

print('ECL_OPT_SIGALTSTACK_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_SIGALTSTACK_SIZE))) 

print('ECL_OPT_HEAP_SIZE = {0}'.format( 

ecl_get_option(ECL_OPT_HEAP_SIZE))) 

print('ECL_OPT_HEAP_SAFETY_AREA = {0}'.format( 

ecl_get_option(ECL_OPT_HEAP_SAFETY_AREA))) 

print('ECL_OPT_THREAD_INTERRUPT_SIGNAL = {0}'.format( 

ecl_get_option(ECL_OPT_THREAD_INTERRUPT_SIGNAL))) 

print('ECL_OPT_SET_GMP_MEMORY_FUNCTIONS = {0}'.format( 

ecl_get_option(ECL_OPT_SET_GMP_MEMORY_FUNCTIONS))) 

  

def init_ecl(): 

r""" 

Internal function to initialize ecl. Do not call. 

  

This function initializes the ECL library for use within Python. 

This routine should only be called once and importing the ecl library 

interface already does that, so do not call this yourself. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

  

At this point, init_ecl() has run. Explicitly executing it 

gives an error:: 

  

sage: init_ecl() 

Traceback (most recent call last): 

... 

RuntimeError: ECL is already initialized 

""" 

global list_of_objects 

global safe_eval_clobj 

global safe_apply_clobj 

global safe_funcall_clobj 

global read_from_string_clobj 

global ecl_has_booted 

cdef char *argv[1] 

cdef sigaction_t sage_action[32] 

cdef int i 

  

if ecl_has_booted: 

raise RuntimeError("ECL is already initialized") 

  

# we need it to stop handling SIGCHLD 

ecl_set_option(ECL_OPT_TRAP_SIGCHLD, 0); 

  

#we keep our own GMP memory functions. ECL should not claim them 

ecl_set_option(ECL_OPT_SET_GMP_MEMORY_FUNCTIONS,0); 

  

#we need a dummy argv for cl_boot (we just don't give any parameters) 

argv[0]="sage" 

  

#get all the signal handlers before initializing Sage so we can 

#put them back afterwards. 

for i in range(1,32): 

sigaction(i, NULL, &sage_action[i]) 

  

#initialize ECL 

ecl_set_option(ECL_OPT_SIGNAL_HANDLING_THREAD, 0) 

cl_boot(1, argv) 

  

#save signal handler from ECL 

sigaction(SIGINT, NULL, &ecl_sigint_handler) 

sigaction(SIGBUS, NULL, &ecl_sigbus_handler) 

sigaction(SIGSEGV, NULL, &ecl_sigsegv_handler) 

  

#verify that no SIGCHLD handler was installed 

cdef sigaction_t sig_test 

sigaction(SIGCHLD, NULL, &sig_test) 

assert sage_action[SIGCHLD].sa_handler == NULL # Sage does not set SIGCHLD handler 

assert sig_test.sa_handler == NULL # And ECL bootup did not set one  

  

#and put the Sage signal handlers back 

for i in range(1,32): 

sigaction(i, &sage_action[i], NULL) 

  

#initialise list of objects and bind to global variable 

# *SAGE-LIST-OF-OBJECTS* to make it rooted in the reachable tree for the GC 

list_of_objects=cl_cons(Cnil,cl_cons(Cnil,Cnil)) 

cl_set(string_to_object(b"*SAGE-LIST-OF-OBJECTS*"), list_of_objects) 

  

cl_eval(string_to_object(b""" 

(setf (logical-pathname-translations "TMP") 

'(("**;*.*" "%s/**/*.*"))) 

""" % str_to_bytes(str(ECL_TMP)))) 

  

# We define our own error catching eval, apply and funcall/ 

# Presently these routines are only converted to byte-code. If they 

# ever turn out to be a bottle neck, it should be easy to properly 

# compile them. 

  

read_from_string_clobj=cl_eval(string_to_object(b"(symbol-function 'read-from-string)")) 

  

cl_eval(string_to_object(b""" 

(defun sage-safe-eval (form) 

(handler-case 

(values (eval form)) 

(serious-condition (cnd) 

(values nil (princ-to-string cnd))))) 

""")) 

safe_eval_clobj=cl_eval(string_to_object(b"(symbol-function 'sage-safe-eval)")) 

  

cl_eval(string_to_object(b""" 

(defun sage-safe-apply (func args) 

(handler-case 

(values (apply func args)) 

(serious-condition (cnd) 

(values nil (princ-to-string cnd))))) 

""")) 

  

safe_apply_clobj=cl_eval(string_to_object(b"(symbol-function 'sage-safe-apply)")) 

cl_eval(string_to_object(b""" 

(defun sage-safe-funcall (func arg) 

(handler-case 

(values (funcall func arg)) 

(serious-condition (cnd) 

(values nil (princ-to-string cnd))))) 

""")) 

safe_funcall_clobj=cl_eval(string_to_object(b"(symbol-function 'sage-safe-funcall)")) 

  

ecl_has_booted = 1 

  

cdef cl_object ecl_safe_eval(cl_object form) except NULL: 

""" 

TESTS: 

  

Test interrupts:: 

  

sage: from sage.libs.ecl import * 

sage: from cysignals.tests import interrupt_after_delay 

sage: ecl_eval("(setf i 0)") 

<ECL: 0> 

sage: inf_loop = ecl_eval("(defun infinite() (loop (incf i)))") 

sage: interrupt_after_delay(1000) 

sage: inf_loop() 

Traceback (most recent call last): 

... 

RuntimeError: ECL says: Console interrupt. 

""" 

cdef cl_object s 

ecl_sig_on() 

cl_funcall(2,safe_eval_clobj,form) 

ecl_sig_off() 

  

if ecl_nvalues > 1: 

s = si_coerce_to_base_string(ecl_values(1)) 

raise RuntimeError("ECL says: {}".format( 

char_to_str(ecl_base_string_pointer_safe(s)))) 

else: 

return ecl_values(0) 

  

cdef cl_object ecl_safe_funcall(cl_object func, cl_object arg) except NULL: 

cdef cl_object l, s 

l = cl_cons(func,cl_cons(arg,Cnil)); 

  

ecl_sig_on() 

cl_apply(2,safe_funcall_clobj,cl_cons(func,cl_cons(arg,Cnil))) 

ecl_sig_off() 

  

if ecl_nvalues > 1: 

s = si_coerce_to_base_string(ecl_values(1)) 

raise RuntimeError("ECL says: {}".format( 

char_to_str(ecl_base_string_pointer_safe(s)))) 

else: 

return ecl_values(0) 

  

cdef cl_object ecl_safe_apply(cl_object func, cl_object args) except NULL: 

cdef cl_object s 

ecl_sig_on() 

cl_funcall(3,safe_apply_clobj,func,args) 

ecl_sig_off() 

  

if ecl_nvalues > 1: 

s = si_coerce_to_base_string(ecl_values(1)) 

raise RuntimeError("ECL says: {}".format( 

char_to_str(ecl_base_string_pointer_safe(s)))) 

else: 

return ecl_values(0) 

  

cdef cl_object ecl_safe_read_string(char * s) except NULL: 

cdef cl_object o 

o = ecl_cstring_to_base_string_or_nil(s) 

o = ecl_safe_funcall(read_from_string_clobj,o) 

return o 

  

def shutdown_ecl(): 

r""" 

Shut down ecl. Do not call. 

  

Given the way that ECL is used from python, it is very difficult to ensure 

that no ECL objects exist at a particular time. Hence, destroying ECL is a 

risky proposition. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: shutdown_ecl() 

""" 

cl_shutdown() 

  

#this prints the objects that sage wants the GC to keep track of. 

#these should be all non-immediate EclObject wrapped objects 

def print_objects(): 

r""" 

Print GC-protection list 

  

Diagnostic function. ECL objects that are bound to Python objects need to 

be protected from being garbage collected. We do this by including them 

in a doubly linked list bound to the global ECL symbol 

*SAGE-LIST-OF-OBJECTS*. Only non-immediate values get included, so 

small integers do not get linked in. This routine prints the values 

currently stored. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: a=EclObject("hello") 

sage: b=EclObject(10) 

sage: c=EclObject("world") 

sage: print_objects() #random because previous test runs can have left objects 

NIL 

WORLD 

HELLO 

""" 

  

cdef cl_object c, s 

c = list_of_objects 

while True: 

s = si_coerce_to_base_string(cl_write_to_string(1,cl_car(c))) 

print(char_to_str(ecl_base_string_pointer_safe(s))) 

c = cl_cadr(c) 

if c == Cnil: 

break 

  

cdef cl_object python_to_ecl(pyobj) except NULL: 

# conversion of a python object into an ecl object 

# most conversions are straightforward. Noteworthy are: 

# python lists -> lisp (NIL terminated) lists 

# tuples -> dotted lists 

# strings ->parsed by lisp reader 

  

cdef bytes s 

cdef cl_object L, ptr 

  

if isinstance(pyobj,bool): 

if pyobj: 

return Ct 

else: 

return Cnil 

elif pyobj is None: 

return Cnil 

elif isinstance(pyobj,long): 

if pyobj >= MOST_NEGATIVE_FIXNUM and pyobj <= MOST_POSITIVE_FIXNUM: 

return ecl_make_integer(pyobj) 

else: 

return python_to_ecl(Integer(pyobj)) 

elif isinstance(pyobj,int): 

return ecl_make_integer(pyobj) 

elif isinstance(pyobj,float): 

return ecl_make_doublefloat(pyobj) 

elif isinstance(pyobj,unicode): 

s=str_to_bytes(pyobj) 

return ecl_safe_read_string(s) 

elif isinstance(pyobj,bytes): 

s=<bytes>pyobj 

return ecl_safe_read_string(s) 

elif isinstance(pyobj,Integer): 

if pyobj >= MOST_NEGATIVE_FIXNUM and pyobj <= MOST_POSITIVE_FIXNUM: 

return ecl_make_integer(pyobj) 

else: 

return ecl_bignum_from_mpz( (<Integer>pyobj).value ) 

elif isinstance(pyobj,Rational): 

return ecl_make_ratio( 

python_to_ecl( (<Rational>pyobj).numerator() ), 

python_to_ecl( (<Rational>pyobj).denominator())) 

elif isinstance(pyobj,EclObject): 

return (<EclObject>pyobj).obj 

elif isinstance(pyobj,list): 

if len(pyobj) == 0: 

return Cnil 

else: 

L=cl_cons(python_to_ecl(pyobj[0]),Cnil) 

ptr=L 

for a in pyobj[1:]: 

cl_rplacd(ptr,cl_cons(python_to_ecl(a),Cnil)) 

ptr=cl_cdr(ptr) 

return L 

elif isinstance(pyobj,tuple): 

if len(pyobj) == 0: 

return Cnil 

elif len(pyobj) == 1: 

return python_to_ecl(pyobj[0]) 

else: 

L=cl_cons(python_to_ecl(pyobj[0]),Cnil) 

ptr=L 

for a in pyobj[1:-1]: 

cl_rplacd(ptr,cl_cons(python_to_ecl(a),Cnil)) 

ptr=cl_cdr(ptr) 

cl_rplacd(ptr,python_to_ecl(pyobj[-1])) 

return L 

else: 

raise TypeError("Unimplemented type for python_to_ecl") 

  

cdef ecl_to_python(cl_object o): 

cdef cl_object s 

cdef Integer N 

# conversions from an ecl object to a python object. 

  

if o == Cnil: 

return None 

elif bint_fixnump(o): 

#SAGE specific conversion 

#return ecl_fixint(o) 

return Integer(ecl_fixint(o)) 

elif bint_integerp(o): 

#SAGE specific conversion 

N = Integer.__new__(Integer) 

N.set_from_mpz(ecl_mpz_from_bignum(o)) 

return N 

elif bint_rationalp(o): 

#SAGE specific conversion 

#vanilla python does not have a class to represent rational numbers 

return Rational((ecl_to_python(cl_numerator(o)),ecl_to_python(cl_denominator(o)))) 

elif bint_floatp(o): 

#Python conversion 

#Since SAGE mainly uses mpfr, perhaps "double is not an appropriate return type 

return ecl_to_double(o) 

elif o == Ct: 

return True 

elif bint_consp(o): 

L=[] 

while o != Cnil: 

L.append(ecl_to_python(cl_car(o))) 

o = cl_cdr(o) 

if not(bint_listp(o)): 

L.append(ecl_to_python(o)) 

return tuple(L) 

return L 

else: 

s = si_coerce_to_base_string(cl_write_to_string(1,o)) 

return char_to_str(ecl_base_string_pointer_safe(s)) 

  

#Maxima's BFLOAT multiprecision float type can be read with: 

#def bfloat_to_python(e): 

# prec=Integer(str(e.car().cddr().car())) 

# mant=Integer(str(e.cdr().car())) 

# exp=Integer(str(e.cddr().car())) 

# return 2^(exp-prec)*mant 

  

cdef class EclObject: 

r""" 

Python wrapper of ECL objects 

  

The ``EclObject`` forms a wrapper around ECL objects. The wrapper ensures 

that the data structure pointed to is protected from garbage collection in 

ECL by installing a pointer to it from a global data structure within the 

scope of the ECL garbage collector. This pointer is destroyed upon 

destruction of the EclObject. 

  

EclObject() takes a Python object and tries to find a representation of it 

in Lisp. 

  

EXAMPLES: 

  

Python lists get mapped to LISP lists. None and Boolean values to 

appropriate values in LISP:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([None,true,false]) 

<ECL: (NIL T NIL)> 

  

Numerical values are translated to the appropriate type in LISP:: 

  

sage: EclObject(1) 

<ECL: 1> 

sage: EclObject(10**40) 

<ECL: 10000000000000000000000000000000000000000> 

  

Floats in Python are IEEE double, which LISP has as well. However, 

the printing of floating point types in LISP depends on settings:: 

  

sage: a = EclObject(float(10^40)) 

sage: ecl_eval("(setf *read-default-float-format* 'single-float)") 

<ECL: SINGLE-FLOAT> 

sage: a 

<ECL: 1.d40> 

sage: ecl_eval("(setf *read-default-float-format* 'double-float)") 

<ECL: DOUBLE-FLOAT> 

sage: a 

<ECL: 1.e40> 

  

Tuples are translated to dotted lists:: 

  

sage: EclObject( (false, true)) 

<ECL: (NIL . T)> 

  

Strings are fed to the reader, so a string normally results in a symbol:: 

  

sage: EclObject("Symbol") 

<ECL: SYMBOL> 

  

But with proper quotation one can construct a lisp string object too:: 

  

sage: EclObject('"Symbol"') 

<ECL: "Symbol"> 

  

EclObjects translate to themselves, so one can mix:: 

  

sage: EclObject([1,2,EclObject([3])]) 

<ECL: (1 2 (3))> 

  

Calling an EclObject translates into the appropriate LISP ``apply``, 

where the argument is transformed into an EclObject itself, so one can 

flexibly apply LISP functions:: 

  

sage: car=EclObject("car") 

sage: cdr=EclObject("cdr") 

sage: car(cdr([1,2,3])) 

<ECL: 2> 

  

and even construct and evaluate arbitrary S-expressions:: 

  

sage: eval=EclObject("eval") 

sage: quote=EclObject("quote") 

sage: eval([car, [cdr, [quote,[1,2,3]]]]) 

<ECL: 2> 

  

TESTS: 

  

We check that multiprecision integers are converted correctly:: 

  

sage: i = 10 ^ (10 ^ 5) 

sage: EclObject(i) == EclObject(str(i)) 

True 

sage: EclObject(-i) == EclObject(str(-i)) 

True 

sage: EclObject(i).python() == i 

True 

sage: EclObject(-i).python() == -i 

True 

""" 

cdef cl_object obj #the wrapped object 

cdef cl_object node #linked list pointer: car(node) == obj 

  

cdef void set_obj(EclObject self, cl_object o): 

if self.node: 

remove_node(self.node) 

self.node=NULL 

self.obj=o 

if not(bint_fixnump(o) or bint_characterp(o) or bint_nullp(o)): 

self.node=insert_node_after(list_of_objects,o) 

  

def __init__(self,*args): 

r""" 

Create an EclObject 

  

See EclObject for full documentation. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([None,true,false]) 

<ECL: (NIL T NIL)> 

  

""" 

if len(args) != 0: 

self.set_obj(python_to_ecl(args[0])) 

  

def __reduce__(self): 

r""" 

This is used for pickling. Not implemented 

  

Ecl does not natively support serialization of its objects, so the 

python wrapper class EclObject does not support pickling. There are 

independent efforts for developing serialization for Common Lisp, such as 

CL-STORE. Look at those if you need serialization of ECL objects. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: s=EclObject([1,2,3]) 

sage: s.__reduce__() 

Traceback (most recent call last): 

... 

NotImplementedError: EclObjects do not have a pickling method 

sage: s==loads(dumps(s)) 

Traceback (most recent call last): 

... 

NotImplementedError: EclObjects do not have a pickling method 

""" 

raise NotImplementedError("EclObjects do not have a pickling method") 

  

def python(self): 

r""" 

Convert an EclObject to a python object. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([1,2,("three",'"four"')]) 

sage: L.python() 

[1, 2, ('THREE', '"four"')] 

  

""" 

return ecl_to_python(self.obj) 

  

def __dealloc__(self): 

r""" 

Deallocate EclObject 

  

It is important to remove the GC preventing reference to the object upon 

deletion of the wrapper. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject("symbol") 

sage: del L 

  

""" 

if self.node: 

remove_node(self.node) 

  

def __repr__(self): 

r""" 

Produce a string representation suitable for interactive printing. 

  

Converts the wrapped LISP object to a string, decorated in such a way that 

it can be recognised as a LISP object. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject("symbol") 

sage: repr(L) 

'<ECL: SYMBOL>' 

  

""" 

return "<ECL: "+str(self)+">" 

  

def __str__(self): 

r""" 

Produce a string representation. 

  

Converts the wrapped LISP object to a string and returns that as a Python 

string. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject("symbol") 

sage: str(L) 

'SYMBOL' 

  

""" 

cdef cl_object s 

s = si_coerce_to_base_string(cl_write_to_string(1,self.obj)) 

return char_to_str(ecl_base_string_pointer_safe(s)) 

  

def __hash__(self): 

r""" 

Return a hash value of the object 

  

Returns the hash value returned by SXHASH, which is a routine that is 

specified in Common Lisp. According to the specification, lisp objects that 

are EQUAL have the same SXHASH value. Since two EclObjects are equal if 

their wrapped objects are EQUAL according to lisp, this is compatible with 

Python's concept of hash values. 

  

It is not possible to enforce immutability of lisp objects, so care should 

be taken in using EclObjects as dictionary keys. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([1,2]) 

sage: L 

<ECL: (1 2)> 

sage: hash(L) #random 

463816586 

sage: L.rplacd(EclObject(3)) 

sage: L 

<ECL: (1 . 3)> 

sage: hash(L) #random 

140404060 

  

""" 

return ecl_fixint(cl_sxhash(self.obj)) 

  

def __call__(self, *args): 

r""" 

Apply self to arguments. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: sqr=EclObject("(lambda (x) (* x x))").eval() 

sage: sqr(10) 

<ECL: 100> 

  

""" 

lispargs = EclObject(list(args)) 

return ecl_wrap(ecl_safe_apply(self.obj,(<EclObject>lispargs).obj)) 

  

def __richcmp__(left, right, int op): 

r""" 

Comparison test. 

  

An EclObject is not equal to any non-EclObject. Two EclObjects 

are equal if their wrapped lisp objects are EQUAL. Since LISP 

has no universal ordering, less than and greater than tests 

are not implemented for EclObjects. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: a=EclObject(1) 

sage: b=EclObject(2) 

sage: a==b 

False 

sage: a<b 

Traceback (most recent call last): 

... 

NotImplementedError: EclObjects can only be compared for equality 

sage: EclObject("<")(a,b) 

<ECL: T> 

""" 

if op == Py_EQ: 

if not(isinstance(left,EclObject) and isinstance(right,EclObject)): 

return False 

else: 

return bint_equal((<EclObject>left).obj,(<EclObject>right).obj) 

elif op == Py_NE: 

if not(isinstance(left,EclObject) and isinstance(right,EclObject)): 

return True 

else: 

return not(bint_equal((<EclObject>left).obj,(<EclObject>right).obj)) 

  

#Common lisp only seems to be able to compare numeric and string types 

#and does not have generic routines for doing that. 

#we could dispatch based on type here, but that seems 

#inappropriate for an *interface*. 

raise NotImplementedError("EclObjects can only be compared for equality") 

  

def __iter__(self): 

r""" 

Implements the iterator protocol for EclObject. 

  

EclObject implements the iterator protocol for lists. This means 

one can use an EclObject in the context where an iterator is 

expected (for instance, in a list comprehension or in a for loop). 

The iterator produces EclObjects wrapping the members of the list that 

the original EclObject wraps. 

  

The wrappers returned are all newly constructed but refer to the 

original members of the list iterated over. This is usually what is 

intended but, just as in Python, can cause surprises if the original 

object is changed between calls to the iterator. 

  

Since EclObject translates Python Lists into LISP lists and Python 

tuples into LISP "dotted" lists (lists for which the final CDR is not 

necessarily NIL), and both these python structures are iterable, the 

corresponding EclObjects are iterable as well. 

  

EclObjects that are not lists are not iterable. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: [i for i in EclObject("(1 2 3)")] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

sage: [i for i in EclObject("(1 2 . 3)")] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

sage: [i for i in EclObject("NIL")] 

[] 

  

TESTS: 

  

These show that Python lists and tuples behave as 

described above:: 

  

sage: [i for i in EclObject([1,2,3])] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

sage: [i for i in EclObject((1,2,3))] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

  

This tests that we cannot iterate EclObjects we shouldn't, 

as described above:: 

  

sage: [i for i in EclObject("T")] 

Traceback (most recent call last): 

... 

TypeError: ECL object is not iterable 

  

""" 

return EclListIterator(self) 

  

def eval(self): 

r""" 

Evaluate object as an S-Expression 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: S=EclObject("(+ 1 2)") 

sage: S 

<ECL: (+ 1 2)> 

sage: S.eval() 

<ECL: 3> 

  

""" 

cdef cl_object o 

o=ecl_safe_eval(self.obj) 

if o == NULL: 

raise RuntimeError("ECL runtime error") 

return ecl_wrap(o) 

  

def cons(self,EclObject d): 

r""" 

apply cons to self and argument and return the result. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: a=EclObject(1) 

sage: b=EclObject(2) 

sage: a.cons(b) 

<ECL: (1 . 2)> 

  

""" 

return ecl_wrap(cl_cons(self.obj,d.obj)) 

  

def rplaca(self,EclObject d): 

r""" 

Destructively replace car(self) with d. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject((1,2)) 

sage: L 

<ECL: (1 . 2)> 

sage: a=EclObject(3) 

sage: L.rplaca(a) 

sage: L 

<ECL: (3 . 2)> 

  

""" 

if not(bint_consp(self.obj)): 

raise TypeError("rplaca can only be applied to a cons") 

cl_rplaca(self.obj, d.obj) 

  

  

def rplacd(self,EclObject d): 

r""" 

Destructively replace cdr(self) with d. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject((1,2)) 

sage: L 

<ECL: (1 . 2)> 

sage: a=EclObject(3) 

sage: L.rplacd(a) 

sage: L 

<ECL: (1 . 3)> 

  

""" 

if not(bint_consp(self.obj)): 

raise TypeError("rplacd can only be applied to a cons") 

cl_rplacd(self.obj, d.obj) 

  

def car(self): 

r""" 

Return the car of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj)): 

raise TypeError("car can only be applied to a cons") 

return ecl_wrap(cl_car(self.obj)) 

  

def cdr(self): 

r""" 

Return the cdr of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj)): 

raise TypeError("cdr can only be applied to a cons") 

return ecl_wrap(cl_cdr(self.obj)) 

  

def caar(self): 

r""" 

Return the caar of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj) and bint_consp(cl_car(self.obj))): 

raise TypeError("caar can only be applied to a cons") 

return ecl_wrap(cl_caar(self.obj)) 

  

def cadr(self): 

r""" 

Return the cadr of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj) and bint_consp(cl_cdr(self.obj))): 

raise TypeError("cadr can only be applied to a cons") 

return ecl_wrap(cl_cadr(self.obj)) 

  

def cdar(self): 

r""" 

Return the cdar of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj) and bint_consp(cl_car(self.obj))): 

raise TypeError("cdar can only be applied to a cons") 

return ecl_wrap(cl_cdar(self.obj)) 

  

def cddr(self): 

r""" 

Return the cddr of self 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: L=EclObject([[1,2],[3,4]]) 

sage: L.car() 

<ECL: (1 2)> 

sage: L.cdr() 

<ECL: ((3 4))> 

sage: L.caar() 

<ECL: 1> 

sage: L.cadr() 

<ECL: (3 4)> 

sage: L.cdar() 

<ECL: (2)> 

sage: L.cddr() 

<ECL: NIL> 

""" 

if not(bint_consp(self.obj) and bint_consp(cl_cdr(self.obj))): 

raise TypeError("cddr can only be applied to a cons") 

return ecl_wrap(cl_cddr(self.obj)) 

  

def fixnump(self): 

r""" 

Return True if self is a fixnum, False otherwise 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject(2**3).fixnump() 

True 

sage: EclObject(2**200).fixnump() 

False 

  

""" 

return bint_fixnump(self.obj) 

  

def characterp(self): 

r""" 

Return True if self is a character, False otherwise 

  

Strings are not characters 

  

EXAMPLES: 

  

sage: from sage.libs.ecl import * 

sage: EclObject('"a"').characterp() 

False 

  

""" 

return bint_characterp(self.obj) 

  

def nullp(self): 

r""" 

Return True if self is NIL, False otherwise 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([]).nullp() 

True 

sage: EclObject([[]]).nullp() 

False 

""" 

return bint_nullp(self.obj) 

  

def listp(self): 

r""" 

Return True if self is a list, False otherwise. NIL is a list. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([]).listp() 

True 

sage: EclObject([[]]).listp() 

True 

""" 

return bint_listp(self.obj) 

  

def consp(self): 

r""" 

Return True if self is a cons, False otherwise. NIL is not a cons. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([]).consp() 

False 

sage: EclObject([[]]).consp() 

True 

""" 

return bint_consp(self.obj) 

  

def atomp(self): 

r""" 

Return True if self is atomic, False otherwise. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([]).atomp() 

True 

sage: EclObject([[]]).atomp() 

False 

  

""" 

return bint_atomp(self.obj) 

  

def symbolp(self): 

r""" 

Return True if self is a symbol, False otherwise. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: EclObject([]).symbolp() 

True 

sage: EclObject([[]]).symbolp() 

False 

  

""" 

return bint_symbolp(self.obj) 

  

cdef class EclListIterator: 

r""" 

Iterator object for an ECL list 

  

This class is used to implement the iterator protocol for EclObject. 

Do not instantiate this class directly but use the iterator method 

on an EclObject instead. It is an error if the EclObject is not a list. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: I=EclListIterator(EclObject("(1 2 3)")) 

sage: type(I) 

<type 'sage.libs.ecl.EclListIterator'> 

sage: [i for i in I] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

sage: [i for i in EclObject("(1 2 3)")] 

[<ECL: 1>, <ECL: 2>, <ECL: 3>] 

sage: EclListIterator(EclObject("1")) 

Traceback (most recent call last): 

... 

TypeError: ECL object is not iterable 

  

""" 

cdef EclObject current 

  

def __init__(EclListIterator self, EclObject o): 

r""" 

Initialize EclListIterator 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: I=EclListIterator(EclObject("(1 2 3)")) 

sage: type(I) 

<type 'sage.libs.ecl.EclListIterator'> 

  

""" 

if not o.listp(): 

raise TypeError("ECL object is not iterable") 

self.current = ecl_wrap(o.obj) 

  

def __iter__(EclListIterator self): 

r""" 

Return self 

  

It seems standard that iterators return themselves if asked to produce 

an iterator. 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: I=EclListIterator(EclObject("(1 2 3)")) 

sage: id(I) == id(I.__iter__()) 

True 

  

""" 

return self 

  

def __next__(EclListIterator self): 

r""" 

Get next element from iterator 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: I=EclListIterator(EclObject("(1 2 3)")) 

sage: next(I) 

<ECL: 1> 

sage: next(I) 

<ECL: 2> 

sage: next(I) 

<ECL: 3> 

sage: next(I) 

Traceback (most recent call last): 

... 

StopIteration 

  

""" 

  

if self.current.nullp(): 

raise StopIteration 

elif self.current.consp(): 

r = self.current.car() 

self.current = self.current.cdr() 

else: 

r = self.current 

self.current = ecl_wrap(Cnil) 

return r 

  

#input: a cl-object. Output: EclObject wrapping that. 

cdef EclObject ecl_wrap(cl_object o): 

cdef EclObject obj = EclObject.__new__(EclObject) 

obj.set_obj(o) 

return obj 

  

#convenience routine to more easily evaluate strings 

cpdef EclObject ecl_eval(str s): 

""" 

Read and evaluate string in Lisp and return the result 

  

EXAMPLES:: 

  

sage: from sage.libs.ecl import * 

sage: ecl_eval("(defun fibo (n)(cond((= n 0) 0)((= n 1) 1)(T (+ (fibo (- n 1)) (fibo (- n 2))))))") 

<ECL: FIBO> 

sage: ecl_eval("(mapcar 'fibo '(1 2 3 4 5 6 7))") 

<ECL: (1 1 2 3 5 8 13)> 

  

""" 

cdef cl_object o 

o=ecl_safe_read_string(str_to_bytes(s)) 

o=ecl_safe_eval(o) 

return ecl_wrap(o) 

  

init_ecl()