Skip to content

Instantly share code, notes, and snippets.

View strager's full-sized avatar

strager strager

View GitHub Profile
#!/usr/bin/env node
// This tests checks whether fragment identifiers are
// preserved across HTTP redirects.
//
// To run:
// 1. Install Node.js.
// 2. Run this script with 'node fragredirect.js'.
// 3. Open the printed URL in your web browser.
# Based on https://hg.python.org/cpython/file/2.7/Lib/contextlib.py
class GeneratorContextManager(object):
def __init__(self, gen):
self.gen = gen
def __enter__(self):
return self.gen.next()
def __exit__(self, type, value, traceback):
@strager
strager / c-ctypes.md
Created February 27, 2015 06:59
C and ctypes

C and Python ctypes comparison

Totally untested.

/* C */                 # ctypes

char *x = NULL;         x = ctypes.c_char_p(None)
                        x = POINTER(ctypes.c_char)(None)

struct C { }; class C(ctypes.Structure): pass

#include <stdio.h>
int
main(int argc, char **argv) {
int x = 1;
int y = 2;
int *xp = &x;
int *yp = &y;
printf("1: xp = %p; *xp = %d\n", xp, *xp);
printf("2: yp = %p; *xp = %d\n", yp, *yp);
@strager
strager / shell.txt
Last active August 29, 2015 14:20
Valgrind bug (OS X)
$ uname -a
Darwin straddler.local 12.6.0 Darwin Kernel Version 12.6.0: Wed Dec 17 19:11:40 PST 2014; root:xnu-2050.48.15~1/RELEASE_X86_64 x86_64
$ ./test sh -c 'kill -TERM $$'
waitpid returned stats 15
$ valgrind ./test sh -c 'kill -TERM $$'
==63381== Memcheck, a memory error detector
==63381== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==63381== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
$ ninja clean
$ time -p ninja b_SelfCompile
real 1.62
user 1.77
sys 0.27
real 1.63
user 1.78
sys 0.27
64 bytes from 8.8.8.8: icmp_seq=161 ttl=56 time=19.274 ms
64 bytes from 8.8.8.8: icmp_seq=162 ttl=56 time=24.346 ms
64 bytes from 8.8.8.8: icmp_seq=163 ttl=56 time=19.304 ms
64 bytes from 8.8.8.8: icmp_seq=164 ttl=56 time=20.252 ms
64 bytes from 8.8.8.8: icmp_seq=165 ttl=56 time=20.240 ms
64 bytes from 8.8.8.8: icmp_seq=166 ttl=56 time=54.444 ms
64 bytes from 8.8.8.8: icmp_seq=167 ttl=56 time=19.309 ms
64 bytes from 8.8.8.8: icmp_seq=168 ttl=56 time=19.600 ms
64 bytes from 8.8.8.8: icmp_seq=169 ttl=56 time=22.545 ms
64 bytes from 8.8.8.8: icmp_seq=170 ttl=56 time=19.346 ms
#include <iostream>
#include <vector>
namespace {
class F {
public:
template<class T>
F(
T *o,
void (*f)(T *)) :
@strager
strager / steam.js
Created October 9, 2011 23:40
Get Steam username from UID
var http = require('http');
var steamUidRe = /<steamID64>(.*)<\/steamID64>/;
function getSteamUid(username, callback) {
var callbackCalled = false;
var req = http.request({
host: 'steamcommunity.com',
port: 80,
// Why does this code sample hang client
// connections if ASYNC === true?
//
// # Start server
// node test.js --sync port
// node test.js --async port
//
// # Test
// curl http://host:port/