This commit is contained in:
Vítor Santos Costa 2018-10-19 17:11:11 +01:00
commit 7986e21d59
54 changed files with 586 additions and 357 deletions

View File

@ -3097,7 +3097,7 @@ void Yap_HidePred(PredEntry *pe) {
if (pe->NextOfPE) {
UInt hash = PRED_HASH(pe->FunctorOfPred, CurrentModule, PredHashTableSize);
READ_LOCK(PredHashRWLock);
PredEntry *p, **op = PredHash+hash;
PredEntry *p, **op = PredHash + hash;
p = *op;
while (p) {
@ -3144,7 +3144,7 @@ void Yap_HidePred(PredEntry *pe) {
op = &p->NextPredOfModule;
p = p->NextPredOfModule;
}
pe->NextPredOfModule = NULL;
pe->NextPredOfModule = NULL;
}
}
@ -3168,16 +3168,15 @@ stash_predicate(USES_REGS1) {
/*
char ns[1024];
const char *s = (pe->ModuleOfPred == PROLOG_MODULE ?
"__prolog__stash__" :
snprintf(sn,1023,"__%s__".RepAtom(AtomOfTerm( pe->ModuleOfPred ))));
pe->ModuleOfPred = MkAtomTerm(Yap_LookupAtom(s));
"__prolog__stash__" :
snprintf(sn,1023,"__%s__".RepAtom(AtomOfTerm(
pe->ModuleOfPred )))); pe->ModuleOfPred = MkAtomTerm(Yap_LookupAtom(s));
*/
return true;
} else
return false;
}
static Int /* $hidden_predicate(P) */
hidden_predicate(USES_REGS1) {
PredEntry *pe =

7
CMakeLists.txt Normal file → Executable file
View File

@ -334,9 +334,14 @@ find_package(GMP)
list(APPEND YAP_SYSTEM_OPTIONS big_numbers)
if (GMP_FOUND)
#config.h needs this (TODO: change in code latter)
include_directories(${GMP_INCLUDE_DIRS})
check_include_file(gmp.h HAVE_GMP_H)
check_include_file_cxx(gmpxx.h HAVE_GMPXX_H)
endif (GMP_FOUND)
option(WITH_READLINE "use readline or libedit" ON)
@ -416,7 +421,7 @@ set(DEF_TRAILSPACE 0)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS DEPTH_LIMIT=1;COROUTINING=1;RATIONAL_TREES=1)
# inform we are compiling YAP
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_YAP_NOT_INSTALLED_=1;HAVE_CONFIG_H=1;_GNU_SOURCE")
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "_YAP_NOT_INSTALLED_=1;HAVE_CONFIG_H=1;_GNU_SOURCE=1")
# Compilation model
# target_compile_definitions(libYap PUBLIC _XOPEN_SOURCE=700 )

11
CXX/yapi.hh Normal file → Executable file
View File

@ -8,11 +8,20 @@
#define YAP_CPP_INTERFACE 1
#include <gmpxx.h>
#include <iostream>
#include <string>
#include <vector>
extern "C"{
#include "config.h"
}
#if HAVE_GMPXX_H
#include <gmpxx.h>
#elif HAVE_GMP_H
#include <gmp.h>
#endif
/*!
*
* @ingroup fli_c_cxx

View File

@ -388,26 +388,6 @@ INLINE_ONLY bool IsStringTerm(Term t) {
#include <stdio.h>
#if !defined(__cplusplus)
#include <gmp.h>
#endif
#else
typedef UInt mp_limb_t;
typedef struct {
Int _mp_size, _mp_alloc;
mp_limb_t *_mp_d;
} MP_INT;
typedef struct {
MP_INT _mp_num;
MP_INT _mp_den;
} MP_RAT;
#endif
INLINE_ONLY bool IsBigIntTerm(Term);
INLINE_ONLY bool IsBigIntTerm(Term t) {
@ -415,7 +395,11 @@ INLINE_ONLY bool IsBigIntTerm(Term t) {
FunctorOfTerm(t) == FunctorBigInt;
}
#ifdef USE_GMP
#if !defined(__cplusplus)
#include <gmp.h>
#endif
Term Yap_MkBigIntTerm(MP_INT *);
MP_INT *Yap_BigIntOfTerm(Term);

4
H/YapEval.h Normal file → Executable file
View File

@ -632,13 +632,13 @@ __Yap_Mk64IntegerTerm(YAP_LONG_LONG i USES_REGS) {
}
inline static Term add_int(Int i, Int j USES_REGS) {
#if defined(__clang__) || defined(__GNUC__)
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 4)
Int w;
if (!__builtin_add_overflow(i, j, &w))
RINT(w);
return Yap_gmp_add_ints(i, j);
;
#elif defined(__GNUC__)
#elif defined(__GNUC__) && __GNUC__ > 4
Int w;
if (!__builtin_add_overflow_p(i, j, w))
RINT(w);

View File

@ -1529,6 +1529,11 @@ extern bool Yap_HasException(void);
extern yap_error_descriptor_t *Yap_GetException();
extern void Yap_PrintException(yap_error_descriptor_t *i);
INLINE_ONLY bool Yap_HasException(void) {
extern yap_error_number Yap_MathException__(USES_REGS1);
yap_error_number me;
if ((me = Yap_MathException__(PASS_REGS1)) && LOCAL_ActiveError->errorNo != YAP_NO_ERROR) {
LOCAL_ActiveError->errorNo = me;
}
return LOCAL_ActiveError->errorNo != YAP_NO_ERROR;
}

View File

@ -24,13 +24,13 @@ inline static int sub_overflow(Int x, Int i, Int j) {
}
inline static Term sub_int(Int i, Int j USES_REGS) {
#if defined(__clang__ ) || defined(__GNUC__)
#if defined(__clang__ ) || (defined(__GNUC__) && __GNUC__ > 4)
Int k;
if (__builtin_sub_overflow(i,j,&k)) {
return Yap_gmp_sub_ints(i, j);
}
RINT(k);
#elif defined(__GNUC__)
#elif defined(__GNUC__) && __GNUC__ >4
Int w;
if (!__builtin_sub_overflow_p(i,j,w))
RINT(w);
@ -64,7 +64,7 @@ inline static int mul_overflow(Int z, Int i1, Int i2) {
return (i2 && z / i2 != i1);
}
#if defined(__clang__) || defined(__GNUC__)
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 4)
#define DO_MULTI() \
if (__builtin_mul_overflow(i1, i2, &z)) { \
goto overflow; \

View File

@ -85,6 +85,7 @@ typedef struct vfs {
/// in this space, usual w,r,a,b flags plus B (store in a buffer)
bool (*close)(int sno); /// close the object
int (*get_char)(int sno); /// get an octet from the stream
int (*get_wchar)(int sno); /// get an octet from the stream
int (*peek_char)(int sno); /// unget an octet from the stream
int (*peek_wchar)(int sno); /// unget an octet from the stream
int (*put_char)(int sno, int ch); /// output an octet to the stream

5
os/fmem.c Normal file → Executable file
View File

@ -23,6 +23,9 @@ static char SccsId[] = "%W% %G%";
*
*/
#define _GNU_SOURCE
#include "YapText.h"
#include "format.h"
#include "sysbits.h"
@ -163,7 +166,7 @@ int Yap_open_buf_read_stream(const char *buf, size_t nchars,
f = st->file = fmemopen((void *)buf, nchars, "r");
st->vfs = NULL;
flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
Yap_initStream(sno, f, RepAtom(fname)->StrOfAE, "r", uname, encoding, flags, NULL);
Yap_initStream(sno, f, fname, "r", uname, encoding, flags, NULL);
// like any file stream.
Yap_DefaultStreamOps(st);
UNLOCK(st->streamlock);

View File

@ -342,7 +342,7 @@ void Yap_DefaultStreamOps(StreamDesc *st) {
st->stream_putc = st->vfs->put_char;
st->stream_wputc = st->vfs->put_wchar;
st->stream_getc = st->vfs->get_char;
st->stream_wgetc = st->vfs->get_char;
st->stream_wgetc = st->vfs->get_wchar;
default_peek(st);
return;
} else {
@ -1126,7 +1126,7 @@ static void check_bom(int sno, StreamDesc *st) {
}
}
bool Yap_initStream(int sno, FILE *fd, const char *name, const char *io_mode,
bool Yap_initStream(int sno, FILE *fd, Atom name, const char *io_mode,
Term file_name, encoding_t encoding, stream_flags_t flags,
void *vfs) {
// fprintf(stderr,"+ %s --> %d\n", name, sno);
@ -1228,7 +1228,8 @@ typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t;
static const param_t open_defs[] = {OPEN_DEFS()};
#undef PAR
static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode, Term user_name, encoding_t enc) {
static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
Term user_name, encoding_t enc) {
struct vfs *vfsp = NULL;
const char *fname;
@ -1273,7 +1274,7 @@ static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
if (strchr(io_mode, 'r')) {
return Yap_OpenBufWriteStream(PASS_REGS1);
} else {
int j= push_text_stack();
int j = push_text_stack();
const char *buf;
buf = Yap_TextTermToText(tin PASS_REGS);
@ -1283,11 +1284,10 @@ static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
}
buf = pop_output_text_stack(j, buf);
Atom nat = Yap_LookupAtom(Yap_StrPrefix(buf, 32));
sno = Yap_open_buf_read_stream(buf, strlen(buf) + 1,
&LOCAL_encoding,
sno = Yap_open_buf_read_stream(buf, strlen(buf) + 1, &LOCAL_encoding,
MEM_BUF_MALLOC, nat,
MkAtomTerm(NameOfFunctor(f)));
pop_text_stack(j);
pop_text_stack(j);
return Yap_OpenBufWriteStream(PASS_REGS1);
}
} else if (!strcmp(RepAtom(NameOfFunctor(f))->StrOfAE, "popen")) {
@ -1313,8 +1313,8 @@ static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
if (!strchr(io_mode, 'b') && binary_file(fname)) {
st->status |= Binary_Stream_f;
}
Yap_initStream(sno, st->file, fname, io_mode, user_name, LOCAL_encoding,
st->status, vfsp);
Yap_initStream(sno, st->file, Yap_LookupAtom(fname), io_mode, user_name,
LOCAL_encoding, st->status, vfsp);
return true;
}
@ -1670,7 +1670,7 @@ int Yap_OpenStream(Term tin, const char *io_mode, Term user_name,
return -1;
}
int Yap_FileStream(FILE *fd, char *name, Term file_name, int flags,
int Yap_FileStream(FILE *fd, Atom name, Term file_name, int flags,
VFS_t *vfsp) {
CACHE_REGS
int sno;

2
os/iopreds.h Normal file → Executable file
View File

@ -31,7 +31,7 @@ INLINE_ONLY bool IsStreamTerm(Term t) {
(IsApplTerm(t) && (FunctorOfTerm(t) == FunctorStream)));
}
extern bool Yap_initStream(int sno, FILE *fd, const char *name, const char *io_mode, Term file_name, encoding_t encoding,
extern bool Yap_initStream(int sno, FILE *fd, Atom name, const char *io_mode, Term file_name, encoding_t encoding,
stream_flags_t flags, void *vfs);
#define Yap_CheckStream(arg, kind, msg) \

2
os/mem.c Normal file → Executable file
View File

@ -225,7 +225,7 @@ int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp,
flags = Input_Stream_f | InMemory_Stream_f;
st->vfs = NULL;
st->name = name;
Yap_initStream(sno, f, "Memory Stream","wa", TermNil, encoding, flags, NULL);
Yap_initStream(sno, f, Yap_LookupAtom("Memory Stream"),"wa", TermNil, encoding, flags, NULL);
// like any file stream.
/* currently these streams are not seekable */
st->status = Input_Stream_f | InMemory_Stream_f;

2
os/readline.c Normal file → Executable file
View File

@ -218,7 +218,7 @@ static char **prolog_completion(const char *text, int start, int end) {
} else if (start == 0) {
int i = 0;
const char *p;
while (isblank(text[i++]) && i <= end)
while (isspace(text[i++]) && i <= end)
;
p = text + i;

View File

@ -363,7 +363,6 @@ Atom Yap_guessFileName(FILE *file, int sno, size_t max) {
}
#endif
if (!StreamName(sno)) {
pop_text_stack(i);
return NULL;
}
pop_text_stack(i);

View File

@ -26,8 +26,8 @@
#include <wchar.h>
#include "YapIOConfig.h"
#include <Yatom.h>
#include <VFS.h>
#include <Yatom.h>
#ifndef _PL_WRITE_
@ -51,7 +51,7 @@ typedef struct AliasDescS {
* @return a new VFS that will support /assets
*/
extern struct vfs *Yap_InitAssetManager( void );
extern struct vfs *Yap_InitAssetManager(void);
/* routines in parser.c */
extern VarEntry *Yap_LookupVar(const char *);
@ -85,8 +85,9 @@ extern int Yap_PlGetWchar(void);
extern int Yap_PlFGetchar(void);
extern int Yap_GetCharForSIGINT(void);
extern Int Yap_StreamToFileNo(Term);
extern int Yap_OpenStream(Term tin, const char* io_mode, Term user_name, encoding_t enc);
extern int Yap_FileStream(FILE*, char *, Term, int, VFS_t *);
extern int Yap_OpenStream(Term tin, const char *io_mode, Term user_name,
encoding_t enc);
extern int Yap_FileStream(FILE *, Atom, Term, int, VFS_t *);
extern char *Yap_TermToBuffer(Term t, int flags);
extern char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length,
encoding_t *encoding, int flags);
@ -112,18 +113,19 @@ typedef enum mem_buf_source {
extern char *Yap_MemStreamBuf(int sno);
extern char *Yap_StrPrefix( const char *buf, size_t n) ;
extern char *Yap_StrPrefix(const char *buf, size_t n);
extern Term Yap_StringToNumberTerm(const char *s, encoding_t *encp,
bool error_on);
extern int Yap_FormatFloat(Float f, char **s, size_t sz);
extern int Yap_open_buf_read_stream(const char *buf, size_t nchars,
encoding_t *encp, memBufSource src, Atom name,
Term uname);
encoding_t *encp, memBufSource src,
Atom name, Term uname);
extern int Yap_open_buf_write_stream(encoding_t enc, memBufSource src);
extern Term Yap_BufferToTerm(const char *s, Term opts);
extern X_API Term Yap_BufferToTermWithPrioBindings(const char *s, Term opts, Term bindings, size_t sz,
int prio);
extern Term Yap_BufferToTerm(const char *s, Term opts);
extern X_API Term Yap_BufferToTermWithPrioBindings(const char *s, Term opts,
Term bindings, size_t sz,
int prio);
extern FILE *Yap_GetInputStream(Term t, const char *m);
extern FILE *Yap_GetOutputStream(Term t, const char *m);
extern Atom Yap_guessFileName(FILE *f, int sno, size_t max);
@ -156,36 +158,34 @@ INLINE_ONLY Term MkCharTerm(Int c) {
return MkAtomTerm(Yap_ULookupAtom(cs));
}
extern char *GLOBAL_cwd;
INLINE_ONLY char *Yap_VF(const char *path) {
char *out;
INLINE_ONLY char *Yap_VF(const char *path){
char *out;
out = (char *)malloc(YAP_FILENAME_MAX+1);
if ( GLOBAL_cwd == NULL || GLOBAL_cwd[0] == 0 || !Yap_IsAbsolutePath(path, false)) {
return (char *) path;
}
strcpy(out, GLOBAL_cwd);
strcat(out, "/" );
strcat(out, path);
return out;
out = (char *)malloc(YAP_FILENAME_MAX + 1);
if (GLOBAL_cwd == NULL || GLOBAL_cwd[0] == 0 ||
!Yap_IsAbsolutePath(path, false)) {
return (char *)path;
}
strcpy(out, GLOBAL_cwd);
strcat(out, "/");
strcat(out, path);
return out;
}
INLINE_ONLY char *Yap_VFAlloc(const char *path) {
char *out;
INLINE_ONLY char *Yap_VFAlloc(const char *path){
char *out;
out = (char *)malloc(YAP_FILENAME_MAX+1);
if ( GLOBAL_cwd == NULL || GLOBAL_cwd[0] == 0 || !Yap_IsAbsolutePath(path, false)) {
return (char *) path;
}
strcpy(out, GLOBAL_cwd);
strcat(out, "/" );
strcat(out, path);
return out;
out = (char *)malloc(YAP_FILENAME_MAX + 1);
if (GLOBAL_cwd == NULL || GLOBAL_cwd[0] == 0 ||
!Yap_IsAbsolutePath(path, false)) {
return (char *)path;
}
strcpy(out, GLOBAL_cwd);
strcat(out, "/");
strcat(out, path);
return out;
}
/// UT when yap started
@ -193,6 +193,4 @@ extern uint64_t Yap_StartOfWTimes;
extern bool Yap_HandleSIGINT(void);
#endif

View File

@ -40,13 +40,45 @@ static int py_putc(int sno, int ch) {
return ch;
}
#endif
char s[2];
unsigned char s[2];
PyObject *err;
s[0] = ch;
s[1] = '\0';
term_t g0 = python_acquire_GIL();
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString(s), NULL);
PyUnicode_FromString((char *)s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred())) {
PyErr_SetString(
err,
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
}
return ch;
}
static int py_wputc(int sno, int ch) {
// PyObject *pyw; // buffer
// int pyw_kind;
// PyObject *pyw_data;
StreamDesc *st = YAP_GetStreamFromId(sno);
#if 0
if (false && (st->user_name == TermOutStream || st->user_name == TermErrStream)) {
size_t sz = put_utf8(st->u.w_irl.ptr, ch);
if (sz > 0) {
st->u.w_irl.ptr += sz;
if (ch == '\n' || st->u.w_irl.ptr - st->u.w_irl.buf > 256)
{pyflush(st); }
}
return ch;
}
#endif
unsigned char s[8];
PyObject *err;
size_t n = put_utf8(s, ch);
s[n] = '\0';
term_t g0 = python_acquire_GIL();
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString((char *)s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred())) {
PyErr_SetString(
@ -84,8 +116,6 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
st->u.w_irl.ptr = st->u.w_irl.buf = outbuf;
]\]
st->user_name = TermOutStream;
} else if (strcmp(name, "sys.stderr") == 0) {
@ -127,38 +157,49 @@ static bool py_close(int sno) {
return true;
}
static bool pygetLine(StreamDesc *rl_iostream, int sno) {
// term_t ctk = python_acquire_GIL();
const char *myrl_line;
PyObject *user_line;
PyObject *user_line, *readl = NULL;
PyObject *err;
StreamDesc *s = YAP_GetStreamFromId(sno);
//term_t tg = python_acquire_GIL();
if (!strcmp(RepAtom(s->name)->StrOfAE,"input") ) {
// note that input may change
PyObject *pystream = PyDict_GetItemString( py_Builtin, "input");
if (pystream == NULL) {
PyObject *err;
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
// term_t tg = python_acquire_GIL();
PyObject_Print(s->u.private_data,stderr,0);
if (PyFunction_Check( s->u.private_data )) {
readl = s->u.private_data;
}
user_line = PyObject_CallFunctionObjArgs(pystream, PyUnicode_FromString("?- ") , NULL);
} else {
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
user_line = PyObject_CallFunction(readl, "?- ");
if (!strcmp(RepAtom(s->name)->StrOfAE, "user_input")) {
// note that input may change
readl = PythonLookupSpecial("input");
}
if (readl == NULL) {
readl = PythonLookup("readline", s->u.private_data);
}
if (readl == NULL) {
readl = PythonLookup("read", s->u.private_data);
}
if (readl == NULL) {
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), NULL);
}
}
user_line = PyObject_CallFunctionObjArgs(readl,
NULL);
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
myrl_line = PyUnicode_AsUTF8(user_line);
if (myrl_line == NULL)
return NULL;
PyObject *err;
if ((err = PyErr_Occurred())) {
if (PyErr_GivenExceptionMatches(err, PyExc_EOFError))
return NULL;
PyErr_SetString(err, "Error in getLine\n");
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf = (unsigned char *)myrl_line;
return true;
@ -182,6 +223,29 @@ static int py_getc(int sno) {
return ch;
}
static int py_wgetc(int sno) {
StreamDesc *s = YAP_RepStreamFromId(sno);
int ch;
bool fetch = (s->u.irl.ptr == NULL);
if (fetch) {
if (!pygetLine(s, sno)) {
return EOF;
}
}
if (s->u.irl.ptr == NULL)
return 10; // ????
const unsigned char *ttyptr = s->u.irl.ptr;
if (*ttyptr == '\0') {
ch = 10;
s->u.irl.ptr = NULL;
} else {
size_t n = get_utf8(ttyptr, strlen((char *)ttyptr), &ch);
s->u.irl.ptr += n;
}
return ch;
}
/**
@brief Yap_ReadlinePeekChar peeks the next char from the
readline buffer, but does not actually grab it.
@ -253,8 +317,10 @@ bool init_python_vfs(void) {
pystream.open = py_open;
pystream.close = py_close;
pystream.get_char = py_getc;
pystream.get_wchar = py_wgetc;
pystream.peek_char = py_peek;
pystream.put_char = py_putc;
pystream.put_wchar = py_wputc;
pystream.flush = py_flush;
pystream.seek = py_seek;
pystream.next = GLOBAL_VFS;

View File

@ -7,7 +7,7 @@
from __future__ import print_function
# the name of the package
name = 'ipykernel'
name = 'yap_kernel'
#-----------------------------------------------------------------------------
# Minimal Python version sanity check

View File

@ -13,7 +13,7 @@ events and the arguments which will be passed to them.
This API is experimental in yap_ipython 2.0, and may be revised in future versions.
"""
from yap_ipython.core.backcall import callback_prototype
from backcall import callback_prototype
class EventManager(object):

View File

@ -1,10 +1,10 @@
# encoding: utf-8
"""
Extra capabilities for yap_ipython
Extra capabilities for IPython
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The yap_ipython Development Team
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.

View File

@ -15,7 +15,7 @@ Computational Science, by H. P. Langtangen:
http://folk.uio.no/hpl/scripting
(although ultimately no code from this text was used, as yap_ipython's system is a
(although ultimately no code from this text was used, as IPython's system is a
separate implementation).
An example notebook is provided in our documentation illustrating interactive

View File

@ -1,7 +1,7 @@
"""Module for interactive demos using yap_ipython.
This module implements a few classes for running Python scripts interactively
in yap_ipython for demonstrations. With very simple markup (a few tags in
in IPython for demonstrations. With very simple markup (a few tags in
comments), you can control points where the script stops executing and returns
control to yap_ipython.
@ -13,15 +13,15 @@ The classes are (see their docstrings for further details):
- Demo: pure python demos
- IPythonDemo: demos with input to be processed by yap_ipython as if it had been
- IPythonDemo: demos with input to be processed by IPython as if it had been
typed interactively (so magics work, as well as any other special syntax you
may have added via input prefilters).
- LineDemo: single-line version of the Demo class. These demos are executed
one line at a time, and require no markup.
- IPythonLineDemo: yap_ipython version of the LineDemo class (the demo is
executed a line at a time, but processed via yap_ipython).
- IPythonLineDemo: IPython version of the LineDemo class (the demo is
executed a line at a time, but processed via IPython).
- ClearMixin: mixin to make Demo classes with less visual clutter. It
declares an empty marquee and a pre_cmd that clears the screen before each
@ -58,7 +58,7 @@ Operation
The file is run in its own empty namespace (though you can pass it a string of
arguments as if in a command line environment, and it will see those as
sys.argv). But at each stop, the global yap_ipython namespace is updated with the
sys.argv). But at each stop, the global IPython namespace is updated with the
current internal demo namespace, so you can work interactively with the data
accumulated so far.
@ -72,7 +72,7 @@ The supported tags are:
# <demo> stop
Defines block boundaries, the points where yap_ipython stops execution of the
Defines block boundaries, the points where IPython stops execution of the
file and returns to the interactive prompt.
You can optionally mark the stop tag with extra dashes before and after the
@ -120,7 +120,7 @@ and back(). It can be reset for a new run via reset() or reloaded from disk
Note: To make this simpler to explore, a file called "demo-exercizer.py" has
been added to the "docs/examples/core" directory. Just cd to this directory in
an yap_ipython session, and type::
an IPython session, and type::
%run demo-exercizer.py
@ -134,11 +134,11 @@ The following is a very simple example of a valid demo file.
::
#################### EXAMPLE DEMO <ex_demo.py> ###############################
'''A simple interactive demo to illustrate the use of yap_ipython's Demo class.'''
'''A simple interactive demo to illustrate the use of IPython's Demo class.'''
print 'Hello, welcome to an interactive yap_ipython demo.'
print 'Hello, welcome to an interactive IPython demo.'
# The mark below defines a block boundary, which is a point where yap_ipython will
# The mark below defines a block boundary, which is a point where IPython will
# stop execution and return to the interactive prompt. The dashes are actually
# optional and used only as a visual aid to clearly separate blocks while
# editing the demo code.
@ -188,7 +188,7 @@ import pygments
from yap_ipython.utils.text import marquee
from yap_ipython.utils import openpy
from yap_ipython.utils import py3compat
__all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
__all__ = ['Demo','yap_ipythonDemo','LineDemo','yap_ipythonLineDemo','DemoError']
class DemoError(Exception): pass
@ -536,10 +536,10 @@ class Demo(object):
class IPythonDemo(Demo):
"""Class for interactive demos with yap_ipython's input processing applied.
"""Class for interactive demos with IPython's input processing applied.
This subclasses Demo, but instead of executing each block by the Python
interpreter (via exec), it actually calls yap_ipython on it, so that any input
interpreter (via exec), it actually calls IPython on it, so that any input
filters which may be in place are applied to the input block.
If you have an interactive environment which exposes special input

View File

@ -2,13 +2,14 @@
Authors : MinRK, gregcaporaso, dannystaple
"""
from html import escape as html_escape
from os.path import exists, isfile, splitext, abspath, join, isdir
from os import walk, sep
from os import walk, sep, fsdecode
from yap_ipython.core.display import DisplayObject
from yap_ipython.core.display import DisplayObject, TextDisplayObject
__all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
'FileLink', 'FileLinks']
'FileLink', 'FileLinks', 'Code']
class Audio(DisplayObject):
@ -197,7 +198,7 @@ class Audio(DisplayObject):
class IFrame(object):
"""
Generic class to embed an iframe in an yap_ipython notebook
Generic class to embed an iframe in an IPython notebook
"""
iframe = """
@ -232,7 +233,7 @@ class IFrame(object):
params=params)
class YouTubeVideo(IFrame):
"""Class for embedding a YouTube Video in an yap_ipython session, based on its video id.
"""Class for embedding a YouTube Video in an IPython session, based on its video id.
e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would
do::
@ -273,7 +274,7 @@ class YouTubeVideo(IFrame):
class VimeoVideo(IFrame):
"""
Class for embedding a Vimeo video in an yap_ipython session, based on its video id.
Class for embedding a Vimeo video in an IPython session, based on its video id.
"""
def __init__(self, id, width=400, height=300, **kwargs):
@ -282,7 +283,7 @@ class VimeoVideo(IFrame):
class ScribdDocument(IFrame):
"""
Class for embedding a Scribd document in an yap_ipython session
Class for embedding a Scribd document in an IPython session
Use the start_page params to specify a starting point in the document
Use the view_mode params to specify display type one off scroll | slideshow | book
@ -297,9 +298,9 @@ class ScribdDocument(IFrame):
super(ScribdDocument, self).__init__(src, width, height, **kwargs)
class FileLink(object):
"""Class for embedding a local file link in an yap_ipython session, based on path
"""Class for embedding a local file link in an IPython session, based on path
e.g. to embed a link that was generated in the yap_ipython notebook as my/data.txt
e.g. to embed a link that was generated in the IPython notebook as my/data.txt
you would do::
@ -334,15 +335,16 @@ class FileLink(object):
if isdir(path):
raise ValueError("Cannot display a directory using FileLink. "
"Use FileLinks to display '%s'." % path)
self.path = path
self.path = fsdecode(path)
self.url_prefix = url_prefix
self.result_html_prefix = result_html_prefix
self.result_html_suffix = result_html_suffix
def _format_path(self):
fp = ''.join([self.url_prefix,self.path])
fp = ''.join([self.url_prefix, html_escape(self.path)])
return ''.join([self.result_html_prefix,
self.html_link_str % (fp, self.path),
self.html_link_str % \
(fp, html_escape(self.path, quote=False)),
self.result_html_suffix])
def _repr_html_(self):
@ -362,9 +364,9 @@ class FileLink(object):
return abspath(self.path)
class FileLinks(FileLink):
"""Class for embedding local file links in an yap_ipython session, based on path
"""Class for embedding local file links in an IPython session, based on path
e.g. to embed links to files that were generated in the yap_ipython notebook
e.g. to embed links to files that were generated in the IPython notebook
under ``my/data``, you would do::
local_files = FileLinks("my/data")
@ -424,7 +426,7 @@ class FileLinks(FileLink):
raise ValueError("Cannot display a file using FileLinks. "
"Use FileLink to display '%s'." % path)
self.included_suffixes = included_suffixes
# remove trailing slashs for more consistent output formatting
# remove trailing slashes for more consistent output formatting
path = path.rstrip('/')
self.path = path
@ -555,3 +557,52 @@ class FileLinks(FileLink):
for dirname, subdirs, fnames in walked_dir:
result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
return '\n'.join(result_lines)
class Code(TextDisplayObject):
"""Display syntax-highlighted source code.
This uses Pygments to highlight the code for HTML and Latex output.
Parameters
----------
data : str
The code as a string
url : str
A URL to fetch the code from
filename : str
A local filename to load the code from
language : str
The short name of a Pygments lexer to use for highlighting.
If not specified, it will guess the lexer based on the filename
or the code. Available lexers: http://pygments.org/docs/lexers/
"""
def __init__(self, data=None, url=None, filename=None, language=None):
self.language = language
super().__init__(data=data, url=url, filename=filename)
def _get_lexer(self):
if self.language:
from pygments.lexers import get_lexer_by_name
return get_lexer_by_name(self.language)
elif self.filename:
from pygments.lexers import get_lexer_for_filename
return get_lexer_for_filename(self.filename)
else:
from pygments.lexers import guess_lexer
return guess_lexer(self.data)
def __repr__(self):
return self.data
def _repr_html_(self):
from pygments import highlight
from pygments.formatters import HtmlFormatter
fmt = HtmlFormatter()
style = '<style>{}</style>'.format(fmt.get_style_defs('.output_html'))
return style + highlight(self.data, self._get_lexer(), fmt)
def _repr_latex_(self):
from pygments import highlight
from pygments.formatters import LatexFormatter
return highlight(self.data, self._get_lexer(), LatexFormatter())

View File

@ -11,25 +11,25 @@ import shlex
import subprocess
import sys
from yap_ipython import get_ipython
from IPython import get_ipython
from yap_ipython.core.error import TryNext
from yap_ipython.utils import py3compat
def install_editor(template, wait=False):
"""Installs the editor that is called by yap_ipython for the %edit magic.
"""Installs the editor that is called by IPython for the %edit magic.
This overrides the default editor, which is generally set by your EDITOR
environment variable or is notepad (windows) or vi (linux). By supplying a
template string `run_template`, you can control how the editor is invoked
by yap_ipython -- (e.g. the format in which it accepts command line options)
by IPython -- (e.g. the format in which it accepts command line options)
Parameters
----------
template : basestring
run_template acts as a template for how your editor is invoked by
the shell. It should contain '{filename}', which will be replaced on
invokation with the file name, and '{line}', $line by line number
invocation with the file name, and '{line}', $line by line number
(or 0) to invoke the file with.
wait : bool
If `wait` is true, wait until the user presses enter before returning,

View File

@ -2,26 +2,26 @@
"""
Support for creating GUI apps and starting event loops.
yap_ipython's GUI integration allows interative plotting and GUI usage in yap_ipython
session. yap_ipython has two different types of GUI integration:
IPython's GUI integration allows interactive plotting and GUI usage in IPython
session. IPython has two different types of GUI integration:
1. The terminal based yap_ipython supports GUI event loops through Python's
1. The terminal based IPython supports GUI event loops through Python's
PyOS_InputHook. PyOS_InputHook is a hook that Python calls periodically
whenever raw_input is waiting for a user to type code. We implement GUI
support in the terminal by setting PyOS_InputHook to a function that
iterates the event loop for a short while. It is important to note that
in this situation, the real GUI event loop is NOT run in the normal
manner, so you can't use the normal means to detect that it is running.
2. In the two process yap_ipython kernel/frontend, the GUI event loop is run in
2. In the two process IPython kernel/frontend, the GUI event loop is run in
the kernel. In this case, the event loop is run in the normal manner by
calling the function or method of the GUI toolkit that starts the event
loop.
In addition to starting the GUI event loops in one of these two ways, yap_ipython
In addition to starting the GUI event loops in one of these two ways, IPython
will *always* create an appropriate GUI application object when GUi
integration is enabled.
If you want your GUI apps to run in yap_ipython you need to do two things:
If you want your GUI apps to run in IPython you need to do two things:
1. Test to see if there is already an existing main application object. If
there is, you should use it. If there is not an existing application object
@ -57,7 +57,7 @@ so you don't have to depend on yap_ipython.
"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from yap_ipython.core.getipython import get_ipython

View File

@ -1,11 +1,11 @@
# coding: utf-8
"""
Deprecated since yap_ipython 5.0
Deprecated since IPython 5.0
Inputhook management for GUI event loop integration.
"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
try:
@ -22,7 +22,7 @@ from distutils.version import LooseVersion as V
from warnings import warn
warn("`yap_ipython.lib.inputhook` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`yap_ipython.lib.inputhook` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
@ -105,7 +105,7 @@ else:
class InputHookManager(object):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Manage PyOS_InputHook for different GUI toolkits.
@ -115,7 +115,7 @@ class InputHookManager(object):
def __init__(self):
if ctypes is None:
warn("yap_ipython GUI event loop requires ctypes, %gui will not be available")
warn("IPython GUI event loop requires ctypes, %gui will not be available")
else:
self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
self.guihooks = {}
@ -130,23 +130,23 @@ class InputHookManager(object):
self._current_gui = None
def get_pyos_inputhook(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Return the current PyOS_InputHook as a ctypes.c_void_p."""
warn("`get_pyos_inputhook` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`get_pyos_inputhook` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
def get_pyos_inputhook_as_func(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Return the current PyOS_InputHook as a ctypes.PYFUNCYPE."""
warn("`get_pyos_inputhook_as_func` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`get_pyos_inputhook_as_func` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
def set_inputhook(self, callback):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Set PyOS_InputHook to callback and return the previous one."""
# On platforms with 'readline' support, it's all too likely to
@ -164,7 +164,7 @@ class InputHookManager(object):
return original
def clear_inputhook(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Set PyOS_InputHook to NULL and return the previous one.
@ -174,9 +174,9 @@ class InputHookManager(object):
This parameter is allowed only so that clear_inputhook() can be
called with a similar interface as all the ``enable_*`` methods. But
the actual value of the parameter is ignored. This uniform interface
makes it easier to have user-level entry points in the main yap_ipython
makes it easier to have user-level entry points in the main IPython
app like :meth:`enable_gui`."""
warn("`clear_inputhook` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`clear_inputhook` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
pyos_inputhook_ptr = self.get_pyos_inputhook()
original = self.get_pyos_inputhook_as_func()
@ -186,9 +186,9 @@ class InputHookManager(object):
return original
def clear_app_refs(self, gui=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Clear yap_ipython's internal reference to an application instance.
Clear IPython's internal reference to an application instance.
Whenever we create an app for a user on qt4 or wx, we hold a
reference to the app. This is needed because in some cases bad things
@ -202,7 +202,7 @@ class InputHookManager(object):
the app for that toolkit. References are not held for gtk or tk
as those toolkits don't have the notion of an app.
"""
warn("`clear_app_refs` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`clear_app_refs` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
if gui is None:
self.apps = {}
@ -210,7 +210,7 @@ class InputHookManager(object):
del self.apps[gui]
def register(self, toolkitname, *aliases):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Register a class to provide the event loop for a given GUI.
@ -225,7 +225,7 @@ class InputHookManager(object):
def enable(self, app=None):
...
"""
warn("`register` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`register` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
def decorator(cls):
if ctypes is not None:
@ -237,15 +237,15 @@ class InputHookManager(object):
return decorator
def current_gui(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Return a string indicating the currently active GUI or None."""
warn("`current_gui` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`current_gui` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
return self._current_gui
def enable_gui(self, gui=None, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Switch amongst GUI input hooks by name.
@ -271,7 +271,7 @@ class InputHookManager(object):
PyOS_InputHook wrapper object or the GUI toolkit app created, if there was
one.
"""
warn("`enable_gui` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`enable_gui` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
if gui in (None, GUI_NONE):
return self.disable_gui()
@ -293,14 +293,14 @@ class InputHookManager(object):
return app
def disable_gui(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Disable GUI event loop integration.
If an application was registered, this sets its ``_in_event_loop``
attribute to False. It then calls :meth:`clear_inputhook`.
"""
warn("`disable_gui` is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("`disable_gui` is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
gui = self._current_gui
if gui in self.apps:
@ -308,7 +308,7 @@ class InputHookManager(object):
return self.clear_inputhook()
class InputHookBase(object):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Base class for input hooks for specific toolkits.
@ -326,17 +326,17 @@ inputhook_manager = InputHookManager()
@inputhook_manager.register('osx')
class NullInputHook(InputHookBase):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
A null inputhook that doesn't need to do anything"""
def enable(self, app=None):
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
@inputhook_manager.register('wx')
class WxInputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with wxPython.
@ -359,7 +359,7 @@ class WxInputHook(InputHookBase):
import wx
app = wx.App(redirect=False, clearSigInt=False)
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
import wx
@ -383,13 +383,13 @@ class WxInputHook(InputHookBase):
return app
def disable(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Disable event loop integration with wxPython.
This restores appnapp on OS X
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
if _use_appnope():
from appnope import nap
@ -398,7 +398,7 @@ class WxInputHook(InputHookBase):
@inputhook_manager.register('qt', 'qt4')
class Qt4InputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with PyQt4.
@ -421,7 +421,7 @@ class Qt4InputHook(InputHookBase):
from PyQt4 import QtCore
app = QtGui.QApplication(sys.argv)
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
from yap_ipython.lib.inputhookqt4 import create_inputhook_qt4
app, inputhook_qt4 = create_inputhook_qt4(self.manager, app)
@ -433,13 +433,13 @@ class Qt4InputHook(InputHookBase):
return app
def disable_qt4(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Disable event loop integration with PyQt4.
This restores appnapp on OS X
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
if _use_appnope():
from appnope import nap
@ -449,7 +449,7 @@ class Qt4InputHook(InputHookBase):
@inputhook_manager.register('qt5')
class Qt5InputHook(Qt4InputHook):
def enable(self, app=None):
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
os.environ['QT_API'] = 'pyqt5'
return Qt4InputHook.enable(self, app)
@ -458,7 +458,7 @@ class Qt5InputHook(Qt4InputHook):
@inputhook_manager.register('gtk')
class GtkInputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with PyGTK.
@ -475,7 +475,7 @@ class GtkInputHook(InputHookBase):
the PyGTK to integrate with terminal based applications like
yap_ipython.
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
import gtk
try:
@ -489,7 +489,7 @@ class GtkInputHook(InputHookBase):
@inputhook_manager.register('tk')
class TkInputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with Tk.
@ -506,7 +506,7 @@ class TkInputHook(InputHookBase):
:class:`InputHookManager`, since creating that object automatically
sets ``PyOS_InputHook``.
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
if app is None:
try:
@ -522,7 +522,7 @@ class TkInputHook(InputHookBase):
@inputhook_manager.register('glut')
class GlutInputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with GLUT.
@ -547,7 +547,7 @@ class GlutInputHook(InputHookBase):
The default screen mode is set to:
glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
import OpenGL.GLUT as glut
@ -576,7 +576,7 @@ class GlutInputHook(InputHookBase):
def disable(self):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Disable event loop integration with glut.
@ -584,7 +584,7 @@ class GlutInputHook(InputHookBase):
dummy one and set the timer to a dummy timer that will be triggered
very far in the future.
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
import OpenGL.GLUT as glut
from glut_support import glutMainLoopEvent
@ -596,7 +596,7 @@ class GlutInputHook(InputHookBase):
@inputhook_manager.register('pyglet')
class PygletInputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with pyglet.
@ -614,7 +614,7 @@ class PygletInputHook(InputHookBase):
yap_ipython.
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
from yap_ipython.lib.inputhookpyglet import inputhook_pyglet
self.manager.set_inputhook(inputhook_pyglet)
@ -624,7 +624,7 @@ class PygletInputHook(InputHookBase):
@inputhook_manager.register('gtk3')
class Gtk3InputHook(InputHookBase):
def enable(self, app=None):
"""DEPRECATED since yap_ipython 5.0
"""DEPRECATED since IPython 5.0
Enable event loop integration with Gtk3 (gir bindings).
@ -641,7 +641,7 @@ class Gtk3InputHook(InputHookBase):
the Gtk3 to integrate with terminal based applications like
yap_ipython.
"""
warn("This function is deprecated since yap_ipython 5.0 and will be removed in future versions.",
warn("This function is deprecated since IPython 5.0 and will be removed in future versions.",
DeprecationWarning, stacklevel=2)
from yap_ipython.lib.inputhookgtk3 import inputhook_gtk3
self.manager.set_inputhook(inputhook_gtk3)
@ -658,7 +658,7 @@ guis = inputhook_manager.guihooks
def _deprecated_disable():
warn("This function is deprecated since yap_ipython 4.0 use disable_gui() instead",
warn("This function is deprecated since IPython 4.0 use disable_gui() instead",
DeprecationWarning, stacklevel=2)
inputhook_manager.disable_gui()

View File

@ -4,14 +4,14 @@ GLUT Inputhook support functions
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The yap_ipython Development Team
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
# GLUT is quite an old library and it is difficult to ensure proper
# integration within yap_ipython since original GLUT does not allow to handle
# integration within IPython since original GLUT does not allow to handle
# events one by one. Instead, it requires for the mainloop to be entered
# and never returned (there is not even a function to exit he
# mainloop). Fortunately, there are alternatives such as freeglut
@ -24,7 +24,7 @@ GLUT Inputhook support functions
# being first created. We choose to make this window invisible. This means that
# display mode options are set at this level and user won't be able to change
# them later without modifying the code. This should probably be made available
# via yap_ipython options system.
# via IPython options system.
#-----------------------------------------------------------------------------
# Imports
@ -42,12 +42,12 @@ from timeit import default_timer as clock
#-----------------------------------------------------------------------------
# Frame per second : 60
# Should probably be an yap_ipython option
# Should probably be an IPython option
glut_fps = 60
# Display mode : double buffeed + rgba + depth
# Should probably be an yap_ipython option
# Should probably be an IPython option
glut_display_mode = (glut.GLUT_DOUBLE |
glut.GLUT_RGBA |
glut.GLUT_DEPTH)
@ -112,7 +112,7 @@ def glut_close():
glutMainLoopEvent()
def glut_int_handler(signum, frame):
# Catch sigint and print the defautl message
# Catch sigint and print the default message
signal.signal(signal.SIGINT, signal.default_int_handler)
print('\nKeyboardInterrupt')
# Need to reprint the prompt at this stage
@ -130,7 +130,7 @@ def inputhook_glut():
needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
though for best performance.
"""
# We need to protect against a user pressing Control-C when yap_ipython is
# We need to protect against a user pressing Control-C when IPython is
# idle and this is running. We trap KeyboardInterrupt and pass.
signal.signal(signal.SIGINT, glut_int_handler)

View File

@ -6,7 +6,7 @@ Authors: Brian Granger
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The yap_ipython Development Team
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.

View File

@ -5,7 +5,7 @@ Enable Gtk3 to be used interacive by yap_ipython.
Authors: Thomi Richards
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the yap_ipython Development Team.
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#

View File

@ -10,7 +10,7 @@ Authors
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The yap_ipython Development Team
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
@ -73,7 +73,7 @@ def inputhook_pyglet():
needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
though for best performance.
"""
# We need to protect against a user pressing Control-C when yap_ipython is
# We need to protect against a user pressing Control-C when IPython is
# idle and this is running. We trap KeyboardInterrupt and pass.
try:
t = clock()

View File

@ -6,7 +6,7 @@ Author: Christian Boos
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The yap_ipython Development Team
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.

View File

@ -7,7 +7,7 @@ Authors: Robin Dunn, Brian Granger, Ondrej Certik
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The yap_ipython Development Team
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
@ -110,7 +110,7 @@ def inputhook_wx3():
time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%.
This sleep time should be tuned though for best performance.
"""
# We need to protect against a user pressing Control-C when yap_ipython is
# We need to protect against a user pressing Control-C when IPython is
# idle and this is running. We trap KeyboardInterrupt and pass.
try:
app = wx.GetApp()

View File

@ -4,10 +4,10 @@ Moved to yap_ipython.kernel.connect
"""
import warnings
warnings.warn("yap_ipython.lib.kernel moved to yap_ipython.kernel.connect in yap_ipython 1.0,"
" and will be removed in yap_ipython 6.0.",
warnings.warn("yap_ipython.lib.kernel moved to yap_ipython.kernel.connect in IPython 1.0,"
" and will be removed in IPython 6.0.",
DeprecationWarning
)
from yap_kernel.connect import *
from ipykernel.connect import *

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tools for handling LaTeX."""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from io import BytesIO, open

View File

@ -1,29 +1,29 @@
# -*- coding: utf-8 -*-
"""
Defines a variety of Pygments lexers for highlighting yap_ipython code.
Defines a variety of Pygments lexers for highlighting IPython code.
This includes:
IPythonLexer, IPython3Lexer
Lexers for pure yap_ipython (python + magic/shell commands)
Lexers for pure IPython (python + magic/shell commands)
IPythonPartialTracebackLexer, IPythonTracebackLexer
Supports 2.x and 3.x via keyword `python3`. The partial traceback
lexer reads everything but the Python code appearing in a traceback.
The full lexer combines the partial lexer with an yap_ipython lexer.
The full lexer combines the partial lexer with an IPython lexer.
IPythonConsoleLexer
A lexer for yap_ipython console sessions, with support for tracebacks.
A lexer for IPython console sessions, with support for tracebacks.
IPyLexer
A friendly lexer which examines the first line of text and from it,
decides whether to use an yap_ipython lexer or an yap_ipython console lexer.
decides whether to use an IPython lexer or an IPython console lexer.
This is probably the only lexer that needs to be explicitly added
to Pygments.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the yap_ipython Development Team.
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
@ -34,7 +34,9 @@ This includes:
import re
# Third party
from pygments.lexers import BashLexer, PythonLexer, Python3Lexer
from pygments.lexers import (
BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer,
Python3Lexer, TexLexer)
from pygments.lexer import (
Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using,
)
@ -51,36 +53,23 @@ __all__ = ['build_ipy_lexer', 'IPython3Lexer', 'IPythonLexer',
'IPythonPartialTracebackLexer', 'IPythonTracebackLexer',
'IPythonConsoleLexer', 'IPyLexer']
ipython_tokens = [
(r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)),
(r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))),
(r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)),
(r"\b(\?\??)(\s*)$", bygroups(Operator, Text)),
(r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword,
using(BashLexer), Text)),
(r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)),
(r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)),
(r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)),
(r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)),
(r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)),
]
def build_ipy_lexer(python3):
"""Builds yap_ipython lexers depending on the value of `python3`.
"""Builds IPython lexers depending on the value of `python3`.
The lexer inherits from an appropriate Python lexer and then adds
information about yap_ipython specific keywords (i.e. magic commands,
information about IPython specific keywords (i.e. magic commands,
shell commands, etc.)
Parameters
----------
python3 : bool
If `True`, then build an yap_ipython lexer from a Python 3 lexer.
If `True`, then build an IPython lexer from a Python 3 lexer.
"""
# It would be nice to have a single yap_ipython lexer class which takes
# It would be nice to have a single IPython lexer class which takes
# a boolean `python3`. But since there are two Python lexer classes,
# we will also have two yap_ipython lexer classes.
# we will also have two IPython lexer classes.
if python3:
PyLexer = Python3Lexer
name = 'IPython3'
@ -88,9 +77,39 @@ def build_ipy_lexer(python3):
doc = """IPython3 Lexer"""
else:
PyLexer = PythonLexer
name = 'yap_ipython'
name = 'IPython'
aliases = ['ipython2', 'ipython']
doc = """yap_ipython Lexer"""
doc = """IPython Lexer"""
ipython_tokens = [
(r'(?s)(\s*)(%%capture)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%debug)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?is)(\s*)(%%html)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(HtmlLexer))),
(r'(?s)(\s*)(%%javascript)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))),
(r'(?s)(\s*)(%%js)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))),
(r'(?s)(\s*)(%%latex)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(TexLexer))),
(r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PerlLexer))),
(r'(?s)(\s*)(%%prun)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%python)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%python2)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PythonLexer))),
(r'(?s)(\s*)(%%python3)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(Python3Lexer))),
(r'(?s)(\s*)(%%ruby)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(RubyLexer))),
(r'(?s)(\s*)(%%time)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%timeit)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r'(?s)(\s*)(%%writefile)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))),
(r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)),
(r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))),
(r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)),
(r"\b(\?\??)(\s*)$", bygroups(Operator, Text)),
(r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword,
using(BashLexer), Text)),
(r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)),
(r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)),
(r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)),
(r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)),
(r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)),
]
tokens = PyLexer.tokens.copy()
tokens['root'] = ipython_tokens + tokens['root']
@ -107,12 +126,12 @@ IPythonLexer = build_ipy_lexer(python3=False)
class IPythonPartialTracebackLexer(RegexLexer):
"""
Partial lexer for yap_ipython tracebacks.
Partial lexer for IPython tracebacks.
Handles all the non-python output. This works for both Python 2.x and 3.x.
"""
name = 'yap_ipython Partial Traceback'
name = 'IPython Partial Traceback'
tokens = {
'root': [
@ -156,7 +175,7 @@ class IPythonPartialTracebackLexer(RegexLexer):
class IPythonTracebackLexer(DelegatingLexer):
"""
yap_ipython traceback lexer.
IPython traceback lexer.
For doctests, the tracebacks can be snipped as much as desired with the
exception to the lines that designate a traceback. For non-syntax error
@ -165,12 +184,12 @@ class IPythonTracebackLexer(DelegatingLexer):
"""
# The lexer inherits from DelegatingLexer. The "root" lexer is an
# appropriate yap_ipython lexer, which depends on the value of the boolean
# `python3`. First, we parse with the partial yap_ipython traceback lexer.
# appropriate IPython lexer, which depends on the value of the boolean
# `python3`. First, we parse with the partial IPython traceback lexer.
# Then, any code marked with the "Other" token is delegated to the root
# lexer.
#
name = 'yap_ipython Traceback'
name = 'IPython Traceback'
aliases = ['ipythontb']
def __init__(self, **options):
@ -190,7 +209,7 @@ class IPythonTracebackLexer(DelegatingLexer):
class IPythonConsoleLexer(Lexer):
"""
An yap_ipython console lexer for yap_ipython code-blocks and doctests, such as:
An IPython console lexer for IPython code-blocks and doctests, such as:
.. code-block:: rst
@ -207,7 +226,7 @@ class IPythonConsoleLexer(Lexer):
In [4]: 1 / 0
Support is also provided for yap_ipython exceptions:
Support is also provided for IPython exceptions:
.. code-block:: rst
@ -217,18 +236,18 @@ class IPythonConsoleLexer(Lexer):
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-1-fca2ab0ca76b> in <module>()
<ipython-input-1-fca2ab0ca76b> in <module>
----> 1 raise Exception
Exception:
"""
name = 'yap_ipython console session'
name = 'IPython console session'
aliases = ['ipythonconsole']
mimetypes = ['text/x-ipython-console']
# The regexps used to determine what is input and what is output.
# The default prompts for yap_ipython are:
# The default prompts for IPython are:
#
# in = 'In [#]: '
# continuation = ' .D.: '
@ -245,7 +264,7 @@ class IPythonConsoleLexer(Lexer):
ipytb_start = re.compile(r'^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)')
def __init__(self, **options):
"""Initialize the yap_ipython console lexer.
"""Initialize the IPython console lexer.
Parameters
----------
@ -254,12 +273,12 @@ class IPythonConsoleLexer(Lexer):
lexer. Otherwise, they are parsed using a Python 2 lexer.
in1_regex : RegexObject
The compiled regular expression used to detect the start
of inputs. Although the yap_ipython configuration setting may have a
of inputs. Although the IPython configuration setting may have a
trailing whitespace, do not include it in the regex. If `None`,
then the default input prompt is assumed.
in2_regex : RegexObject
The compiled regular expression used to detect the continuation
of inputs. Although the yap_ipython configuration setting may have a
of inputs. Although the IPython configuration setting may have a
trailing whitespace, do not include it in the regex. If `None`,
then the default input prompt is assumed.
out_regex : RegexObject
@ -475,11 +494,11 @@ class IPythonConsoleLexer(Lexer):
class IPyLexer(Lexer):
"""
Primary lexer for all yap_ipython-like code.
Primary lexer for all IPython-like code.
This is a simple helper lexer. If the first line of the text begins with
"In \[[0-9]+\]:", then the entire text is parsed with an yap_ipython console
lexer. If not, then the entire text is parsed with an yap_ipython lexer.
"In \[[0-9]+\]:", then the entire text is parsed with an IPython console
lexer. If not, then the entire text is parsed with an IPython lexer.
The goal is to reduce the number of lexers that are registered
with Pygments.

View File

@ -77,11 +77,13 @@ Inheritance diagram:
Portions (c) 2009 by Robert Kern.
:license: BSD License.
"""
from contextlib import contextmanager
import datetime
import os
import re
import sys
import types
import re
import datetime
from collections import deque
from io import StringIO
from warnings import warn
@ -95,6 +97,9 @@ __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
MAX_SEQ_LENGTH = 1000
# The language spec says that dicts preserve order from 3.7, but CPython
# does so from 3.6, so it seems likely that people will expect that.
DICT_IS_ORDERED = sys.version_info >= (3, 6)
_re_pattern_type = type(re.compile(''))
def _safe_getattr(obj, attr, default=None):
@ -112,7 +117,7 @@ def _safe_getattr(obj, attr, default=None):
class CUnicodeIO(StringIO):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
warn(("CUnicodeIO is deprecated since yap_ipython 6.0. "
warn(("CUnicodeIO is deprecated since IPython 6.0. "
"Please use io.StringIO instead."),
DeprecationWarning, stacklevel=2)
@ -392,6 +397,10 @@ class RepresentationPrinter(PrettyPrinter):
meth = cls._repr_pretty_
if callable(meth):
return meth(obj, self, cycle)
if cls is not object \
and callable(cls.__dict__.get('__repr__')):
return _repr_pprint(obj, self, cycle)
return _default_pprint(obj, self, cycle)
finally:
self.end_group()
@ -537,17 +546,12 @@ def _default_pprint(obj, p, cycle):
p.end_group(1, '>')
def _seq_pprinter_factory(start, end, basetype):
def _seq_pprinter_factory(start, end):
"""
Factory that returns a pprint function useful for sequences. Used by
the default pprint for tuples, dicts, and lists.
"""
def inner(obj, p, cycle):
typ = type(obj)
if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
# If the subclass provides its own repr, use it instead.
return p.text(typ.__repr__(obj))
if cycle:
return p.text(start + '...' + end)
step = len(start)
@ -564,21 +568,16 @@ def _seq_pprinter_factory(start, end, basetype):
return inner
def _set_pprinter_factory(start, end, basetype):
def _set_pprinter_factory(start, end):
"""
Factory that returns a pprint function useful for sets and frozensets.
"""
def inner(obj, p, cycle):
typ = type(obj)
if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
# If the subclass provides its own repr, use it instead.
return p.text(typ.__repr__(obj))
if cycle:
return p.text(start + '...' + end)
if len(obj) == 0:
# Special case.
p.text(basetype.__name__ + '()')
p.text(type(obj).__name__ + '()')
else:
step = len(start)
p.begin_group(step, start)
@ -596,24 +595,21 @@ def _set_pprinter_factory(start, end, basetype):
return inner
def _dict_pprinter_factory(start, end, basetype=None):
def _dict_pprinter_factory(start, end):
"""
Factory that returns a pprint function used by the default pprint of
dicts and dict proxies.
"""
def inner(obj, p, cycle):
typ = type(obj)
if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
# If the subclass provides its own repr, use it instead.
return p.text(typ.__repr__(obj))
if cycle:
return p.text('{...}')
step = len(start)
p.begin_group(step, start)
keys = obj.keys()
# if dict isn't large enough to be truncated, sort keys before displaying
if not (p.max_seq_length and len(obj) >= p.max_seq_length):
# From Python 3.7, dicts preserve order by definition, so we don't sort.
if not DICT_IS_ORDERED \
and not (p.max_seq_length and len(obj) >= p.max_seq_length):
keys = _sorted_for_pprint(keys)
for idx, key in p._enumerate(keys):
if idx:
@ -745,24 +741,28 @@ _type_pprinters = {
int: _repr_pprint,
float: _repr_pprint,
str: _repr_pprint,
tuple: _seq_pprinter_factory('(', ')', tuple),
list: _seq_pprinter_factory('[', ']', list),
dict: _dict_pprinter_factory('{', '}', dict),
set: _set_pprinter_factory('{', '}', set),
frozenset: _set_pprinter_factory('frozenset({', '})', frozenset),
tuple: _seq_pprinter_factory('(', ')'),
list: _seq_pprinter_factory('[', ']'),
dict: _dict_pprinter_factory('{', '}'),
set: _set_pprinter_factory('{', '}'),
frozenset: _set_pprinter_factory('frozenset({', '})'),
super: _super_pprint,
_re_pattern_type: _re_pattern_pprint,
type: _type_pprint,
types.FunctionType: _function_pprint,
types.BuiltinFunctionType: _function_pprint,
types.MethodType: _repr_pprint,
datetime.datetime: _repr_pprint,
datetime.timedelta: _repr_pprint,
_exception_base: _exception_pprint
}
# render os.environ like a dict
_env_type = type(os.environ)
# future-proof in case os.environ becomes a plain dict?
if _env_type is not dict:
_type_pprinters[_env_type] = _dict_pprinter_factory('environ{', '}')
try:
# In PyPy, types.DictProxyType is dict, setting the dictproxy printer
# using dict.setdefault avoids overwritting the dict printer
@ -774,7 +774,7 @@ except AttributeError: # Python 3
_type_pprinters[types.MappingProxyType] = \
_dict_pprinter_factory('mappingproxy({', '})')
_type_pprinters[slice] = _repr_pprint
try:
_type_pprinters[long] = _repr_pprint
_type_pprinters[unicode] = _repr_pprint

View File

@ -1,5 +1,5 @@
"""
Password generation for the yap_ipython notebook.
Password generation for the IPython notebook.
"""
#-----------------------------------------------------------------------------
# Imports

View File

@ -1,7 +1,7 @@
"""Tests for pylab tools module.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2011, the yap_ipython Development Team.
# Copyright (c) 2011, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
@ -19,7 +19,7 @@ import time
import nose.tools as nt
# Our own imports
from yap_ipython.lib import backgroundjobs as bg
from IPython.lib import backgroundjobs as bg
#-----------------------------------------------------------------------------
# Globals and constants

View File

@ -1,8 +1,8 @@
import nose.tools as nt
from yap_ipython.core.error import TryNext
from yap_ipython.lib.clipboard import ClipboardEmpty
from yap_ipython.testing.decorators import skip_if_no_x11
from IPython.core.error import TryNext
from IPython.lib.clipboard import ClipboardEmpty
from IPython.testing.decorators import skip_if_no_x11
@skip_if_no_x11
def test_clipboard_get():

View File

@ -1,16 +1,16 @@
# -*- coding: utf-8 -*-
"""Test suite for the deepreload module."""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import nose.tools as nt
from yap_ipython.utils.syspathcontext import prepended_to_syspath
from yap_ipython.utils.tempdir import TemporaryDirectory
from yap_ipython.lib.deepreload import reload as dreload
from IPython.utils.syspathcontext import prepended_to_syspath
from IPython.utils.tempdir import TemporaryDirectory
from IPython.lib.deepreload import reload as dreload
def test_deepreload():
"Test that dreload does deep reloads and skips excluded modules."

View File

@ -1,8 +1,8 @@
"""Tests for yap_ipython.lib.display.
"""Tests for IPython.lib.display.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the yap_ipython Development Team.
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
@ -14,13 +14,18 @@
#-----------------------------------------------------------------------------
from tempfile import NamedTemporaryFile, mkdtemp
from os.path import split, join as pjoin, dirname
import sys
try:
import pathlib
except ImportError:
pass
# Third-party imports
import nose.tools as nt
# Our own imports
from yap_ipython.lib import display
from yap_ipython.testing.decorators import skipif_not_numpy
from IPython.lib import display
from IPython.testing.decorators import skipif_not_numpy
#-----------------------------------------------------------------------------
# Classes and functions
@ -33,6 +38,9 @@ from yap_ipython.testing.decorators import skipif_not_numpy
def test_instantiation_FileLink():
"""FileLink: Test class can be instantiated"""
fl = display.FileLink('example.txt')
# TODO: remove if when only Python >= 3.6 is supported
if sys.version_info >= (3, 6):
fl = display.FileLink(pathlib.PurePath('example.txt'))
def test_warning_on_non_existant_path_FileLink():
"""FileLink: Calling _repr_html_ on non-existant files returns a warning
@ -175,3 +183,7 @@ def test_recursive_FileLinks():
def test_audio_from_file():
path = pjoin(dirname(__file__), 'test.wav')
display.Audio(filename=path)
def test_code_from_file():
c = display.Code(filename=__file__)
assert c._repr_html_().startswith('<style>')

View File

@ -4,8 +4,8 @@ from unittest import mock
import nose.tools as nt
from yap_ipython import get_ipython
from yap_ipython.lib import editorhooks
from IPython import get_ipython
from IPython.lib import editorhooks
def test_install_editor():
called = []

View File

@ -1,11 +1,11 @@
# encoding: utf-8
from yap_ipython.testing import decorators as dec
from IPython.testing import decorators as dec
def test_import_backgroundjobs():
from yap_ipython.lib import backgroundjobs
from IPython.lib import backgroundjobs
def test_import_deepreload():
from yap_ipython.lib import deepreload
from IPython.lib import deepreload
def test_import_demo():
from yap_ipython.lib import demo
from IPython.lib import demo

View File

@ -1,14 +1,14 @@
# encoding: utf-8
"""Tests for yap_ipython.utils.path.py"""
"""Tests for IPython.utils.path.py"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from unittest.mock import patch
import nose.tools as nt
from yap_ipython.lib import latextools
from yap_ipython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
from yap_ipython.utils.process import FindCmdError
from IPython.lib import latextools
from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
from IPython.utils.process import FindCmdError
def test_latex_to_png_dvipng_fails_when_no_cmd():

View File

@ -1,6 +1,6 @@
"""Test lexers module"""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from unittest import TestCase
@ -127,3 +127,50 @@ class TestLexers(TestCase):
(Token.Text, '\n'),
]
self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
fragment = '%%writefile -a foo.py\nif a == b:\n pass'
tokens = [
(Token.Operator, '%%writefile'),
(Token.Text, ' -a foo.py\n'),
(Token.Keyword, 'if'),
(Token.Text, ' '),
(Token.Name, 'a'),
(Token.Text, ' '),
(Token.Operator, '=='),
(Token.Text, ' '),
(Token.Name, 'b'),
(Token.Punctuation, ':'),
(Token.Text, '\n'),
(Token.Text, ' '),
(Token.Keyword, 'pass'),
(Token.Text, '\n'),
]
self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
fragment = '%%timeit\nmath.sin(0)'
tokens = [
(Token.Operator, '%%timeit\n'),
(Token.Name, 'math'),
(Token.Operator, '.'),
(Token.Name, 'sin'),
(Token.Punctuation, '('),
(Token.Literal.Number.Integer, '0'),
(Token.Punctuation, ')'),
(Token.Text, '\n'),
]
fragment = '%%HTML\n<div>foo</div>'
tokens = [
(Token.Operator, '%%HTML'),
(Token.Text, '\n'),
(Token.Punctuation, '<'),
(Token.Name.Tag, 'div'),
(Token.Punctuation, '>'),
(Token.Text, 'foo'),
(Token.Punctuation, '<'),
(Token.Punctuation, '/'),
(Token.Name.Tag, 'div'),
(Token.Punctuation, '>'),
(Token.Text, '\n'),
]
self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

View File

@ -1,19 +1,20 @@
# coding: utf-8
"""Tests for yap_ipython.lib.pretty."""
"""Tests for IPython.lib.pretty."""
# Copyright (c) yap_ipython Development Team.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from collections import Counter, defaultdict, deque, OrderedDict
import os
import types
import string
import unittest
import nose.tools as nt
from yap_ipython.lib import pretty
from yap_ipython.testing.decorators import skip_without
from IPython.lib import pretty
from IPython.testing.decorators import skip_without
from io import StringIO
@ -406,6 +407,15 @@ def test_mappingproxy():
for obj, expected in cases:
nt.assert_equal(pretty.pretty(obj), expected)
def test_pretty_environ():
dict_repr = pretty.pretty(dict(os.environ))
# reindent to align with 'environ' prefix
dict_indented = dict_repr.replace('\n', '\n' + (' ' * len('environ')))
env_repr = pretty.pretty(os.environ)
nt.assert_equals(env_repr, 'environ' + dict_indented)
def test_function_pretty():
"Test pretty print of function"
# posixpath is a pure python module, its interface is consistent
@ -420,4 +430,24 @@ def test_function_pretty():
return "Don't panic"
nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life))
class OrderedCounter(Counter, OrderedDict):
'Counter that remembers the order elements are first encountered'
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
def __reduce__(self):
return self.__class__, (OrderedDict(self),)
class MySet(set): # Override repr of a basic type
def __repr__(self):
return 'mine'
def test_custom_repr():
"""A custom repr should override a pretty printer for a parent type"""
oc = OrderedCounter("abracadabra")
nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc))
nt.assert_equal(pretty.pretty(MySet()), 'mine')

View File

@ -1,6 +1,6 @@
# coding: utf-8
from yap_ipython.lib import passwd
from yap_ipython.lib.security import passwd_check, salt_len
from IPython.lib import passwd
from IPython.lib.security import passwd_check, salt_len
import nose.tools as nt
def test_passwd_structure():

View File

@ -10,7 +10,7 @@
:- module( jupyter,
[jupyter_query/3,
blank/1,
streams/1
streams/2
]
).
*/
@ -41,7 +41,7 @@ jupyter_cell(Caller, _, Line ) :-
Query = Caller,
catch(
python_query(Query,Line),
E=error(A,B),
error(A,B),
system_error(A,B)
).
@ -69,7 +69,7 @@ jupyter_consult(Cell) :-
open_mem_read_stream( Cell, Stream),
load_files(user:'jupyter cell',[stream(Stream)| Options])
),
E=error(A,B),
error(A,B),
(close(Stream), system_error(A,B))
),
fail.
@ -91,7 +91,7 @@ blank(Text) :-
close(user_input),
close(user_output),
close(user_error).
streams(true) :-
streams( true) :-
open('/python/input', read, _Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]).

View File

@ -23,7 +23,7 @@
jupyter( []).
ready( Engine, Query) :-
errors( Engine , Cell ),
errors( Engine , Query ),
Es := Engine.errors,
not Es == [].
@ -50,11 +50,11 @@ open_esh(Engine , Text, Stream, Name) :-
Name := Engine.stream_name,
open_mem_read_stream( Text, Stream ).
esh(Engine , Name, Stream) :-
esh(Engine , _Name, Stream) :-
repeat,
catch(
read_clause(Stream, Cl, [ syntax_errors(dec10)]),
error(C,E),
read_clause(Stream, Cl, [ syntax_errors(dec10)]),
error(C,E),
p3_message(C,Engine,E)
),
Cl == end_of_file,
@ -77,19 +77,19 @@ close_esh( _Engine , Stream ) :-
p3_message( _Severity, Engine, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
python_clear_errors,
!,
Engine.errors := [t(Cause,LN,CharPos,Details)]+Engine.errors.
p3_message(error, Engine, E) :-
python_clear_errors,
!,
Engine.errors := [t(Cause,LN,CharPos,Details)]+Engine.errors .
p3_message(error, _Engine, _E) :-
python_clear_errors,
!.
p3_message(warning, Engine, E) :-
!.
p3_message(error, Engine, E) :-
Engine.errors := [E] + Engine.errors.
p3_message(warning, Engine, E) :-
Engine.errors := [E] + Engine.errors.
%% ready(_Self, Line ) :-
p3_message(warning, _Engine, _E) :-
!.
p3_message(error, Engine, E) :-
Engine.errors := [E] + Engine.errors.
p3_message(warning, Engine, E) :-
Engine.errors := [E] + Engine.errors.
%% ready(_Self, Line ) :-
%% blank( Line ),
%% !.
%% ready(Self, Line ) :-

View File

@ -12,7 +12,7 @@ from traitlets import Instance
from yap_ipython.core.inputsplitter import *
from yap_ipython.core.inputtransformer import *
from yap_ipython.core.interactiveshell import *
from ipython_genutils.py3compat import builtin_mod
from yap_ipython.core import interactiveshell
from collections import namedtuple
@ -29,7 +29,7 @@ enter_cell = namedtuple('enter_cell', 'self' )
exit_cell = namedtuple('exit_cell', 'self' )
completions = namedtuple('completions', 'txt self' )
errors = namedtuple('errors', 'self text' )
streams = namedtuple('streams', ' text' )
streams = namedtuple('streams', 'text' )
nostreams = namedtuple('nostreams', ' text' )
global engine
@ -684,7 +684,7 @@ class YAPRun:
for i in self.errors:
try:
(_,lin,pos,text) = i
e = SyntaxError(what, (self.cell_name, lin, pos, text+'\n'))
e = self.SyntaxError( (self.cell_name, lin, pos, text+'\n'))
raise e
except SyntaxError:
self.shell.showsyntaxerror( )
@ -723,7 +723,7 @@ class YAPRun:
# Give the displayhook a reference to our ExecutionResult so it
# can fill in the output value.
self.shell.displayhook.exec_result = self.result
if syntaxErrors(self, text):
if self.syntaxErrors(cell):
self.result.result = False
has_raised = False
try:

View File

@ -172,8 +172,8 @@ class YAPKernel(KernelBase):
else:
self._sys_raw_input = builtin_mod.raw_input
self._sys_eval_input = builtin_mod.input
builtin_mod.raw_input = self.raw_input
builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
builtin_mod.raw_input = self.raw_input
builtin_mod.input = lambda prompt='': eval(self._sys_input(prompt))
self._save_getpass = getpass.getpass
getpass.getpass = self.getpass

View File

@ -695,7 +695,7 @@ db_files(Fs) :-
'$csult'(Fs, _M) :-
'$skip_list'(_, Fs ,L),
L \== [],
vz L \== [],
!,
user:dot_qualified_goal(Fs).
'$csult'(Fs, M) :-

View File

@ -221,7 +221,7 @@ compose_message( loaded(included,AbsFileName,Mod,Time,Space), _Level) --> !,
[ '~a included in module ~a, ~d msec ~d bytes' -
[AbsFileName,Mod,Time,Space] ].
compose_message( loaded(What,AbsoluteFileName,Mod,Time,Space), _Level) --> !,
[ '~a ~a in module ~a, ~d msec ~g bytes' -
[ '~a ~a in module ~a, ~d msec ~d bytes' -
[What, AbsoluteFileName,Mod,Time,Space] ].
compose_message(signal(SIG,_), _) -->
!,
@ -347,8 +347,8 @@ main_error_message(resource_error(Who)) -->
main_error_message(type_error(Type,Who)) -->
[ '~*|** ~q should be of type ~a **' - [10,Who,Type]],
[ nl ].
main_error_message(system_error(Who)) -->
[ '~*|** ~q **' - [10,Who]],
main_error_message(system_error(Who, In)) -->
[ '~*|** ~q ~q **' - [10,Who, In]],
[ nl ].
main_error_message(uninstantiation_error(T)) -->
[ '~*|** found ~q, expected unbound variable **' - [10,T], nl ].
@ -689,11 +689,11 @@ list_of_preds([P|L]) -->
['~q' - [P]],
list_of_preds(L).
syntax_error_term(between(_I,J,_L),S,_LC) -->
syntax_error_term(between(_I,J,_L),[S|T],_LC) -->
{string(S)},
!,
[ '~s' - [S] ],
[' <<<< at line ~d ' - [J], nl ].
[' <<<< at line ~d >>>> ~s' - [J,T], nl ].
syntax_error_term(between(_I,J,_L),LTaL,LC) -->
syntax_error_tokens(LTaL, J, LC).

View File

@ -49,6 +49,7 @@ prolog:'$protect' :-
sub_atom(Name,0,1,_, '$'),
functor(P,Name,Arity),
'$stash_predicate'(P,M),
% '$hide_predicate'(P,M),
fail.
prolog:'$protect' :-
current_atom(Name),